How to create and use custom PowerShell Functions

#  *** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ***
<#

.DESCRIPTION
	Starts the Windows Update service (wuauserv) if it is stopped and forces a checkin 
    with the WSUS Server. This function uses the Invoke-Command CMDlet which 
    will require PSRemoting to be enabled on the target machine. 

.NOTES
	File Name: force-WSUScheckin.ps1
	Author: David Hall
	Contact Info: 
		Website: www.signalwarrant.com
		Twitter: @signalwarrant
		Facebook: facebook.com/signalwarrant/
		Google +: plus.google.com/113307879414407675617
		YouTube Subscribe link: https://www.youtube.com/channel/UCgWfCzNeAPmPq_1lRQ64JtQ?sub_confirmation=1
	Requires: PowerShell Remoting Enabled (Enable-PSRemoting) 
	Tested: PowerShell Version 5

.PARAMETER ComputerName
    See the examples below, the computername can be one or 
    many computer names
		 
.EXAMPLE
     .\force-WSUScheckin.ps1 -ComputerName CL1
     .\force-WSUScheckin.ps1 -ComputerName CL1 -verbose
     .\force-WSUScheckin.ps1 -ComputerName CL1, CL2 -verbose
     .\force-WSUScheckin.ps1 -ComputerName (Get-Content -Path "C:\computers.txt") -verbose

#>

Function Force-WSUSCheckin
{
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [string[]]$ComputerName

)

$service = get-service -Name wuauserv


# Check to see if the wuauserv service is stopped
if ($service.Status -eq "Stopped"){

    # If the service is stopped we're going to start it and force WSUS checkin
    # then Exit
    Write-verbose "1. WUAUSERV is stopped... starting"
    Invoke-Command -ComputerName $ComputerName -scriptblock {Start-Service wuauserv} 
        [System.Threading.Thread]::Sleep(3000)
    
    Write-verbose "2. Forcing WSUS Checkin"
    Invoke-Command -ComputerName $ComputerName -scriptblock {wuauclt.exe /detectnow}
        [System.Threading.Thread]::Sleep(1500)
    
    Write-verbose "3. Checkin Complete"
    Exit  

    } else {

    # If the service is started we'll just force the WSUS checkin and Exit
    Write-verbose "1. Forcing WSUS Checkin"
    Invoke-Command -ComputerName $ComputerName -scriptblock {wuauclt.exe /detectnow}
        [System.Threading.Thread]::Sleep(1500)
    
    Write-Verbose "2. Checkin Complete"
    Exit

    }
}

 

Website: www.signalwarrant.com
Twitter: @signalwarrant
Facebook: facebook.com/signalwarrant/
Google +: plus.google.com/113307879414407675617