PoshCode Archive  Artifact [4ad617247e]

Artifact 4ad617247ea575f55215e8aa9e3bb07ddeedb3ffa7d23526535d39e74144c892:

  • File Set-RemoteService.ps1 — part of check-in [1a5751c7cd] at 2018-06-10 13:49:24 on branch trunk — The Function Set-RemoteService will set the State and StartMode for the Service on the Remote Machine. (user: DexterPOSH size: 5598)

# encoding: ascii
# api: powershell
# title: Set-RemoteService
# description: The Function Set-RemoteService will set the State and StartMode for the Service on the Remote Machine.
# version: 3.0
# type: function
# author: DexterPOSH
# license: CC0
# function: Get-ServerUptime
# x-poshcode-id: 5056
# x-archived: 2014-04-12T03:30:18
# x-published: 2014-04-07T04:38:00
# It will also interpret the WMI return codes if any and display a warning (Other than Return Code 0 for success)
# Note – Can pass alternate credentials too (requires Min PowerShell v3) 
# Fixed a typo in the WMIHash splatting variable
Function Get-ServerUptime {
		gets ServerUptime Info

		Uses Win32_ComputerSystem to get the LastBootUptime from Remote Machine.
        Creates a PSObject and writes it to pipeline

       You can pipe the computer names, take it from a text file or pass objects with ComputerName property

        PS C:\> Get-ServerUptime -ComputerName DeterDC -Verbose
        VERBOSE: Starting the Function...Checking if ErrorFile exists
        VERBOSE:  ErrorFile exists & logging time to it - C:\Users\DexterPOSH\Desktop\UpTimeError.txt
        VERBOSE: Checking if DeterDC is online
        VERBOSE: DeterDC is online

        ComputerName                                      Uptime                                            LastReboot                                      
        ------------                                      ------                                            ----------                                      
        DeterDC                                           137 Days 7 Hours 4 Min 46 sec                     11/19/2013 11:11:15 AM                          
        VERBOSE: Ending the Function



		Original Script : Bhargav Shukla MSFT
        Url _ http://blogs.technet.com/b/bshukla/archive/2010/12/09/powershell-script-to-report-uptime.aspx

        Modified by - DexterPOSH
        Blog Url - http://dexterposgh.blogspot.com
#Requires -RunAsAdministrator
#Requires -version 3.0
    # Specify the ComputerNames (Default is the local machine)
				helpmessage="Enter the ComputerNames to check uptime on",

    # Error Log file , Default path User Desktop\UpTimeError.txt
    [Parameter(Mandatory=$false,helpmessage="Enter the Startup Type of the Service to be set")]

    [Parameter(Mandatory=$false,helpmessage="Enter the Credentials to Use")]
    [System.Management.Automation.PSCredential]$Credentials = [System.Management.Automation.PSCredential]::Empty

    Write-Verbose -Message "Starting the Function...Checking if ErrorFile exists" 
    #CReate the Errofile ...it will log Offline machines, Machines with issues
    if (!(Test-Path -Path $ErrorFile -PathType Leaf))
        Write-Verbose -Message "Creating ErrorFile & logging time to it - $ErrorFile"
        New-Item -Path $ErrorFile -ItemType file | Out-Null
        Add-Content -Value "$("#"*40)$(Get-Date)$("#"*40)" -Path $ErrorFile
            Write-Verbose -Message " ErrorFile exists & logging time to it - $ErrorFile"
             Add-Content -Value "$("#"*40)$(Get-Date)$("#"*40)" -Path $ErrorFile
	foreach ($computer in $computername )
        #Create the WMI Hash here        
        $WMIHash = @{
            ComputerName = $Computer
            ErrorAction = 'Stop'
            Query= "SELECT LastBootUpTime FROM Win32_OperatingSystem"

        #Remove Credentials if the machine is local
        if ($Computer -ne $env:COMPUTERNAME)
            $WMIHash.Credential = $Credentials
		Write-Verbose -Message "Checking if $Computer is online"
		if (Test-Connection -ComputerName $Computer -Count 2 -Quiet)
            Write-Verbose -message "$Computer is online"
                $wmi = Get-WmiObject @WMIHash
                $uptime = (Get-Date) -  $($wmi.ConvertToDateTime($wmi.LastBootUpTime))
                    $hash = [ordered]@{ComputerName="$computer";
                            Uptime=$("{0} Days {1} Hours {2} Min {3} sec" -f $uptime.days,$uptime.hours,$uptime.Minutes,$uptime.Seconds )
                            LastReboot= $($wmi.ConvertToDateTime($wmi.LastBootUpTime)) }
                $Object = New-Object -TypeName PSObject -Property $hash
                Write-Output -InputObject $Object
                Write-Warning -Message " $computer :: $_.exception"
                Add-Content -Value "$computer :: $_.exception" -Path $ErrorFile
            Write-Host -ForegroundColor Red "$computer is Offline " #Write-host is not adviced as you can't capture it 
            #can better log this into a file using 
            Add-Content -Value "$computer :: Offline" -Path $ErrorFile
    Write-Verbose -Message "Ending the Function"