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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 104K Reputation points MVP
    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 104K Reputation points MVP
    2021-01-16T01:36:42.42+00:00

    Getting closer ;-) 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 -Clusterobject $clusterResource | 
        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
    }
    

    Im not sure if get-VMMemory will work this way. But we will see.


    (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

  2. jeremy smith 41 Reputation points
    2021-01-16T01:44:04.18+00:00

    Not getting VMMemory errors but its definitely having a problem with get-vhd. This same error is repeated for all the VMs

    Get-VHD : Hyper-V was unable to find a virtual machine with id "3ecece8a-b68e-4cfa-a00d-bbf66f21d4c7".
    At line:13 char:14

    • Get-VHD |
    • ~~~~~~~
    • CategoryInfo : ObjectNotFound: (:) [Get-VHD], VirtualizationException
    • FullyQualifiedErrorId : ObjectNotFound,Microsoft.Vhd.PowerShell.Cmdlets.GetVHD

    Cannot index into a null array.
    At line:27 char:18

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

    Cannot index into a null array.
    At line:28 char:18

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

  3. Andreas Baumgarten 104K Reputation points MVP
    2021-01-16T01:59:22.297+00:00

    Next try:

    $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-VHD $vmObject.harddrives.path | 
                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

  4. jeremy smith 41 Reputation points
    2021-01-16T02:11:18.63+00:00

    Negative sir. It gave me the following errors repeated for all the VMs. I'm starting to see I have a long way to go powershell wise.

    Get-VHD : Getting the mounted storage instance for the path 'C:\ClusterStorage\Application\VMTestName\VMTestName.vhd' failed.
    The operation cannot be performed while the object is in use.
    At line:11 char:11

    • $z = Get-VHD $vmObject.harddrives.path |
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : ResourceBusy: (:) [Get-VHD], VirtualizationException
    • FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.GetVHD

    Get-VHD : Getting the mounted storage instance for the path 'C:\ClusterStorage\Application\VMTestName\Data.vhd' failed.
    The operation cannot be performed while the object is in use.
    At line:11 char:11

    • $z = Get-VHD $vmObject.harddrives.path |
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : ResourceBusy: (:) [Get-VHD], VirtualizationException
    • FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.GetVHD

    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.
    At line:27 char:18

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