Link to home
Start Free TrialLog in
Avatar of patron
patronFlag for India

asked on

Script to check last reboot date-time with credentials used to reboot

Please help to share power shell  script To check last reboot for windows servers (mentioned in server list ) with user id used for reboot
Avatar of yo_bee
yo_bee
Flag of United States of America image

You will need the Active Directory Module on the computer you want to run this script from.
I have a Get-WinEvent script that I use to see how long a user's computer is locked and I figure I can do something similar for your request.

I first gather all the computer in my environment using the Get-ADComputer  and then for each computer I run Get-WinEvent and story it in an array.

$computers = Get-adcomputer -Filter * -SearchBase 'OU=Computers,OU=FLH,DC=contoso,DC=com' -SearchScope OneLevel 
$Table = @()
Foreach ($computer in $computers)
{
 If(Test-Connection -ComputerName $computer.name)
 {
    Get-WinEvent -ComputerName $computer.Name -FilterHashtable @{ProviderName='User32';ID = 1074} | select -First 1 | foreach {  $sid = $_.userid;  if($sid -eq $null) { return; }  $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid);  $objUser = $objSID.Translate([System.Security.Principal.NTAccount]);
     $Results = New-Object Object
     $Results | Add-Member -Type NoteProperty -Name 'Computername' -Value $computer.name 
     $Results | Add-Member -Type NoteProperty -Name 'User' -Value $objUser.Value
     $Results | Add-Member -Type NoteProperty -Name 'Date' -Value $_.TimeCreated
     $table += $Results
    }
    }
    }
    $table | Export-Csv -Path C:\temp\Lastreboot.csv -NoTypeInformation

Open in new window


I took some info from these two sites and put together the script.  
https://prajwaldesai.com/restarted-windows-server/
https://stackoverflow.com/questions/17155311/get-username-from-get-winevent
ASKER CERTIFIED SOLUTION
Avatar of Jess Dodson
Jess Dodson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patron

ASKER

Thanks