9 Replies Latest reply on Jan 11, 2011 6:38 AM by peter79

    Need help with some cluster power shell scripts

    peter79 Enthusiast

      Guys,

       

      I need help with 2 scripts.

       

      Script 1.

      I need a power shell script that will get all the VM's in a particular cluster.  Then output the VM names along with the number of CPU's, allocated RAM and allocated size to  CSV file.

       

      Script 2.

      I need a script that will get all the datastores in a particular cluster and output the datastore name along with the amounf of free space to a CSV file.

       

      Thanks in advance guys.  

        • 1. Re: Need help with some cluster power shell scripts
          mattboren Master
          vExpert

          Greetings, @peter79-

           

          For the first part:

          Get-Cluster myClusterName | Get-VM | Select-Object Name, MemoryMB, NumCpu | Export-Csv -NoTypeInformation c:\temp\myVMInfo.csv
          

           

          And, for the second, how about:

          Get-Cluster myClusterName | Get-VMHost | Get-Random | Get-Datastore | Select-Object name, FreeSpaceMB, CapacityMB | Export-Csv -NoTypeInformation c:\temp\myDaterstoreInfo.csv
          

           

          The first one would be quite a bit faster using Get-View instead of Get-VM.  It would be like:

          ## get an array of VM View objects with just the desired/needed properties
          $arrVMViews = Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster myClusterName).MoRef -Property Config.Hardware,Name
          ## make the two filters for displaying the desired info
          $hshCPUCountFilter = @{n="NumCPUs"; e={$_.Config.Hardware.NumCPU}}
          $hshMemAmtFilter = @{n="MemoryMB"; e={$_.Config.Hardware.MemoryMB}}
          ## display the info, and export to CSV
          $arrVMViews | Select-Object name, $hshCPUCountFilter, $hshMemAmtFilter | Export-Csv -NoTypeInformation c:\temp\myVMInfo.csv
          

           

          Enjoy.

          • 2. Re: Need help with some cluster power shell scripts
            peter79 Enthusiast

            Guys,

             

            I just realised I made a mistake in my above post.  The request for script 2 should read as follows

             

            Script 2

            I need to search for any datastore that has C1 in the title and output there names as well as amount of free disk space left to a CSV file.

             

            Thanks again guys.

            • 3. Re: Need help with some cluster power shell scripts
              mattboren Master
              vExpert

              I missed the "and allocated size" portion of the original post.  Here are some updated ways to get the desired info including the disk size:

              Get-Cluster myClusterName | Get-VM | Select-Object Name, MemoryMB, NumCpu, ProvisionedSpaceGB | Export-Csv -NoTypeInformation c:\temp\myVMInfo.csv
              
              

               

              or

               

              $arrVMViews = Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster myClusterName).MoRef -Property Config.Hardware,Name,Summary.Storage
              $hshCPUCountFilter = @{n="NumCPUs"; e={$_.Config.Hardware.NumCPU}}
              $hshMemAmtFilter = @{n="MemoryMB"; e={$_.Config.Hardware.MemoryMB}}
              $hshDiskAmtFilter = @{n="DiskGB"; e={"{0:n1}" -f (($_.Summary.Storage.Committed + $_.Summary.Storage.Uncommitted) / 1gb)}}
              $arrVMViews | Select-Object name, $hshCPUCountFilter, $hshMemAmtFilter, $hshDiskAmtFilter | Export-Csv -NoTypeInformation c:\temp\myVMInfo.csv
              
              1 person found this helpful
              • 4. Re: Need help with some cluster power shell scripts
                mattboren Master
                vExpert

                Hello-

                 

                Well, that can be easily handled by adding the wildcard that you desire, as such:

                Get-Cluster myClusterName | Get-VMHost | Get-Random | Get-Datastore *c1* | Select-Object name, FreeSpaceMB, CapacityMB | Export-Csv -NoTypeInformation c:\temp\myDaterstoreInfo.csv
                

                 

                Enjoy.

                1 person found this helpful
                • 5. Re: Need help with some cluster power shell scripts
                  peter79 Enthusiast

                  Thanks for your reponse the first script worked nicely but the second doesnt seem to work.

                  • 6. Re: Need help with some cluster power shell scripts
                    mattboren Master
                    vExpert

                    Where is the second one-liner breaking for you?  What is/are the error(s) returned?  And, what version of PowerShell are you using?  (You can grab the PowerShell version from within PowerShell by looking at the "$host" variable or running the "Get-Host" cmdlet).

                    • 7. Re: Need help with some cluster power shell scripts
                      peter79 Enthusiast

                      I was able to figure out the problem thanks for your help I've awarded you some nice points!

                      • 8. Re: Need help with some cluster power shell scripts
                        mattboren Master
                        vExpert

                        Indeed, thx for the pts.  So, curious:  what did you find to be the problem?

                        • 9. Re: Need help with some cluster power shell scripts
                          peter79 Enthusiast

                          I ran the script below and it got what I needed.

                           

                          Get-Datastore *C1*| Select-Object name, FreeSpaceMB, CapacityMB | Export-Csv -NoTypeInformation c:\myDaterstoreInfo.csv