4 Replies Latest reply on Jul 28, 2015 6:37 AM by KTatUC

    List datastores per host

    KTatUC Lurker

      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!

        • 1. Re: List datastores per host
          mattboren Master
          vExpert

          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?

          • 2. Re: List datastores per host
            KTatUC Lurker

            Does this require powercli 6.0 r1 ?

             

            5.8 does not like -PipeLineVariable nor -pv

            • 3. Re: List datastores per host
              mattboren Master
              vExpert

              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?

              • 4. Re: List datastores per host
                KTatUC Lurker

                That got it - Thanks !!