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. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-01-16T08:05:36.163+00:00

    The challenge is "I don't have a Hyper-V cluster for testing before posting" ;-)

    Next try please:

    $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
    }
    

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

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-01-17T01:43:01.19+00:00

    Without the Get-VHD part this should work. The lines are still in the script but not executed.

    $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
    }
    

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

    Regards
    Andreas Baumgarten


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.