VMware Cloud Community
Hetfield84
Enthusiast
Enthusiast
Jump to solution

Get OS of VMs

Hi,

I'm trying to get the Name and OS of all our VMs using VMware Tools, if VMtools isn't running or the query fails, then I'd like to get the OS from the VM properties. The reason for this, is because we have had cases where a VM has had it's OS upgraded but the VM OS property was not manually changed so the guest OS and the OS listed in the properties do not align.

The script I've written below produces a lot of errors saying "Export-CSV: the process cannot access the file....etc"

Any help to optimise this script and ensure that each result is appended to the CSV file would be appreciated.

A side question, what's the best language to choose for PowerShell in the insert/edit code drop down for readability for future posts?

 

 

$servers = Get-VM | where {$_.powerstate -eq "PoweredOn"} | Select Name
ForEach ($vm in $servers){
$name = $vm.name
$os = Get-View -ViewType VirtualMachine -Filter @{"Name" = $name} | Select-Object @{N="dns";E={$_.Guest.Hostname}}, @{N="OS";E={$_.Guest.GuestFullName}}
if($os.os -eq $null){
write-host "OS is empty from VMtools" -ForegroundColor red
$os = Get-VM $name | Select @{N="dns";E={$_.Name}}, @{N=”OS”;E={$_.ExtensionData.Config.GuestFullname}}
write-host "OS from vm config = $os" -ForegroundColor yellow
$os | Export-CSV "$PSScriptRoot\osResults.csv" -NoTypeInformation -Append
}else{
$os | Export-CSV "$PSScriptRoot\osResults.csv" -NoTypeInformation -Append
}
}

 

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When the script uses the 2nd Export-Csv, the is in use by the 1st Export-Csv.

You could do something like this, which uses the pipeline to pass the data to Export-Csv.

I use 'C#' for the code blocks, by lack of a better option. I just wish they had PS in the list.

Get-View -ViewType VirtualMachine -Filter @{'runtime.powerstate'='poweredon'} -PipelineVariable vm |
ForEach-Object -Process {
    if($vm.Guest.GuestFullName){
        $os = $vm.Guest.GuestFullName
        $dns = $vm.Guest.Hostname
    }
    else{
        $os = $vm.ExtensionData.Config.GuestFullname
        $dns = $vm.Name
    }
    New-Object -TypeName PSObject -Property @{
        DNS = $dns
        OS =$os
    }
} | Export-CSV "$PSScriptRoot\osResults.csv" -NoTypeInformation -Append

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

When the script uses the 2nd Export-Csv, the is in use by the 1st Export-Csv.

You could do something like this, which uses the pipeline to pass the data to Export-Csv.

I use 'C#' for the code blocks, by lack of a better option. I just wish they had PS in the list.

Get-View -ViewType VirtualMachine -Filter @{'runtime.powerstate'='poweredon'} -PipelineVariable vm |
ForEach-Object -Process {
    if($vm.Guest.GuestFullName){
        $os = $vm.Guest.GuestFullName
        $dns = $vm.Guest.Hostname
    }
    else{
        $os = $vm.ExtensionData.Config.GuestFullname
        $dns = $vm.Name
    }
    New-Object -TypeName PSObject -Property @{
        DNS = $dns
        OS =$os
    }
} | Export-CSV "$PSScriptRoot\osResults.csv" -NoTypeInformation -Append

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Hetfield84
Enthusiast
Enthusiast
Jump to solution

Excellent this works perfectly, thanks! Thanks for the heads up about the code blocks too.

Final question, a small set of the results either had a blank host name or a blank OS.Is there a way to put in some error catching to a log so I can see what ones failed and why, so I can resolve them?

Reply
0 Kudos