A N

January 11, 2016

January-2016-scripting-games-puzzle | Get-UpTime

Filed under: Tips — donrsh @ 3:25 pm

My solution for January-2016-scripting-games-puzzle

Get-UpTime


<#	
	.NOTES
	===========================================================================
	 Created on:   	2016-01-11 08:28
	 Created by:   	Arash Nabi
	 Mail:          arash@nabi.nu 	
	===========================================================================
	.SYNOPSIS
		A brief description of the Get-UpTime function.
	
	.DESCRIPTION
		Get uptime of the remote server. This is 2016-January Scripting Games Puzzle
	    For more info please visist: http://powershell.org/wp/2016/01/02/january-2016-scripting-games-puzzle/
	.EXAMPLE
		PS C:\> Get-UpTime -ComputerName 
	    PS C:\> $value1 | Get-UpTime
#>

function Get-UpTime
{
	[CmdletBinding()]
	param
	(
		[Parameter(Mandatory = $false,
				   ValueFromPipeline = $true)]
		$ComputerName = $env:COMPUTERNAME
	)
	
	Process
	{
		try
		{
            $AllObj = @()
			foreach ($PC in $ComputerName)
			{
				$params = @{
					'ComputerName' = "$PC"
				}
                $PingComputer = Test-Connection -ComputerName $PC -ErrorAction SilentlyContinue
				if (-not ($PingComputer))
				{
					$params.Add("Status", "Offline")
					Write-Warning "$PC is Offline"
				}
				# 
				if ($PingComputer)
				{
					$objOption = New-CimSessionOption -Protocol Dcom -ErrorAction SilentlyContinue
					$objSession = New-CimSession -ComputerName $PC -SessionOption $objOption -ErrorAction SilentlyContinue
					if ($objSession)
					{
						$LastBootUpTime = Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_OperatingSystem | select LastBootUpTime
						if ($LastBootUpTime)
						{
							$upTime = New-TimeSpan -Start $($LastBootUpTime.LastBootUpTime) -End (get-date)
							$params.add("StartTime", "$($LastBootUpTime.LastBootUpTime)")
							$params.Add("Status", "OK")
							$params.Add("UpTime (Days)", "$($upTime.Days)")
						}
						
						if ($upTime -gt 30)
						{
							$params.add("MightNeedPatched", "Yes")
						}
						else
						{
							$params.add("MightNeedPatched", "No")
						}
					}
					else
					{
						$params.Add("Status", "Error")
					}
				}
				
				$objresult = New-Object psobject -Property $params
				$AllObj += $objresult
			}
			
		}
		
		catch [System.Net.WebException], [System.Exception]
		{
			$errorMessage = $_.Exception.Message
            Write-Warning -Message $errorMessage

		}
		finally
		{
			$AllObj | Select-Object ComputerName, StartTime, 'Uptime (Days)', Status, MightNeedPatched | Format-Table -AutoSize
		}
	}
	
}


	
Advertisements

2 Comments »

  1. I went with a Workflow to ensure that it processes “multiple servers at once”

    Workflow Get-UpTime {
        
        param([string[]]$servers)
    
        If (-not $servers) {
            $servers = $env:computername
        }
    
    
        ForEach -parallel ($server in $servers) {
            InlineScript {
                $BootTime = "N/A"
                $Uptime = "N/A"
                If (Test-Connection -ComputerName $using:server -Quiet -Count 1 ){
    
                    $OS = Get-WmiObject win32_operatingsystem -ComputerName $using:server -ErrorAction SilentlyContinue
                    
                    If (-not $OS) {
                        $status = "Error"
                    } Else {
                        $upTime = New-TimeSpan -Start $($OS.ConvertToDateTime($OS.LastBootUpTime)) -End (get-date)
                        $BootTime = $OS.ConvertToDateTime($OS.LastBootUpTime) 
                        $Status = "OK"
    
                        If ($upTime -gt 30) {
                            $NeedsPatching = "Yes"
                        } Else {
                            $NeedsPatching = "No"
                        }
                    }
                } Else {
                    $status = "Offline"
                }
                    [pscustomobject] @{
                    Server = $using:Server
                    'Start Time' = $BootTime
                    'Up Time (Days)' = $Uptime.Days
                    'Needs patching' = $NeedsPatching
                    Status = $status
                }
            }
        }
    }
    

    Comment by Matt — January 22, 2016 @ 11:52 am

    • Intresting Matt, Thanks for shareing your code.

      Comment by donrsh — February 4, 2016 @ 1:29 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: