get Computer Uptime

<#@
.SYNOPSIS Gets the uptime and last boot time from remote or local computer
.PARAMETER computername
The computer name or names to query DEFAULT: localhost
.EXAMPLE get-upt -computername localhost
#>
Function get-upt{
    [CmdLetBinding()]
    param (
        [parameter(Mandatory=$true)]
        [string]$comp_name
    )
    write-verbose “connecting to $comp_name”
    $os = Get-WmiObject win32_OperatingSystem -ComputerName $comp_name
    $boottime = $os.ConvertToDateTime($os.lastbootuptime)
    $uptime = New-TimeSpan (get-date $boottime)
    $uptime_days = [int]$uptime.Days
    $l = “$comp_name,$boottime,$uptime_days”
    $l | Out-File c:\uptime.csv -Append
    write-verbose “… finished”
    }

$servernames = Get-Content C:\Servers.txt

foreach ($servername in $servernames){
    if (Test-Connection -ComputerName $servername -count 1 -Quiet){
    get-upt $servername -Verbose
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *