header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Get last reboot time for a remote machine
Last Post 04 Apr 2008 06:48 PM by bsonposh. 13 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 04:21 PM  

Right now I'm using the add-in PshX-Sapien to pass the machine name and it would be nice to somehow pass it to get the last reboot time from th event logs. I can get my machine's last reboot time by simply putting:

<br />
$app = (Get-EventLog -LogName system|where{$_.Eventid -eq '6005'}|select -First 1)<br />
$app.Timegenerated<br />

Thanks for the help and let me know if there is any confusion.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 04:42 PM  
Perhaps another way is to look at the LastBootUpTime.

Get-WmiObject Win32_OperatingSystem | Select LastBootUpTime
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 04:47 PM  

Thanks, I thought about that as soon as I posted this and then I went to go get some breakfast. But thanks for the quick reply. That works but it gives me this: 20080403165449.375000-360
Is there a way to split it up into Date and Time separately?

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 04:57 PM  
If you like a nice pretty date... you could change this to a datetime object like so


$date = Get-WmiObject Win32_OperatingSystem | %{$_.LastBootUpTime}
$RebootTime = [System.DateTime]::ParseExact($date.split(".")[0],'yyyyMMddHHmmss',$null)
$RebootTime
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 04:57 PM  
bleh
Ύ], should be [ 0 ] no spaces
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 05:31 PM  

getting an error, not sure on how to fix it:

ERROR: You cannot call a method on a null-valued expression.
ERROR: At line:19 char:56
ERROR: + $RebootTime = [System.DateTime]::ParseExact($date.split( <<<< '.')[2],'yyyyMM
ERROR: ddHHmmss',$null) $RebootTime

$compres = Read-InputBox -message "Enter a computer name." -title "Restart Time for Computer"</p>
<p>$date = Get-WmiObject Win32_OperatingSystem -ComputerName $compres| foreach{$_.LastBootUpTime} `<br />
$RebootTime = [System.DateTime]::ParseExact($date.split('.')[0],'yyyyMMddHHmmss',$null) $RebootTime 

Thanks Brandon you da man!

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 05:38 PM  
That means the $date is not the format it expects.
I would write-host $date and see what you get. Also... it does look like you changed Ύ] to [ 0 ]
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 05:39 PM  
I meant does NOT look like you changed :)
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 06:07 PM  

Yeah I did actually but it came out like yours with the 'Y]

I'll try that and let you know.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 06:20 PM  
what did $date look like?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 06:23 PM  

Ok,
If I put Write-host $date after the line that is getting the error then I get the same error. But of course if I put it before that line I get the same number as if it weren't formatted.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 06:31 PM  
Try this.. it worked like a charm for me
http://www.powershellcentral.com/scripts/166
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
valdezdjUser is Offline
New Member
New Member
Posts:62

--
04 Apr 2008 06:45 PM  

Ok I don't know what changed from your other script but that worked. ???
So here's the final script for this, thanks Brandon!

<br />
Add-PSSnapin PshX-Sapien</p>
<p>$compres = Read-InputBox -message "Enter a computer name." -title "Restart Time for Computer"<br />
$date = Get-WmiObject Win32_OperatingSystem -ComputerName $compres | foreach{$_.LastBootUpTime}<br />
$RebootTime = [System.DateTime]::ParseExact($date.split('.')[0],'yyyyMMddHHmmss',$null) <br />
$compname = (Get-WmiObject -class Win32_ComputerSystem -computername $compres)</p>
<p>$myarr = @()</p>
<p>$myset = ""|Select-Object Name, Username, Model, Time<br />
$myset.Name = $compname.name<br />
$myset.Username = $compname.username<br />
$myset.Model = $compname.model<br />
$myset.Time = $RebootTime<br />
$myarr += $myset</p>
<p>$myarr|ft -auto<br />

My boss likes to see other information and it looks nice if I have to export it out to excel or csv

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
04 Apr 2008 06:48 PM  
glad to help
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer