Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Get-Uptime.ps1 #14

Closed
steviecoaster opened this issue Jul 25, 2018 · 7 comments
Closed

Add Get-Uptime.ps1 #14

steviecoaster opened this issue Jul 25, 2018 · 7 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@steviecoaster
Copy link
Owner

Function to read in a simple list file and generate an uptime report object that can be piped into Export-CSV or Out-GridView

Idea Credit: Reddit user /u/OttoVonMonstertruck

@steviecoaster steviecoaster added the enhancement New feature or request label Jul 25, 2018
@Adhdmatt
Copy link

`Function Get-Uptime {

[CmdletBinding()]
Param ( [string] $ComputerName = $env:COMPUTERNAME )
$os = Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue
if ($os.LastBootUpTime) {
     $uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime)
    Write-Output ("Last boot: " + $os.ConvertToDateTime($os.LastBootUpTime) )
    Write-Output ("Uptime   : " + $uptime.Days + " Days " + $uptime.Hours + " Hours " + $uptime.Minutes + " Minutes" )
}
else {
    Write-Warning "Unable to connect to $computername"
}

}`

@theSysadminChannel
Copy link

theSysadminChannel commented Jul 25, 2018

I went ahead and added to @Adhdmatt Get-UpTime Script. Mainly to grab the properties and put them in an object. I also added the functionality to be able to input an array of computers.

Function Get-Uptime {

[CmdletBinding()]

Param ( 
    [string[]] 
        $ComputerName = $env:COMPUTERNAME,
        
    [Switch]   
        $ShowOfflineComputers
    
    )
BEGIN {
    $ErroredComputers = @()
}

PROCESS {
    Foreach ($Computer in $ComputerName) {
        Try {
            $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
            $Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
            $Properties = @{ComputerName  = $Computer
                            LastBoot      = $OS.ConvertToDateTime($OS.LastBootUpTime)
                            Uptime        = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes")
                            }

            $Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime

        } catch {
            if ($ShowOfflineComputers) {
                $ErrorMessage = $Computer + " Error: " + $_.Exception.Message
                $ErroredComputers += $ErrorMessage

                $Properties = @{ComputerName  = $Computer
                                LastBoot      = "Unable to Connect"
                                Uptime        = "Error Shown Below"
                                }

                $Object = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime
            }
               
        } finally {
            Write-Output $Object        

            $Object       = $null
            $OS           = $null
            $Uptime       = $null
            $ErrorMessage = $null
            $Properties   = $null
        }
    }
    
    if ($ShowOfflineComputers) {
        Write-Output ""
        Write-Output "Errors for Computers not able to connect."
        Write-Output $ErroredComputers
    }
}

END {}

}

@MikeShepard
Copy link

WMI cmdlets are deprecated. Shouldn't this use CIM cmdlets instead?

@steviecoaster
Copy link
Owner Author

You are correct, and using CIM returns DateTime objects instead of file time, so no weird conversion code is needed. Fixed in 8abc422

@steviecoaster steviecoaster added this to the Functional milestone Jul 29, 2018
@steviecoaster steviecoaster self-assigned this Jul 29, 2018
@steviecoaster
Copy link
Owner Author

After testing, this appears functional

@theSysadminChannel
Copy link

The problem with cim cmdlets are that it also requires WinRM do be running. Try running this on a client computer or a Windows 2008 server. You'll notice you get errors because WinRM is not enabled by default.

@vexx32
Copy link
Contributor

vexx32 commented Jul 30, 2018

WinRM isn't needed for CIM. It's needed to remotely invoke it, but not for CIM itself. I've used CIM on many machines that aren't properly configured for WinRM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants