VMware Cloud Community
KTatUC
Contributor
Contributor
Jump to solution

List datastores per host

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!

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

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?

View solution in original post

Reply
0 Kudos
4 Replies
mattboren
Expert
Expert
Jump to solution

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?

Reply
0 Kudos
KTatUC
Contributor
Contributor
Jump to solution

Does this require powercli 6.0 r1 ?

5.8 does not like -PipeLineVariable nor -pv

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

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?

Reply
0 Kudos
KTatUC
Contributor
Contributor
Jump to solution

That got it - Thanks !! Smiley Happy

Reply
0 Kudos