VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Uptime parameter shows blank

Hi,

I am getting the blank output for uptime days, Hours and Minutes, please help

Script

$script = @'

Get-WmiObject win32_operatingsystem | %{

  $_.ConverttoDateTime($_.lastbootuptime)

  }

'@

$machines = Get-Content .\CS_VM1.txt

Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |

Select @{N ='VM';E={$_.VM.Name}},

@{N='LastBootUpTime'; E={[DateTime]$_.ScriptOutput}},

@{N='Uptime';E={[math]::Round((New-TimeSpan -Start ([DateTime]$_.ScriptOutput) -End (Get-Date).Days + " Days " + (Get-Date).Hours + "Hours" + (Get-Date).Minutes + "Minutes"))}} | ft -auto

Output :

VM         LastBootUpTime    Uptime

APP04      05/26/2019 5:45:37 AM

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$script = @'

Get-WmiObject win32_operatingsystem | %{

  $_.ConverttoDateTime($_.lastbootuptime)

  }

'@

$machines = Get-Content .\CS_VM1.txt

Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |

Select @{N ='VM';E={$_.VM.Name}},

@{N='LastBootUpTime'; E={[DateTime]$_.ScriptOutput}},

@{N='Uptime';E={

   $diff = New-TimeSpan -Start ([DateTime]$_.ScriptOutput) -End (Get-Date)

   "$($diff.Days) Days $($diff.Hours) Hours $($diff.Minutes) Minutes"

   }} |

ft -auto

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$script = @'

Get-WmiObject win32_operatingsystem | %{

  $_.ConverttoDateTime($_.lastbootuptime)

  }

'@

$machines = Get-Content .\CS_VM1.txt

Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |

Select @{N ='VM';E={$_.VM.Name}},

@{N='LastBootUpTime'; E={[DateTime]$_.ScriptOutput}},

@{N='Uptime';E={

   $diff = New-TimeSpan -Start ([DateTime]$_.ScriptOutput) -End (Get-Date)

   "$($diff.Days) Days $($diff.Hours) Hours $($diff.Minutes) Minutes"

   }} |

ft -auto

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD, Still Uptime shows blank without any error

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Seems to work perfectly for me.

How are you running this?
Is the script returning anything for the VMs you specified in the .TXT file?

uptime.png


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

I was able to fix, there was typo error from my end. Thanks a lot for your help Smiley Happy

Reply
0 Kudos