VMware Cloud Community
The0ray
Contributor
Contributor
Jump to solution

one-liner get running OS & configured OS & Cluster name

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!

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
16 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

Nice LucD! Thank you!

Can you show me how to add a column with the VMguests resource pool folder location? (full path)?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

Just getting back to this, ResourcePool column is empty, hmm. I'll have to play around with it...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

The0ray
Contributor
Contributor
Jump to solution

Still empty; when I run this command "get-vm vmname | select -Property ResourcePool |ft -a" I see the ResourcePool that the vm is in.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, that seems to be working for me.

I'll do some more tests.


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

Reply
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

'Spacin' 2day I guess Smiley Happy

Once I added this ,"ResourcePool" to the initial Get-View line, all good!

Thanks LucD!

Reply
0 Kudos
lkovalio
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Try it like this:

get-vm | select Name,PowerState,@{ Name="ToolsStatus"; Expression={$_.ExtensionData.guest.toolsstatus}}

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
lkovalio
Contributor
Contributor
Jump to solution

great Rob

many many many thanks

Reply
0 Kudos
lkovalio
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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"

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
lkovalio
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos