Automation

 View Only
  • 1.  Get-VM with uptime reports incorrect

    Posted Jun 30, 2022 09:39 AM

    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...



  • 2.  RE: Get-VM with uptime reports incorrect
    Best Answer

    Posted Jun 30, 2022 10:05 AM

     

    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


  • 3.  RE: Get-VM with uptime reports incorrect

    Posted Jun 30, 2022 10:28 AM

    Worked like a charm!

    Thanks!!