VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

uptime of vms

Hi,

i went tho many notes. like

Get-VM -location xxx | `

  where {$_.PowerState -eq "PoweredOn"} | `

  Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `

  select Entity,

         @{Name="Boottime";

          Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}} |export-csv c:\temp\sf-pete-vm-uptime.csv

$report = @()
Get-VM -location xxx | Get-View | %{
$row = "" | select Name, PowerState, LastBoot, LastSuspend, TotalSuspend
$row.Name = $_.Name
$row.PowerState = $_.runtime.powerState
$row.LastBoot = $_.runtime.bootTime
$row.LastSuspend = $_.runtime.suspendTime
$row.TotalSuspend = $_.runtime.suspendInterval
$report += $row
}
$report

But they do not work ...

Anyone has a final & working ,  script for getting the uptime of vm ?

thanks

1 Solution

Accepted Solutions
kunaludapi
Expert
Expert
Jump to solution

You can also use something like below, Requirement is VMware tools should be present on requested VMs, You will require same username password for windows and have permissions also on Linux root or anyother username password should be same., It will directly pull Uptime/boottime from inside vm.

$report = @()

Get-VM | Where-Object {$_.powerstate -eq "PoweredOn"} | ForEach-Object {

    $os = $_.extensiondata.Guest.GuestFullName

    if ($os -match "Windows") {

        $test = Invoke-VMScript -VM $_ -ScriptText "(Systeminfo)[11]" -GuestUser Domain\username -GuestPassword password

        $test = $test.ScriptOutput -replace ("System Boot Time:          ", "")

    }

    else {

        $test = Invoke-VMScript -VM $_ -ScriptText "uptime" -GuestUser Root -GuestPassword Password

    }

    $row = "" | select Name, Boottime

    $row.Name = $_.Name

    $row.Boottime = $test

    $report += $row

}

$report

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

View solution in original post

12 Replies
kunaludapi
Expert
Expert
Jump to solution

You can also use something like below, Requirement is VMware tools should be present on requested VMs, You will require same username password for windows and have permissions also on Linux root or anyother username password should be same., It will directly pull Uptime/boottime from inside vm.

$report = @()

Get-VM | Where-Object {$_.powerstate -eq "PoweredOn"} | ForEach-Object {

    $os = $_.extensiondata.Guest.GuestFullName

    if ($os -match "Windows") {

        $test = Invoke-VMScript -VM $_ -ScriptText "(Systeminfo)[11]" -GuestUser Domain\username -GuestPassword password

        $test = $test.ScriptOutput -replace ("System Boot Time:          ", "")

    }

    else {

        $test = Invoke-VMScript -VM $_ -ScriptText "uptime" -GuestUser Root -GuestPassword Password

    }

    $row = "" | select Name, Boottime

    $row.Name = $_.Name

    $row.Boottime = $test

    $report += $row

}

$report

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
esxi1979
Expert
Expert
Jump to solution

Thanks, This looks is the ONLY  way .. ie directly talking to the windows OS with Invoke-VMScript

Surprisingly even with vmware tools installed no way to get accurate info with promising commands (only on paper Smiley Wink)  Get-Stat -Stat sys.uptime.latest..

Reply
0 Kudos
esxi1979
Expert
Expert
Jump to solution

Thanks, How do it get this in export-csv Please ?

Reply
0 Kudos
kunaludapi
Expert
Expert
Jump to solution

on the last line replace $report with below line.

$report | Export-Csv -NoTypeInformation -Path c:\temp\info.csv

Apart from your can pull information on Windows servers using wmi as well,

ie:

Get-WmiObject -class win32_operatingsystem -ComputerName Localhost | Select-Object CSName, @{N="uptime"; E={$_.converttodatetime($_.LastBootUpTime)}}

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
esxi1979
Expert
Expert
Jump to solution

I tried $report | Export-Csv -NoTypeInformation -Path c:\temp\info.csv already

But the o/p in the csv file shows name but the boottime coloum is empety .. not sure what is wrong .. can u Please re-check

Reply
0 Kudos
dajbozerozum
Contributor
Contributor
Jump to solution

Invoke-VMScript works only on PowerCLI 32-bit right?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Only when running against a VM on vSphere server earlier than 5.0


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
esxi1979
Expert
Expert
Jump to solution

I am not sure what could be wrong here, i am tring to install vmware tool in bulk .. tried below to test, nothing happens

get-vm vm1 |Invoke-VMScript -guestcredential $c3 -ScriptType bat msiexec.exe /i "D:\setup64.exe" /s /v /qn ADDLOCAL=All

get-vm vm1 |Invoke-VMScript -hostcredential $c3 -guestcredential $c3 -ScriptType bat msiexec.exe /i "D:\VMware Tools.msi" ADDLOCAL=ALL /qn

===

But below works

get-vm vm1  |Invoke-VMScript   -guestcredential $c3 -ScriptType bat  dir

Reply
0 Kudos
kunaludapi
Expert
Expert
Jump to solution

If you want to upgrade vmware tools use update manager for that, as to run script inside VM, vmware tools should be present.

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
LucD
Leadership
Leadership
Jump to solution

You didn't state if there were any error messages, but try specifying the command to execute as a string.

Like this

get-vm vm1 | Invoke-VMScript -guestcredential $c3 -ScriptType bat -ScriptText "msiexec.exe /i D:\setup64.exe /s /v /qn ADDLOCAL=All"

But note that you need to already have the VMware Tools installed to use the Invoke-VMScript cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

esxi1979
Expert
Expert
Jump to solution

I am exploring Sysinternal psexec tool for this.

I have posted a Q on their portal tho Smiley Sad

======================================================================================================================================================

Hi,

I am trying to insall vmware tools on a windows vm with below cmd ..it hangs below

psexec \\10.xxx -u domain\id
-p passwd  msiexec.exe /i "D:\setup[64].exe" /s /v /qn ADDLOCAL=All -
qn

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com


Can someone Please help ?

======================================================================================================================================================

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at KB1018377 to see the silent install options for the VMware Tools sw.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference