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. jeremy smith 41 Reputation points
    2021-01-15T22:39:51.497+00:00

    I could post all the errors but as there are several VM on this cluster it'll take a bit to clear the VM name from the errors. Let me know if it'll help. It might take a bit though.

    0 comments No comments

  2. jeremy smith 41 Reputation points
    2021-01-15T22:44:08.213+00:00

    actually here is the full error. It just repeats the errors for each VM.

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

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

    get-VMMemory : Hyper-V was unable to find a virtual machine with name "TestVMName".
    At line:6 char:7

    • $y = get-VMMemory $computername
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : ObjectNotFound: (:) [Get-VMMemory], VirtualizationException
    • FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.GetVMMemory

    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) [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.
    At line:27 char:14

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

  3. Andreas Baumgarten 104K Reputation points MVP
    2021-01-15T22:53:09.253+00:00

    In the script you posted in your question:
    What did you enter instead of "whatever" to get the script running successfully?

    The first error message shows Get-VM "TestVMName". What is the exact name of the VM? Is the name TestVM or TestVMName?


    Kind regards
    Andreas Baumgarten

    0 comments No comments

  4. jeremy smith 41 Reputation points
    2021-01-15T23:08:13.403+00:00

    Just the actual VMs name. The name is actually completely different I changed the names in the errors to keep from putting actual VM objects names in the post.