Hi, need some help here; we have 3 different DataCenters with different cluster names within each. However, due to some DR requirements, duplicate vm names exist within the separate Datacenters/Clusters. When we run some scripts, I need to make sure we are working with the VMs in the correct Datacenter/cluster. I'm trying this code but not getting the Cluster name that the VM is in. What am I missing?
Get-View -ViewType "VirtualMachine" -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Where-Object {($_.Config.GuestFullName -ne $_.Guest.GuestFullName) -and ($_.Guest.GuestFullName -ne $null) -and ($_.Guest.PowerState -ne "PoweredOn")} | Select-Object -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.GuestFullName}}, @{N="Cluster Name";E={$_.Host.Parent.Name}} | Format-Table -Autosize
Thanks for the assistance!
You can find the clustername by folowing the Parent-trail
Get-View -ViewType "VirtualMachine" -Property "Name","Config.GuestFullName","Guest.GuestFullName","Runtime.Host" | Where-Object {($_.Config.GuestFullName -ne $_.Guest.GuestFullName) -and ($_.Guest.GuestFullName -ne $null) -and ($_.Guest.PowerState -ne "PoweredOn")} | Select-Object -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.GuestFullName}}, @{N="Cluster Name";E={ $parent = Get-View $_.Runtime.Host -Property Parent,Name while($parent -isnot [VMware.Vim.ClusterComputeResource]){ $parent = Get-View $parent.Parent -Property Parent,Name } $Parent.Name }} | ft -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Welcome to the VMware VMTN Communities.
The object that the Get-View cmdlet returns doesn't have a Host property. That is why you can't find the cluster on which the VM is running. You better use the Get-VM cmdlet that does return the Host property instead of using the Get-View cmdlet.
Something like:
Get-VM | ` Where-Object {($_.ExtensionData.Config.GuestFullName -ne $_.Guest.OSFullName) -and ($_.Guest.OSFullName -ne $null) -and ($_.PowerState -ne "PoweredOn")} | ` Select-Object -Property Name, @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.OSFullName}}, @{N="Cluster Name";E={$_.VMHost.Parent.Name}} |` Format-Table -Autosize
Regards, Robert
Thanks for the response Robert!
When I ran that, it only gave me results from one Datacenter not all 3? Also, took forever compared to get-view.
If we can keep the speed up, i'm not "married" to the one liner approach.
You can find the clustername by folowing the Parent-trail
Get-View -ViewType "VirtualMachine" -Property "Name","Config.GuestFullName","Guest.GuestFullName","Runtime.Host" | Where-Object {($_.Config.GuestFullName -ne $_.Guest.GuestFullName) -and ($_.Guest.GuestFullName -ne $null) -and ($_.Guest.PowerState -ne "PoweredOn")} | Select-Object -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.GuestFullName}}, @{N="Cluster Name";E={ $parent = Get-View $_.Runtime.Host -Property Parent,Name while($parent -isnot [VMware.Vim.ClusterComputeResource]){ $parent = Get-View $parent.Parent -Property Parent,Name } $Parent.Name }} | ft -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nice LucD! Thank you!
Can you show me how to add a column with the VMguests resource pool folder location? (full path)?
Sure, same method, follow the ResourcePool parent path.
Note that I excluded the hidden folders 'host', 'Resources' and 'Datacenters' from the path.
Get-View -ViewType "VirtualMachine" -Property "Name","Config.GuestFullName","Guest.GuestFullName","Runtime.Host","ResourcePool" | Where-Object {($_.Config.GuestFullName -ne $_.Guest.GuestFullName) -and ($_.Guest.GuestFullName -ne $null) -and ($_.Guest.PowerState -ne "PoweredOn")} | Select-Object -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.GuestFullName}}, @{N="Cluster Name";E={ $parent = Get-View $_.Runtime.Host -Property Parent,Name while($parent -isnot [VMware.Vim.ClusterComputeResource]){ $parent = Get-View $parent.Parent -Property Parent,Name } $Parent.Name }}, @{N="Resourcepool";E={ $parent = Get-View $_.ResourcePool -Property Parent,Name $path = $parent.Name while($parent.Parent){ $parent = Get-View $parent.Parent -Property Parent,Name if("Datacenters","host","Resources" -notcontains $parent.Name){ $path = $Parent.Name + "/" + $path
} } $path
}}
I left out Format-Table -AutoSize, I'm not sure it will fit on 1 line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Just getting back to this, ResourcePool column is empty, hmm. I'll have to play around with it...
Are the VMs perhaps in the hidden folder ?
Try changing these lines
if("Datacenters","host","Resources" -notcontains $parent.Name){ $path = $Parent.Name + "/" + $path
}
into this
$path = $Parent.Name + "/" + $path
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Still empty; when I run this command "get-vm vmname | select -Property ResourcePool |ft -a" I see the ResourcePool that the vm is in.
Strange, that seems to be working for me.
I'll do some more tests.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
'Spacin' 2day I guess
Once I added this ,"ResourcePool" to the initial Get-View line, all good!
Thanks LucD!
hi
urgent help need!!!
im use this one-liner to get report for VMtools status & Power State
get-vm | % { get-view $_.ID } | select Name, @{name="PowerState"; Expression={$_.powerstate}}, @{ Name="ToolsStatus"; Expression={$_.guest.toolsstatus}},
i do somthing wrong because under the power state expression i get blank
please HELP
Try it like this:
get-vm | select Name,PowerState,@{ Name="ToolsStatus"; Expression={$_.ExtensionData.guest.toolsstatus}}
Message was edited by: RvdNieuwendijk
great Rob
many many many thanks
one More Q
i export that report to CSV and want to mail it as attachment
get-vm test | select Name, @{name="PowerState"; Expression={$_.powerstate}}, @{ Name="ToolsStatus"; Expression={$_.ExtensionData.guest.toolsstatus}} | sort-object name | Export-Csv -NoTypeInformation -UseCulture -Path "E:\reports\PowerCli\VMtools Status.csv" | Send-MailMessage -SmtpServer IP -Attachments $att = new-object Net.Mail.Attachment("E:\reports\PowerCli\VMtools Status.csv") -From PowerCli@XXXX.com -Subject "VMtools Status" -to liavk@XXXX.com -body "created by PowerCli"
i get the email but without attachd file
any ideas?
Just specify the path to the file you want to attach:
get-vm test | select Name,PowerState, @{ Name="ToolsStatus"; Expression={$_.ExtensionData.guest.toolsstatus}} | sort-object name | Export-Csv -NoTypeInformation -UseCulture -Path "E:\reports\PowerCli\VMtools Status.csv" Send-MailMessage -SmtpServer IP -Attachments "E:\reports\PowerCli\VMtools Status.csv" ` -From PowerCli@XXXX.com -Subject "VMtools Status" -to liavk@XXXX.com -body "created by PowerCli"
ok i find a solution
get-vm test | select Name, @{name="PowerState"; Expression={$_.powerstate}}, @{ Name="ToolsStatus"; Expression={$_.ExtensionData.guest.toolsstatus}} | sort-object name | Export-Csv -NoTypeInformation -UseCulture -Path "E:\reports\PowerCli\VMtools Status.csv" |
Send-MailMessage -SmtpServer IP -Attachments $att = new-object Net.Mail.Attachment("E:\reports\PowerCli\VMtools Status.csv") -From PowerCli@XXXX.com -Subject "VMtools Status" -to liavk@XXXX.com -body "created by PowerCli"
just remove the pipeline and saperate the tasks