A PowerShell function to retrieve uptime information for one or more Windows computers.
The Get-Uptime
function allows you to query the boot time and current uptime of specified Windows computers. It supports remote execution, credential passing, and various output formats.
Get-Uptime [-ComputerName <String[]>] [-Credential <PSCredential>] [-OutputFormat <String>] [-LogPath <String>]
The Get-Uptime
function uses Windows Management Instrumentation (WMI) to retrieve operating system information from target computers. It calculates the current uptime based on the last boot time and provides options for formatting the output.
- ComputerName: Specifies the target computers. Default is the local computer.
- Credential: Specifies alternate credentials for connecting to remote computers.
- OutputFormat: Specifies the output format. Valid options are "Object" (default), "CSV", or "HTML".
- LogPath: Specifies the path for the log file. Default is "$env:TEMP\Get-Uptime.log".
- Get uptime for the local computer:
Get-Uptime
- Get uptime for multiple remote computers:
Get-Uptime -ComputerName "Server1", "Server2", "Server3"
- Get uptime and export results to CSV:
Get-Uptime -ComputerName "Server1", "Server2" -OutputFormat CSV
- Get uptime using alternate credentials:
$cred = Get-Credential | Get-Uptime -ComputerName "RemoteServer" -Credential $cred
- Ensure you have the necessary permissions to query WMI on target computers.
- The function uses PowerShell's parallel execution feature for improved performance when querying multiple computers.
- Error handling is implemented to provide informative messages for troubleshooting.
If you encounter issues:
- Check network connectivity to target computers.
- Verify that the Remote Registry service is running on target computers.
- Ensure you have the necessary permissions.
- Review the log file specified by the -LogPath parameter for detailed error messages.
Remember, as the old IT adage goes: "Have you tried turning it off and on again?" But with this script, at least you'll know exactly how long it's been since someone did!