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

Answer accepted by question author

Andreas Baumgarten 132.3K 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

Was this answer helpful?


18 additional answers

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

    yeah the first part does work. But I get the following error.

    Get-VM : Hyper-V was unable to find a virtual machine with name "@{Name=TestName}".
    At line:3 char:13

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

    Was this answer helpful?

    0 comments No comments

  2. Andreas Baumgarten 132.3K Reputation points MVP Volunteer Moderator
    2021-01-15T21:41:59.097+00:00

    If you run the script, what is the content of $computername.
    I don't have a Hyper-V environment available. I couldn't test the script.

    For testing it's possible to run this command as well. Just to see what is the output:

    Get-ClusterGroup -Cluster <name of your hyper-v cluster> | Where {$_.GroupType –eq 'VirtualMachine' } | Get-VM | Select Name
    

    It should be a list of al VM names on the specified Hyper-V cluster.


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

    Regards
    Andreas Baumgarten

    Was this answer helpful?

    0 comments No comments

  3. jeremy smith 41 Reputation points
    2021-01-15T21:24:00.287+00:00

    Hmmm keeps saying these systems can't be found.

    Was this answer helpful?

    0 comments No comments

  4. Andreas Baumgarten 132.3K Reputation points MVP Volunteer Moderator
    2021-01-15T20:33:13.157+00:00

    You can give it a try with modifying the first line of your script (not tested!):

     $computername = Get-ClusterGroup -Cluster <name of your hyper-v cluster> | Where {$_.GroupType –eq 'VirtualMachine' } | Get-VM | Select Name
    
      $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

    Was this answer helpful?

    0 comments No comments

Your answer

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