VMware Cloud Community
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Get-VM with uptime reports incorrect

Hi.

I need some help tweaking my script to report the correct uptime of each VM;

The script:

Connect-VIserver vcenter.blabla.com -User administrator@vsphere.local -Password xxxxx
Get-VM |
select Name, @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}, @{N="FolderName";E={ $_.Folder.Name}},
    @{N='FolderPath';E={
        $path = $_.Name

        $parent = Get-View -Id $_.ExtensionData.Parent
        while($parent){
            $path = "$($parent.Name)\$path"
            if($parent.Parent){
                $parent = Get-View -Id $parent.Parent
            }
            else{
                $parent = $null
            }
        }
        $path}},
    NumCpu, MemoryGB,
    @{N="Up Time";E={$Timespan = New-Timespan -Seconds (Get-Stat -Entity $VM.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value
      "" + $Timespan.Days + " Days, "+ $Timespan.Hours + " Hours, " +$Timespan.Minutes + " Minutes"}},
    Notes
     |
Export-Csv "C:\Reporting\uptimetest_$((Get-Date).ToString("yyyy-MM-dd")).csv" -NoTypeInformation
 
This script runs fine, but the "uptime" column shows the same for all VMs. 
Fnilsen80_0-1656581920231.png

 

Anyone have any good ide?

 

...And yes...I'm new to PS scripting...

0 Kudos
1 Solution

Accepted Solutions
ObjectifDubai
Enthusiast
Enthusiast
Jump to solution

 

Hi,

I think you have to replace $vm.name by $_.name

ObjectifDubai_0-1656583137985.png

 

@{N="Up Time";E={$Timespan = New-Timespan -Seconds (Get-Stat -Entity $_.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value
"" + $Timespan.Days + " Days, "+ $Timespan.Hours + " Hours, " +$Timespan.Minutes + " Minutes"}}, Notes

View solution in original post

2 Replies
ObjectifDubai
Enthusiast
Enthusiast
Jump to solution

 

Hi,

I think you have to replace $vm.name by $_.name

ObjectifDubai_0-1656583137985.png

 

@{N="Up Time";E={$Timespan = New-Timespan -Seconds (Get-Stat -Entity $_.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value
"" + $Timespan.Days + " Days, "+ $Timespan.Hours + " Hours, " +$Timespan.Minutes + " Minutes"}}, Notes
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Worked like a charm!

Thanks!!

0 Kudos