Powershell script to pull information from all VMs in a failovercluster

jeremy smith 41 Reputation points
2021-01-15T19:35:14.587+00:00

I was previously helped creating a powershell script that pulls info from specific named VMs on a host. Now I just need to get it to do this by cluster so the excel document lists all VMs on the cluster with the specified information. I'm still rather new to powershell thank for any help you can offer. The current script is below.

$computername = "whatever"

 $x = Get-VM $computername |
     Select-Object Name,MemoryStartup,MemoryMinimum,MemoryMaximum,DynamicMemoryEnabled,ProcessorCount

 $y = get-VMMemory $computername
 $x | Add-Member NoteProperty buffer $y.buffer
 $x | Add-Member NoteProperty priority $y.priority

 $z = Get-VM $computername | 
     Select-Object vmid | 
         Get-VHD | 
             Select-Object path,size
 $GreatestNumberOfPaths = 0
 $z |
     Foreach-Object{
         if($_.Path.size -gt $GreatestNumberOfPaths){
             $GreatestNumberOfPaths = $_.Path.Length
         }
     }
 $GreatestNumberOfPaths -= 1

 $z.Path |
     ForEach-Object {$i=0}{
         if ($GreatestNumberOfPaths -le $i){
             $x | Add-Member NoteProperty ("Path[{0}]" -f ($i+1)) $z.Path[$i]
             $x | Add-Member NoteProperty ("Size[{0}]" -f ($i+1)) $z.Size[$i]
         }
         Else{
             $x | Add-Member NoteProperty ("Path[{0}]" -f ($1+1)) ""
             $x | Add-Member NoteProperty ("Size[{0}]" -f ($1+1)) ""
         }
         $i++
     }

 $x | Export-CSV c:\temp\VMInfo.csv -NoTypeInformation
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-01-17T09:36:37.813+00:00

    Please check:
    $clusterResources contains all VMs?
    $x is created and contains details for each VM?

    If both is yes than $x needs to be added to the csv file in every for-each loop.

    Please try this:

    $clusterResources = Get-ClusterResource -Cluster <ClusterName> | Where ResourceType -eq "Virtual Machine"
    foreach ($clusterResource in $clusterResources) {           
    $x = Get-VM -Clusterobject $clusterResource |
        Select-Object Name,MemoryStartup,MemoryMinimum,MemoryMaximum,DynamicMemoryEnabled,ProcessorCount
    
    $vmObject = Get-VM -Clusterobject $clusterResource
    $y = get-VMMemory -VM $vmObject
    $x | Add-Member NoteProperty buffer $y.buffer
    $x | Add-Member NoteProperty priority $y.priority
    
    <#   
    $z = Get-VM -Computername (Get-ClusterNode) | where {$_.VMId -eq $vmObject.VMId} | 
        Get-VHD -VMId $_.VMId -ComputerName $_.ComputerName | 
                Select-Object path,size
    $GreatestNumberOfPaths = 0
    
    $z |
        Foreach-Object{
            if($_.Path.size -gt $GreatestNumberOfPaths){
                $GreatestNumberOfPaths = $_.Path.Length
            }
        }
    $GreatestNumberOfPaths -= 1
    
    $z.Path |
        ForEach-Object {$i=0}{
            if ($GreatestNumberOfPaths -le $i){
                $x | Add-Member NoteProperty ("Path[{0}]" -f ($i+1)) $z.Path[$i]
                $x | Add-Member NoteProperty ("Size[{0}]" -f ($i+1)) $z.Size[$i]
            }
            Else{
                $x | Add-Member NoteProperty ("Path[{0}]" -f ($1+1)) ""
                $x | Add-Member NoteProperty ("Size[{0}]" -f ($1+1)) ""
            }
            $i++
        }
     #>              
    $x | Export-CSV c:\temp\VMInfo.csv -NoTypeInformation -Append
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


18 additional answers

Sort by: Most helpful
  1. jeremy smith 41 Reputation points
    2021-01-15T21:48:52.853+00:00

    I also checked the variable $computername and it is listing all the VMs on the cluster.

    0 comments No comments

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-01-15T21:57:32.643+00:00

    Could you please give this a try:

    $computernames = Get-ClusterGroup -Cluster <name of your hyper-v cluster> | Where {$_.GroupType –eq 'VirtualMachine' } | Get-VM | Select -ExpandProperty Name
    foreach ($computername in $computernames) {          
    $x = Get-VM $computername |
        Select-Object Name,MemoryStartup,MemoryMinimum,MemoryMaximum,DynamicMemoryEnabled,ProcessorCount
    
    $y = get-VMMemory $computername
    $x | Add-Member NoteProperty buffer $y.buffer
    $x | Add-Member NoteProperty priority $y.priority
    
    $z = Get-VM $computername | 
        Select-Object vmid | 
            Get-VHD | 
                Select-Object path,size
    $GreatestNumberOfPaths = 0
    $z |
        Foreach-Object{
            if($_.Path.size -gt $GreatestNumberOfPaths){
                $GreatestNumberOfPaths = $_.Path.Length
            }
        }
    $GreatestNumberOfPaths -= 1
    
    $z.Path |
        ForEach-Object {$i=0}{
            if ($GreatestNumberOfPaths -le $i){
                $x | Add-Member NoteProperty ("Path[{0}]" -f ($i+1)) $z.Path[$i]
                $x | Add-Member NoteProperty ("Size[{0}]" -f ($i+1)) $z.Size[$i]
            }
            Else{
                $x | Add-Member NoteProperty ("Path[{0}]" -f ($1+1)) ""
                $x | Add-Member NoteProperty ("Size[{0}]" -f ($1+1)) ""
            }
            $i++
        }
    
    $x | Export-CSV c:\temp\VMInfo.csv -NoTypeInformation
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. jeremy smith 41 Reputation points
    2021-01-15T22:14:50.337+00:00

    I get a few of the same errors on line 3 but then errors at lines 6,10,26 and 27.

    At line:10 char:11

    • $z = Get-VM $computername |
    • ~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (@{Name=TestVMName}:String) [Get-VM], VirtualizationException
    • FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.GetVM

    Cannot index into a null array.


    At line:26 char:18

    • ... $x | Add-Member NoteProperty ("Path[{0}]" -f ($i+1)) $z.P ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : NullArray

    Cannot index into a null array.


  4. jeremy smith 41 Reputation points
    2021-01-15T22:33:29.213+00:00

    No worries thank you so much for the help. Looks like we are still getting the previous errors but some of the errors did change up a bit.

    Get-VM : Hyper-V was unable to find a virtual machine with name "TestVMName".
    At line:10 char:7

    • $z = Get-VM $computername |
    • ~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (TestVMName:String) [Get-VM], VirtualizationException
    • FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.GetVM

    Cannot index into a null array.
    At line:26 char:14

    • ... $x | Add-Member NoteProperty ("Path[{0}]" -f ($i+1)) $z.P ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : NullArray

    Cannot index into a null array.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.