0

Below is my Script to fetch the information. I am getting OSStatus and uptime as blank in output. Can some one Please help me to get the output using "Get-VM | Select-Object -ExpandProperty NetworkAdapters" and point out what mistake I am making here.

$clus = Read-Host 'Enter the Cluster Name'
$Nodes = Get-clusternode -Cluster $clus
foreach($Node in $Nodes)
          {
$VMs = Get-VM | Select-Object -ExpandProperty NetworkAdapters | Select VMName, IPAddresses, State
    foreach($VM in $VMs)
    {
       $results = [ordered]@{

            'NodeName' = $Node;    
            'VMName' = $VM.VMName; 
            'IPaddress' = $VM.IPAddresses[0];
            'Status' = $VM.State;
            'AdapterStatus' = $VM.Status[0];
            'OSStatus' = $VM.OSStatus;
            'Uptime' = $VM.Uptime
            }
    $Output = New-Object -TypeName PSObject -Property $results
    Write-Output $Output
        } 
    }

I would also like to know if we can have output from separate foreach loops and put it in one array and format it for output at the end of output.

Below is Output I am getting. Blank for OSStatus and Uptime.

NodeName      : ABC
VMName        : System1
IPaddress     : 192.168.1.6
Status        : Running
AdapterStatus : Degraded
OSStatus      : 
Uptime        : 

NodeName      : ABC
VMName        : System2
IPaddress     : 192.168.1.7
Status        : Running
AdapterStatus : Degraded
OSStatus      : 
Uptime        : 

NodeName      : ABC
VMName        : System3
IPaddress     : 192.168.1.8
Status        : Running
AdapterStatus : Degraded
OSStatus      : 
Uptime        : 
0

It seems like some information that you need is in the output of Get-VM and gets lost when you only select the NetworkAdapters Property. That's why I changed it below. it loops over the VM's now and selects the NetworkAdapters Property during the loop.

I also:

  • Put everything in a PSCustomObject directly, so no need for New-Object or [ordered]
  • Put the $results Variable before the foreach loop
  • Added | ft -Autosize to your Write-Output so it formats it the way you want to (if you want to use the output of the script further in your script, remove | ft -autosize again, it's only good for cosmetic reasons)
  • Specified the ClusterObject in your Get-VM - I'm not so sure about that since I don't work with clusters, but I think you need to specify this information, otherwise it would only read local VM's, never from a remote cluster.
  • There was no Property for OSStatus. I think you meant OperationalStatus. I changed it to that.

Here's the code

$clus = Read-Host 'Enter the Cluster Name'
$Nodes = Get-clusternode -Cluster $clus
$results = @()
foreach($Node in $Nodes)
{
    $VMs = Get-VM -ClusterObject $Node
    foreach($VM in $VMs)
    {
        $VMtmp = $VM | select -expand NetworkAdapters
        $results += [PSCustomObject]@{
            'NodeName' = $Node;    
            'VMName' = $VM.VMName; 
            'IPaddress' = $VMtmp.IPAddresses[0];
            'Status' = $VM.State;
            'AdapterStatus' = $VMtmp.Status[0];
            'OSStatus' = $VM.OperationalStatus[0];
            'Uptime' = $VM.Uptime
        }
    } 
}
Write-Output $results | ft -AutoSize
  • Thanks a lot Simon. It works and clears my doubt as well. – CuriousPiyush Sep 25 '18 at 10:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.