Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.         Retrieve Last bootup information
  4.    
  5.     .DESCRIPTION
  6.         This function will connect to remote computers to retrieve their last boot timestamp.
  7.    
  8.     .PARAMETER ComputerName
  9.         Name of computer system to query
  10.    
  11.     .PARAMETER Credential
  12.         Credential object to use when connecting to remote computers
  13.    
  14.     .EXAMPLE
  15.         PS C:\> Get-PCUptime -ComputerName 'server1','server2'
  16.        
  17.         This example will connect to the two computers listed and retrieve their last boot time
  18.         information using the security context of the user running the script
  19.    
  20.     .EXAMPLE
  21.         PS C:\> $cred = Get-Credential
  22.         Get-PCUptime -ComputerName 'server1','server2' -Credential $cred
  23.  
  24.         This example will connect to the two computers listed and retrieve their last boot time
  25.         information using the security context of the credential created when calling Get-Credential
  26.    
  27.     .NOTES
  28.         Additional information about the function.
  29. #>
  30. function Get-PCUptime
  31. {
  32.     [CmdletBinding()]
  33.     param
  34.     (
  35.         [Parameter(Mandatory = $true)]
  36.         [ValidateNotNullOrEmpty()]
  37.         [string[]]$ComputerName,
  38.         [ValidateNotNullOrEmpty()]
  39.         [pscredential]$Credential
  40.     )
  41.    
  42.     begin
  43.     {
  44.         #no prep work needed for this function
  45.     }
  46.    
  47.     process
  48.     {
  49.         foreach ($name in $ComputerName)
  50.         {
  51.            
  52.             # use splatting to add credential parameter if present when called
  53.             $obj = New-Object psobject -Property @{
  54.                 ComputerName     = $name;
  55.                 LastBootUpTime   = $null;
  56.                 Uptime           = $null
  57.             }
  58.            
  59.             try
  60.             {
  61.                 #any exception in this block causes script to fall back to DCOM for connection to remote server
  62.                 $arguments = @{
  63.                     ComputerName   = $name;
  64.                     ErrorAction    = 'Stop'
  65.                 }
  66.                
  67.                 if ($Credential)
  68.                 {
  69.                     $arguments.Add('Credential', $Credential)
  70.                 }
  71.                
  72.                 $session = New-CimSession @arguments
  73.                 $OS = Get-CimInstance -CimSession $session -ClassName Win32_OperatingSystem -ErrorAction Stop
  74.             }
  75.            
  76.             catch
  77.             {
  78.                 try
  79.                 {
  80.                     $SessionOption = New-CimSessionOption -Protocol DCOM
  81.                    
  82.                     $arguments = @{
  83.                         ComputerName    = $name;
  84.                         ErrorAction     = 'Stop';
  85.                         SessionOption   = $SessionOption
  86.                     }
  87.                    
  88.                     if ($Credential) { $arguments.Add('Credential', $Credential) }
  89.                    
  90.                     $session = New-CimSession @arguments
  91.                     $OS = Get-CimInstance -CimSession $session -ClassName Win32_OperatingSystem -ErrorAction Stop
  92.                 }
  93.                
  94.                 catch
  95.                 {
  96.                     'Connection to server {0} failed' -f $name | Write-Error
  97.                 }
  98.             }
  99.            
  100.             try
  101.             {
  102.                
  103.                 $LastBootTime = $OS | Select-Object -ExpandProperty LastBootUpTime
  104.                
  105.                 $obj.LastBootUpTime = $LastBootTime
  106.                
  107.                 $obj.Uptime = New-TimeSpan -Start $LastBootTime -End (get-date)
  108.                 $obj
  109.                
  110.             }
  111.            
  112.             catch
  113.             {
  114.                 'Unable to retrieve last boot time on server {0}' -f $name | Write-Error
  115.                 $obj
  116.             }
  117.         }
  118.     }
  119.    
  120.     end
  121.     {
  122.         # no cleanup code needed in this function
  123.     }
  124. }
  125.  
  126. if (!$cred) { $cred = Get-Credential}
  127. Get-PCUptime -ComputerName 'miw059wt-cmtest','miw070wt-cmtest' -Credential $cred
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement