$OperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
Write-Host "System Uptime: $($Uptime.Days) days, $($Uptime.Hours) hours, $($Uptime.Minutes) minutes, $($Uptime.Seconds) seconds"
That's great, thanks. Can you please modify the script so that it logs to a file called uptime.log, which includes the uptime and uptime start time/date?I hope this helps! Let me know if you have any other questions.
Yes, I can definitely help with that. Here's the modified script that logs the uptime and start time/date to a file called `uptime.log`:That's great, thanks. Can you please modify the script so that it logs to a file called uptime.log, which includes the uptime and uptime start time/date?
$OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
$UptimeDays = $Uptime.Days
$UptimeHours = $Uptime.Hours
$UptimeMins = $Uptime.Minutes
$UptimeSecs = $Uptime.Seconds
$StartTime = $CurrentTime - $Uptime
Write-Host "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds"
$Path = "C:\Logs\uptime.log"
$Content = "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime"
Add-Content $Path $Content
Please modify to capture an entire domain.Yes, I can definitely help with that. Here's the modified script that logs the uptime and start time/date to a file calleduptime.log
:
Code:$OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem $LastBootUpTime = $OperatingSystem.LastBootUpTime $CurrentTime = Get-Date $Uptime = $CurrentTime - $LastBootUpTime $UptimeDays = $Uptime.Days $UptimeHours = $Uptime.Hours $UptimeMins = $Uptime.Minutes $UptimeSecs = $Uptime.Seconds $StartTime = $CurrentTime - $Uptime Write-Host "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" $Path = "C:\Logs\uptime.log" $Content = "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime" Add-Content $Path $Content
This script does the following:
1. It uses theGet-WmiObject
cmdlet to retrieve the system'sLastBootUpTime
property from theWin32_OperatingSystem
class.
2. It retrieves the current time and calculates the difference between the current time and theLastBootUpTime
property using the-
operator.
3. It formats the uptime values as days, hours, minutes, and seconds and sets them to variables.
4. It calculates the start time/date by subtracting the uptime from the current time.
5. It displays the system uptime in days, hours, minutes, and seconds using theWrite-Host
cmdlet.
6. It sets the log file path and content.
7. It appends the uptime and start time/date to the log file using theAdd-Content
cmdlet.
Make sure to replaceC:\Logs\uptime.log
with the desired file path for the log file.
I hope this helps! Let me know if you have any other questions.
If you want to capture the uptime for all the computers in a domain, you'll need to modify the original script to loop through all the computers in the domain and retrieve their individual uptime. Here's an updated script that can accomplish that:Please modify to capture an entire domain.
$domain = "yourdomain.com" # Replace with your domain name
$cred = Get-Credential # Enter Domain Admin credentials
$computerList = Get-ADComputer -Filter * -Credential $cred -Server $domain | Select-Object -ExpandProperty Name
foreach ($computer in $computerList) {
$OperatingSystem = Get-WmiObject -ComputerName $computer -Credential $cred -Class Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
$UptimeDays = $Uptime.Days
$UptimeHours = $Uptime.Hours
$UptimeMins = $Uptime.Minutes
$UptimeSecs = $Uptime.Seconds
$StartTime = $CurrentTime - $Uptime
Write-Host "System Uptime for $computer: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds"
$Path = "C:\Logs\uptime.log"
$Content = "Computer Name: $computer" + "`r`n" + "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime"
Add-Content $Path $Content
}
Get-Credential
cmdlet.Get-ADComputer
cmdlet to retrieve a list of all computers in the domain and selects their names.LastBootUpTime
property from the Win32_OperatingSystem
class.LastBootUpTime
property using the -
operator.Write-Host
cmdlet.Add-Content
cmdlet.yourdomain.com
with the appropriate domain name and provide the appropriate credentials. Also, replace C:\Logs\uptime.log
with the desired file path for the log file.