Reputation: 103
I can create a CSV from the following code, however it fails to give details on the "System Up Time" for days.
It displays the hours and the minutes no problem, but instead of giving a number before days, just prints out the word days.
Server Last Rebooted System Up Time Srv1 25/07/2018 20:49 Days 12 Hours 37 Minutes PC1 04/02/2019 15:55 Days 17 Hours 31 Minutes
I have tried to surround the $Uptime
variable with parentheses, but to no avail. I have also tried $_.Days
to get the information without any luck.
$Path = "C:\Temp\Manual_Servers"
$Tx = "\Servers.txt"
$CS = "\Res.csv"
$File = $Path + $tx
$Server = GC $File
$Np = "\Res.txt"
$Res = $Path + $Np
$Res2 = $Path + $CS
function Uptime2 {
$Server | ForEach-Object {
$SF = $_
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $_ -ErrorAction SilentlyContinue
$uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime)
$OS |
Select-Object @{Name="Server";Expression={$SF}},
@{Name="Last Rebooted";Expression={($_.ConvertToDateTime($os.LastBootUpTime)).DateTime}},
@{Name="System Up Time";Expression={$_.$uptime.Days + "Days " + $uptime.Hours + " Hours " + $uptime.Minutes + " Minutes" }} |
Export-Csv $Res2 -NoClobber -NoTypeInformation -Append
}
}
Uptime2
Desired result:
Server Last Rebooted System Up Time Srv1 25/07/2018 20:49 200 Days 12 Hours 37 Minutes PC1 04/02/2019 15:55 9 Days 17 Hours 31 Minutes
Upvotes: 0
Views: 52
Reputation: 103
Bizzarely here is the solution;
$Boot| Select-Object @{Name="Server";Expression={$SF}},
@{Name="Last Rebooted";Expression={$($boot)}},
@{Name="System Up Time";Expression= {$Boot.$uptime.Seconds +""+ $uptime.Days + " Days " + $uptime.Hours + " Hours " + $uptime.Minutes + " Minutes "}}
| Export-CSv $Res2 -NoClobber -NoTypeInformation -Append
I Had to add the $uptime.seconds to print nothing then add a blank "" for it to print out correctly. See results;
Upvotes: 0
Reputation: 146
Try using this code as a base example and reworking it to your needs:
$servers = 'COREDC01','COREDC02'
$currentDT = Get-Date
foreach ($i in $servers) {
$BootTime = (Get-CimInstance -ComputerName $i -ClassName Win32_OperatingSystem).LastBootupTime
$uptime = $currentDT - $BootTime
Write-Output `
-InputObject "Uptime for Server $i -
Days:$($uptime.Days),
Hours:$($uptime.Hours),
Minutes:$($uptime.Minutes),
Seconds:$($uptime.Seconds)
"
}
Upvotes: 1