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
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
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
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"
Correct
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
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.
It is strange, there is definitely something wrong with the way the nested ForEach loops are saving the results.
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
Same thing. It looks like it's getting the clusters right but mixing up some of the datacenters.
Sorry, works for me.
I have no clue why it doesn't work for you.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
That just returns the VMs under the vCenter I'm connected to, so only the one datacenter.
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
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
The issue might have been related to your vCenters being in linked-mode.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference