How to Retrieve VMs details from multiple Clusters

Hello there,

Thank you for the time and effort you have given to build the script which retrives VM\s details from multiple hypervisor on the Network.

I would like to use the same script this time which is at the bottom of this page: Retrieve details of vms on each and every host server on the network …but to retrieve the data from several clusters.

I have combined it with another script which I found on Nested Foreach in PowerShell to get all the VMs from another cluster, but this considers each cluster listed with get-cluster as a node and note as cluster.

Here is the code:

#https://www.powershellgallery.com/packages/ImportExcel/7.8.0
Install-Module -Name ImportExcel

#Assign the username to the viariable.
$Identifiant = 'Boulbul'

#Assign the computer names to the viriable.


$ComputerName = get-Cluster *


$Result =
Invoke-Command -ComputerName $ComputerName -HideComputerName -Credential $Identifiant -ScriptBlock {
    $HostOS = Get-CimInstance -ClassName CIM_OperatingSystem
    Get-VM | 
    ForEach-Object {
        $VM = $_  
        $NicList = $VM.NetworkAdapters
        $VM_HD = $VM.VMId  | Get-VHD
        foreach ($Nic in $NicList) {
            [PSCustomObject]@{
                HostComputerName  = $ENV:COMPUTERNAME
                Host_OS           = $HostOS.caption
                Nic_IPAddressesV4 = $Nic.IPAddresses[0]
                Nic_IPAddressesV6 = $Nic.IPAddresses[1]
                VM_Name           = $VM.Name
                Nic_MacAddress    = $Nic.MacAddress
                VM_CreationTime   = $VM.CreationTime
                VM_MemoryAssigned = ($VM.MemoryAssigned / 1GB)
                VM_ID             = $VM.VMId
                VM_HD_Size        = $(foreach ($HD in $VM_HD) { ($HD.Size / 1GB) } ) -join ' || '
                VM_HD_Location    = ($VM_HD.Path) -join ' || '
            }
        }
    } 
} |
Select HostComputerName, Host_OS, Nic_IPAddressesV4,Nic_IPAddressesV6, VM_Name, Nic_MacAddress, VM_CreationTime, VM_MemoryAssigned, VM_ID, VM_HD_Size, VM_HD_Location

#export the result to an excel file.
$Result  |  Export-Excel -Path C:\temp\allVM.xls

Please note that Get-Cluster * lists all the cluters that are on the domain…Here is the output:

Name             
----             
Cluster1 
Cluster2
Cluster3
Cluster4

What would be the best aproach to list all the VMs that exsit within all clusters listed above using the same script above too please?

Thank you in advance.

Kind regards,

Boulbul

Hmmm … I’m not working that much with Hyper-V-Clusters but are you sure you’re using the proper commands? Since Hyper-V is not the only type of clusters you can run on Windows servers there might be another way …

I googled a little bit and found some search hits you may use as inspiration:

Regardless of that …

I’d expect that you should know the Hyper-V clusters you have. So you should be able to provide them instead of querying them. :wink:

Olaf,

Thank you for getting back to me.

Thanks to your help and that of other posts, I have managed to retrieve all VMs from clusters. If the code is run over all clusters, this would take a long, long time to finnish and sometimes it may lead to some confusing errors.

So, the solution I found is to run the script over one cluster at the time. To automate this process, I have created a menu for user intervention.

No, I did not :slight_smile:…so I did the following to find all the hypervisors/nodes within the clusters by doing finding all clusters within the domain with Get-Clusters * then for each cluster found you find the nodes with Get-ClusterNode -Name ClusterName | select name

Here is the code:

#This script aims to retrieve inventory of all VMs and associated data from Hypervisor servers and clusters.
#The data will be exported to a file on the temporary folder from which the script is performed.

#Ce script vise à récupérer l'inventaire de toutes les machines virtuelles et des données associées à partir du serveur Hyperv et des clusters. 
#Les données seront exportées vers un fichier sur le dossier temporaire à partir duquel le script est exécuté.



# Install ImportExcel to export output to an XL file format.
Install-Module -Name ImportExcel



#Select Hypervisors '  nodes or cluster for inventory retrieval.

        #0- Hypervisors' Servers
            $SRV1 = ‘VM1-1’,‘VM1-1’

        #1- Nodes on Clusters  Cluster
            $SRV2 = ‘VM2-2’,‘VM2-2’

        


#Set $ComputerName (Nodes), $Title (file name for data export


$Today = Get-Date -Format 'yyyy-MM-dd-hh-mm'



#$Subject = Read-Host -Prompt "Veuillez sélectionner votre liste de nœuds ou votre cluster pour l'inventaire"

#Choose inventory from Menu.

$continue = $true
while ($continue){
  write-host “ ”
  write-host “ --------------Import Inventory----------------”
  write-host “ "
  write-host “ "
  write-host “ Please select a number from the following:"
  write-host “ "
  write-host “0. SRV1          | SRV2”
  write-host " "
  write-host "            x. pour EXIT "
  write-host "------------------------------------------------"
  $choix = read-host “Please Choose :”
  $Subject = " "
  $ComputerName = " "
  switch ($choix){
    0{$ComputerName =  $SRV1;         $Title = ' SRV1-' + $Today ;          $continue = $false}
    1{$ComputerName =  $SRV1;         $Title = ' SRV2-' + $Today ;          $continue = $false}
    ‘x’ {$continue = $false}
    default {Write-Host "Choix invalide"-ForegroundColor Red}
  }
}



#Set your usersname.
$Identifiant =  $cred = Get-Credential 


#Retrieve Data from nodes.
$Result =
Invoke-Command -ComputerName $ComputerName -HideComputerName -Credential $Identifiant -ScriptBlock {
 
    
    $HostOS = Get-CimInstance -ClassName CIM_OperatingSystem
    Get-VM | 
    ForEach-Object {
        $VM = $_  
        $NicList = $VM.NetworkAdapters
        $VM_HD = $VM.VMId  | Get-VHD
        foreach ($Nic in $NicList) {
            [PSCustomObject]@{
                HostComputerName  = $ENV:COMPUTERNAME
                Host_OS           = $HostOS.caption
                Nic_IPAddressesV4 = $Nic.IPAddresses[0]
                Nic_IPAddressesV6 = $Nic.IPAddresses[1]
                VM_Name           = $VM.Name
                Nic_MacAddress    = $Nic.MacAddress
                VM_CreationTime   = $VM.CreationTime
                VM_MemoryAssigned = ($VM.MemoryAssigned / 1GB)
                VM_ID             = $VM.VMId
                VM_HD_Size        = $(foreach ($HD in $VM_HD) { ($HD.Size / 1GB) } ) -join ' || '
                VM_HD_Location    = ($VM_HD.Path) -join ' || '
            }
        }
    } 
} | Select HostComputerName, Host_OS, Nic_IPAddressesV4,Nic_IPAddressesV6, VM_Name, Nic_MacAddress, VM_CreationTime, VM_MemoryAssigned, VM_ID, VM_HD_Size, VM_HD_Location

#export the result to an excel file.
$Result  |  Export-Excel -Path C:\temp\allVM.xls -WorksheetName $Title 




Having said that, I still get errors which relate to corrupt VMs and other things…is there anyway to redirect these errors to the existing XL file, in a worksheet called ERRORs please?

Many thanks in advance.

Kind regards,

Boulbul

Either you use conditions where they fit and where you actually expect to get errors or you use the general error handling method of PowerShell