Get last boot time for server list and Export to csv file
-
Thursday, December 13, 2012 10:19 PM
Hi,
I am a newb when it comes to scripting/powershell. I have a PS script to read from a list of server names in a txt file and return the last boot time information for each server. The script works great except I would like to get that information exported into a csv file so I can import it into excel and be able to sort it. The Main thing that I want to display is just the server name and the last boot date. my script is as follows...
$Servers = Get-Content “ServersTest.txt”
foreach($Server in $Servers)
{
#CREATE A WMI QUERY AGAINST THE CURRENT $SERVER NAME
$wmi=Get-WmiObject -class Win32_OperatingSystem -computer $Server
#THE LAST BOOT TIME COMES BACK IN A LONG FORMAT SO CONVERT IT TO A VALID DATE TIME AND ASSIGN IT TO $LASTBOOTTIME
$lastBootUpTime=$wmi.ConvertToDateTime($wmi.LastBootUpTime)
#CALCULATE TIME SINCE LAST BOOT
$now = Get-Date
$upTime = $now - $lastBootUpTime
$days = $Uptime.Days
$hours = $Uptime.Hours
$min = $uptime.Minutes
$sec = $uptime.Seconds
#DISPLAY RESULTS
Write-Host "SERVER NAME:" $Server "Last Boot Time:" $lastBootUpTime
Write-Host "HAS BEEN UP FOR: " $days "DAYS " $hours "HOURS " $min "MINUTES" $sec "SECONDS"
}
Any Help would be greatly appreciated.
Thank you,
Kevin
All Replies
-
Friday, December 14, 2012 12:34 AM
Too many quotes
Write-Host "SERVER NAME: $Server Last Boot Time: $lastBootUpTime"
Write-Host "HAS BEEN UP FOR: $days DAYS $hours HOURS $min MINUTES $sec SECONDS"
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Friday, December 14, 2012 12:34 AM
-
Friday, December 14, 2012 12:44 AM
Here is an easier way to do this:
Get-WmiObject Win32_OperatingSystem -computer (Get-Content ServersTest.txt) |
Select CSNAME, LastBootUpTime |
Export-Csv file.CSV -NoTypeInfoThat's all you need to do.
¯\_(ツ)_/¯
-
Friday, December 14, 2012 12:52 AM
Or this way:
Get-WmiObject Win32_OperatingSystem -computer (Get-Content ServersTest.txt) | Select CSNAME, @{N='LastBoot';E={[System.Management.ManagementDateTimeCOnverter]::ToDateTime($_.LastBootUpTime)}} | Export-Csv file.CSV -NoTypeInfo¯\_(ツ)_/¯
-
Friday, December 14, 2012 1:58 PM
JRV,
Thank you. That is exactly what i was looking for.
Kevin Whitaker

