Skip to content

Instantly share code, notes, and snippets.

@signalwarrant
Last active October 4, 2021 18:07
Show Gist options
  • Save signalwarrant/5580a1de90917504c2f1771ef123623c to your computer and use it in GitHub Desktop.
Save signalwarrant/5580a1de90917504c2f1771ef123623c to your computer and use it in GitHub Desktop.
Force-WSUSchechin.ps1
# *** 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/
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
The computername can be one or many computer names
.EXAMPLE
.\force-WSUScheckin.ps1 -ComputerName CL1
.EXAMPLE
.\force-WSUScheckin.ps1 -ComputerName CL1 -verbose
.EXAMPLE
.\force-WSUScheckin.ps1 -ComputerName CL1, CL2 -verbose
.EXAMPLE
.\force-WSUScheckin.ps1 -ComputerName (Get-Content -Path "C:\computers.txt") -verbose
#>
[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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment