I want to query each ESXi host for datastore info and return to a csv the following:
ESXi host name, datastore name, datastore state
I started with:
Get-vmhost | Get-Datastore | Export-Csv C:\script\reports\test.csv -NoTypeInformation
Looks like I need to do a for each maybe ???
Any help would be appreciated!
Hello, KTatUC-
Of course there are plenty of ways to do things -- a ForEach-Object would do. Another way, taking advantage of the -PipelineVariable parameter in PowerShell v4, would be like:
Get-VMHost -PipelineVariable oThisVMHost | Get-Datastore | Select-Object @{n="VMHost"; e={$oThisVMHost}},Name,State
That then uses a calculated property to add VMHost to the selected output. You could then pipe that to Export-Csv or do as you like. How's that do?
Hello, KTatUC-
Of course there are plenty of ways to do things -- a ForEach-Object would do. Another way, taking advantage of the -PipelineVariable parameter in PowerShell v4, would be like:
Get-VMHost -PipelineVariable oThisVMHost | Get-Datastore | Select-Object @{n="VMHost"; e={$oThisVMHost}},Name,State
That then uses a calculated property to add VMHost to the selected output. You could then pipe that to Export-Csv or do as you like. How's that do?
Does this require powercli 6.0 r1 ?
5.8 does not like -PipeLineVariable nor -pv
Hello-
No, it does not. Again, that -PipelineVariable is a feature of PowerShell v4 (not of a particular PowerShell Snapin or Module version).
So, it sounds like you are using an older version of PowerShell on your machine. Another way to do it, that should run the same in all versions of PowerShell, would be:
Get-VMHost | Foreach-Object {$oThisVMHost = $_; $_ | Get-Datastore | Select-Object @{n="VMHost"; e={$oThisVMHost}},Name,State}
That do better?
That got it - Thanks !!