Search code examples
powershellpowershell-3.0hyper-v

Need help in getting list of IP address with Name, Status, State, OSversion, Nodename against each VM


I need help with script. Below is my script for pulling data from Hyper v nodes. I want some VM attributes as well in output. But I am getting blank. Can someone pls help to understand what is wrong and how to fix this. All the properties which I want are not there in $Vms. How to combine properties from $VirtualM and $VMs and get an output.

$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


Output is as below
=========================

Output Comes Like 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    : 

Solution

  • Update

    Adding a try{}catch{} to see where the object fails, if it does. Back to your question, you can add multiple properties from different variables in to an object (like in the example bellow $VM.Name , $VM.Name and $Node) .

    Selecting before the end results is bad practice. You also do not need to run the same command twice, you can instead "utilize" what you already have ran. If you are making an object, you can put all the collective input in there from different variables and there you can specify what you need. You are also resetting the $Output after every $Node, you want to put the array outside and pump it with objects. I have never used the VM cmdlets, so I just rebuild your script without testing it.

    $Output = @()
    
    foreach($Node in $Nodes) {
        $VMs = Get-VM 
        foreach($VM in $VMs) {
            $VMName = Get-VMNetworkAdapter -VMName $VM.Name
            try{
                $CombineObject= New-Object -type PSObject -Property  @{ 
                    NodeName  = $Node ;
                    VMName    = $VMName.VMName ;
                    IPaddress = $VMName.IPAddresses ;
                    Name      = $VM.Name ;
                    Status    = $VM.Status ;
                    State     = $VM.State
                }
            }catch{
                Write-Output "Error occure with either: $Node or $($VM.Name) or $($VMName.VMName)"
            }
            $Output += $CombineObject
        } 
    } 
    #Display the array of objects
    $Output
    

    Since this is not tested, you should test the two cmdlets separately to make sure you get the right input/output