locked
Need help on fetching NodeName, IP address, VMName, OS Status, VM OS Version across all nodes in HyperV Cluster RRS feed

  • Question

  • I am new to PowerShell and need help. The Script and Output are pasted below. Get-VMNetworkAdapter has IP address as property. But if I use this I loose other properties of Get-VM.

    How can I have different set of properties in one array and write that to output. See the Output below as per my script, I am getting Blank. Can some body please let me know what I am doing wrong and best way to rectify this.

    $Nodes = Get-Content -Path "C:\Nodes.txt"
    
    foreach($Node in $Nodes)
              {
    
     $VMs = Get-VM | Get-VMNetworkAdapter | Select-Object -Property VMName, IPAddresses
     $VirtualM = Get-VM | Select Name, Status, State
     $Output = @()
    
     foreach($VM in $VMs)
        {
       $results = [ordered]@{
    
            'NodeName' = $Node;    
            'VMName' = $VM.VMName; 
            'IPaddress' = $VM.IPAddresses[0];
            }
    $Output += New-Object -TypeName PSObject -Property $results
    $Output += New-Object -TypeName PSObject -Property $VirtualM
        } 
    } Write-Output $Output | Select-Object -Property NodeName, VMName, IPAddress, State, Status
    

    I am getting Output as below

    NodeName  : ABC
    VMName    : s1
    IPaddress : 192.168.1.5
    State     : 
    Status    : 
    
    NodeName  : ABC
    VMName    : s2
    IPaddress : 192.168.1.6
    State     : 
    Status    : 
    
    NodeName  : ABC
    VMName    : s3
    IPaddress : 192.168.1.7
    State     : 
    Status    : 
    
    NodeName  : DEF
    VMName    : D1
    IPaddress : 192.168.1.9
    State     : 
    Status    : 
    
    NodeName  : DEF
    VMName    : D2
    IPaddress : 192.168.1.10
    State     : 
    Status    :

    Saturday, September 22, 2018 4:31 PM

Answers

  • $nodes = Get-Content C:\Nodes.txt
    $output = foreach($node in $nodes){
    
        $VMs = Get-Cluster $node| Get-VM 
        foreach($VM in $VMs){
            $vmadapter = Get-VMNetworkAdapter $VM
            [pscustomobject]@{
                NodeName = $node  
                VMName = $VM.VMName
                Status = $VM.Status
                State = $VM.State
                IPaddress = $vmadapter.IPAddresses[0]
            }
        }
    }
    
    $uutput | Select-Object NodeName, VMName, IPAddress, State, Status
    

    You need to start here:

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    • Marked as answer by CuriousPiyush Sunday, September 23, 2018 10:21 AM
    Sunday, September 23, 2018 12:06 AM

All replies

  • $nodes = Get-Content C:\Nodes.txt
    $output = foreach($node in $nodes){
    
        $VMs = Get-Cluster $node| Get-VM 
        foreach($VM in $VMs){
            $vmadapter = Get-VMNetworkAdapter $VM
            [pscustomobject]@{
                NodeName = $node  
                VMName = $VM.VMName
                Status = $VM.Status
                State = $VM.State
                IPaddress = $vmadapter.IPAddresses[0]
            }
        }
    }
    
    $uutput | Select-Object NodeName, VMName, IPAddress, State, Status
    

    You need to start here:

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    • Marked as answer by CuriousPiyush Sunday, September 23, 2018 10:21 AM
    Sunday, September 23, 2018 12:06 AM
  • Thanks a lot for your reply. You made my day. I have some queries to understand it better logically.

    Please help me if I want to achieve the same result of IP address using

    Get-VM $Node | Select -ExpandProperty NetworkAdapters | Select IPaddresses

    how will I go for it in this script. I need all the properties as is but want to pull IP address using above command. I will not be able to run it inside second foreach loop as Get-VM will work on $Node. I want to understand how we can merge different properties to one array(if they are from separate foreach loops) to get desired output, if that is possible.

    Sunday, September 23, 2018 10:21 AM