Ever felt the need the need to get the boot time of the servers or workstations? What If these machines are remote machines and are in huge numbers? Logging on to the each and every machine will be hectic and many a times impossible.
Although there are many tools out there to accomplish this task but there's absolutely no need to spend time and money on such tools when we have robust powershell that is absolutely free and easy to use and would do our work within minutes.
We have created the script to fetch the boot time of windows servers on the network. This will fetch the boot time of the servers and will dump it in the text file along with the server name. For the servers that are not reachable or if there is any kind of error that occurs while fetching the boot time, the script will output 'Error in connecting' along with the server name in the output file.
Here, in this example we are running the script for all the domain controller servers in the domain. This is done using the list of domain controllers which is fetched using 'Get-ADDomainController' command. You may want to provide a separate list of servers. You may do this by taking the server list from a text file. You will just have to replace "Get-ADDomainController -Filter * | select -ExpandProperty name" with "Get-Content D:\ServerList.txt" where D:\ServerList is the complete filepath of the input text file. You can change it with the filepath of your choice. Additionally you should remove "Import-Module activedirectory" because if the server from where you are running the script does not have RSAT tool installed, it will throw an error.
So, that's all about the script and how to run it. Section underneath contains the complete script. Enjoy!!
Import-Module activedirectory
$servers= Get-ADDomainController -Filter * | select -ExpandProperty name | sort
foreach ($server in $servers) {
try
{
$time = ((Get-WmiObject -ComputerName $server -Namespace root\cimv2 -Class win32_operatingsystem -ErrorAction SilentlyContinue | select -expandproperty lastbootuptime) -split "\.")[0]
}
catch
{
Write-Output "$server Error in connecting" `n | Out-File D:\BootTime.txt -Append
continue
}
$year = $time.Substring(0,4)
$month = $time.Substring(4,2)
$day = $time.Substring(6,2)
$hours = $time.Substring(8,2)
$minutes = $time.Substring(10,2)
$seconds = $time.Substring(12,2)
$output = "$month-$day-$year $hours`:$minutes`:$seconds"
$bootdate=[datetime]$output
Write-output "$server - $bootdate" | Out-File D:\rebootstatus.txt -Append
}