I'm having trouble getting a Powershell Foreach loop to output the [DateTime] object so that I can compare it with another object after rebooting.
The script example below, I am looking to create a hash table to save the Computername + Last reboot time, and then add the current reboot time so that it can compare the reboot time.
$servers = GC D:\Scripts\list1.txt foreach($server in $servers){ Try{ $operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $server $current = [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime) "$server last rebooted $current" }
-Edit, the above script outputs below as a string. I am trying to get a collection of TypeName: System.DateTime objects.
Server1 last rebooted 10/24/2015 11:39:34 Server2 last rebooted 10/22/2015 01:34:33
Thus, I chatted more and more and got this line, essentially the script becomes "Restart computers until everything becomes relevant."
IF($current -gt ((Get-Date).AddHours(-6))) {"Server reboot is current for $server"}ELSE{"Please check $server"}
source share