VMware Cloud Community
Darryl201110141
Contributor
Contributor
Jump to solution

Get-View question

We have an multi datacenter environment with linked vCenters.  I'm trying to build a PowerCLI script to create a csv report listing all VMs with PowerState, ToolsVersion and ToolsStatus.

I've made it that far (by digging through these forums 😀 ), but I also want to include Datacenter and Cluster for the VMs so the report can be sorted/filtered in a spreadsheet by those values.  I'm a PowerCLI noob so I'm getting a bit lost.

Here's what I have so far... the script does a connect-viserver to each of the vCenters and then runs this and pipes it to Export-CSV:

Get-VM | % { get-view $_.id } | select name, @{Name="PowerState"; Expression={$_.Runtime.PowerState}}, @{Name=“ToolsVersion”; Expression={$_.config.tools.toolsversion}}, @{ Name=“ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}}

 

Any help in getting the datacenter and cluster info in there would be greatly appreciated!

Thanks,
Darryl

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, then try the following when you are connected to all your vCenters.

$global:defaultVIServers |
ForEach-Object -Process {
    $vc = $_
    Get-View -ViewType Datacenter -Server $vc |
    ForEach-Object -Process {
        $dc = $_
        Get-View -ViewType ClusterComputeResource -SearchRoot $dc.MoRef -Server $vc |
        ForEach-Object -Process {
            $cluster = $_
            Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef -Server $vc |
            select Name, 
                @{Name = "PowerState"; Expression = { $_.Runtime.PowerState } }, 
                @{Name = “ToolsVersion”; Expression = { $_.config.tools.toolsversion } }, 
                @{ Name = “ToolStatus”; Expression = { $_.Guest.ToolsVersionStatus } },
                @{N = 'Datacenter'; E = { $dc.Name } },
                @{N = 'Cluster'; E = { $cluster.Name } }
        }
    }
}


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

View solution in original post

16 Replies
LucD
Leadership
Leadership
Jump to solution

Instead of getting the Datacenter and Cluster from the VM, I would suggest looping through Datacenters and Clusters.
With the SearchRoot parameter you can restrict where Get-View looks for objects.

Get-View -ViewType Datacenter -PipelineVariable dc |
ForEach-Object -Process {
    Get-View -ViewType ClusterComputeResource -SearchRoot $dc.MoRef -PipelineVariable cluster |
    ForEach-Object -Process {
        Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef |
        select Name, 
            @{Name="PowerState"; Expression={$_.Runtime.PowerState}}, 
            @{Name=“ToolsVersion”; Expression={$_.config.tools.toolsversion}}, 
            @{ Name=“ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}},
            @{N='Datacenter';E={$dc.Name}},
            @{N='Cluster';E={$cluster.Name}}
    }
}


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

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

Nice!  So then, if I just add a pipe to Export-csv to the end like so, I should be good to go right?

Get-View -ViewType Datacenter -PipelineVariable dc |
ForEach-Object -Process {
    Get-View -ViewType ClusterComputeResource -SearchRoot $dc.MoRef -PipelineVariable cluster |
    ForEach-Object -Process {
        Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef |
        select Name, 
            @{Name="PowerState"; Expression={$_.Runtime.PowerState}}, 
            @{Name=“ToolsVersion”; Expression={$_.config.tools.toolsversion}}, 
            @{ Name=“ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}},
            @{N='Datacenter';E={$dc.Name}},
            @{N='Cluster';E={$cluster.Name}}
    }
} |
Export-Csv -Path "E:\VMware\Reporting\VMTools_$((Get-Date).ToString('dd-MMM-yyyy')).csv"

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct


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

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

Something went wrong.  The output lists many VMs in the wrong datacenter, ie: VM1 in DatacenterA - Cluster1 is showing as VM1 in DatacenterB - Cluster1.  Seems like some data got mixed up in the pipelining.

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I have no idea how this could happen.
The SearchRoot parameter normally limits the search, and via the PipelineVariable you should see the correct Datacenter and Cluster.


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

0 Kudos
wesley_yalipu
Enthusiast
Enthusiast
Jump to solution

Also, I would recommend using the excellent module "ImportExcel" to quickly format your results.
https://www.powershellgallery.com/packages/ImportExcel

Replace "export-csv ...." with "Export-Excel -AutoSize -TableStyle Medium10 -AutoFilter -FreezeTopRow -Show -WorksheetName 'My Report'" for example.

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

It is strange, there is definitely something wrong with the way the nested ForEach loops are saving the results.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try this alternative, without the Pipelinevariable.

Get-View -ViewType Datacenter |
ForEach-Object -Process {
    $dc = $_
    Get-View -ViewType ClusterComputeResource -SearchRoot $dc.MoRef |
    ForEach-Object -Process {
        $cluster = $_
        Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef |
        select Name, 
            @{Name="PowerState"; Expression={$_.Runtime.PowerState}}, 
            @{Name=“ToolsVersion”; Expression={$_.config.tools.toolsversion}}, 
            @{ Name=“ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}},
            @{N='Datacenter';E={$dc.Name}},
            @{N='Cluster';E={$cluster.Name}}
    }
}


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

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

Same thing.  It looks like it's getting the clusters right but mixing up some of the datacenters.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, works for me.
I have no clue why it doesn't work for you.


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

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

No problem, it doesn't make sense to me either.  It should work.

I'll have to do some more experimenting or find another alternate method.

 

Thanks,
Darryl

0 Kudos
LucD
Leadership
Leadership
Jump to solution

One more thing you could check, disconnect from all vCenters.
And then connect to only 1 vCenter


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

0 Kudos
Darryl201110141
Contributor
Contributor
Jump to solution

That just returns the VMs under the vCenter I'm connected to, so only the one datacenter.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, then try the following when you are connected to all your vCenters.

$global:defaultVIServers |
ForEach-Object -Process {
    $vc = $_
    Get-View -ViewType Datacenter -Server $vc |
    ForEach-Object -Process {
        $dc = $_
        Get-View -ViewType ClusterComputeResource -SearchRoot $dc.MoRef -Server $vc |
        ForEach-Object -Process {
            $cluster = $_
            Get-View -ViewType VirtualMachine -SearchRoot $cluster.MoRef -Server $vc |
            select Name, 
                @{Name = "PowerState"; Expression = { $_.Runtime.PowerState } }, 
                @{Name = “ToolsVersion”; Expression = { $_.config.tools.toolsversion } }, 
                @{ Name = “ToolStatus”; Expression = { $_.Guest.ToolsVersionStatus } },
                @{N = 'Datacenter'; E = { $dc.Name } },
                @{N = 'Cluster'; E = { $cluster.Name } }
        }
    }
}


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

Darryl201110141
Contributor
Contributor
Jump to solution

I think we have a winner!!  Did a few spot checks of the results and all looks to be in the right place.

Thanks so much for the help!

 

Regards,
Darryl

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The issue might have been related to your vCenters being in linked-mode.


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

0 Kudos