This is a great script that I've used several times to install the available updates on a list of servers.
Instructions based on: https://gallery.technet.microsoft.com/SCCM-Configmgr-Powershell-ebbb2c0e
Step 1: Create a Clients.txt file that has a list of computers/servers on it that you want to install updates on
Step 2: Create a Powershell script in the same directory. Copy/paste this command in and save it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <#
AUTHOR : Eswar Koneti
DATE : 14-Nov-2016
COMMENT : This script check and install the software updates available in
software center on clients remotly with nice logging info
VERSION : 1.0
#>
# Determine script location
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$log = "$ScriptDir\InstallUpdates.log"
$date = Get-Date -Format "dd-MM-yyyy hh:mm:ss"
# Get list of clients from notepad
"--------------------- Script executed on $date (DD-MM-YYYY hh:mm:ss) ---------------------" + "`r`n" | Out-File $log -append
ForEach ($system in Get-Content $ScriptDir"\clients.txt")
{
$wmicheck=$null
$wmicheck =Get-WmiObject -ComputerName $system -namespace root\cimv2 -Class Win32_BIOS -ErrorAction SilentlyContinue
if ($wmicheck)
{
# Get list of all instances of CCM_SoftwareUpdate from root\CCM\ClientSDK for missing updates https://msdn.microsoft.com/en-us/library/jj155450.aspx?f=255&MSPPError=-2147217396
$TargetedUpdates= Get-WmiObject -ComputerName $system -Namespace root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter ComplianceState=0
$approvedUpdates= ($TargetedUpdates |Measure-Object).count
$pendingpatches=($TargetedUpdates |Where-Object {$TargetedUpdates.EvaluationState -ne 8} |Measure-Object).count
$rebootpending=($TargetedUpdates |Where-Object {$TargetedUpdates.EvaluationState -eq 8} |Measure-Object).count
if ($pendingpatches -gt 0)
{
try {
$MissingUpdatesReformatted = @($TargetedUpdates | ForEach-Object {if($_.ComplianceState -eq 0){[WMI]$_.__PATH}})
# The following is the invoke of the CCM_SoftwareUpdatesManager.InstallUpdates with our found updates
$InstallReturn = Invoke-WmiMethod -ComputerName $system -Class CCM_SoftwareUpdatesManager -Name InstallUpdates -ArgumentList (,$MissingUpdatesReformatted) -Namespace root\ccm\clientsdk
"$system,Targeted Patches :$approvedUpdates,Pending patches:$pendingpatches,Reboot Pending patches :$rebootpending,initiated $pendingpatches patches for install" | Out-File $log -append
}
catch {"$System,pending patches - $pendingpatches but unable to install them ,please check Further" | Out-File $log -append }
}
else {"$system,Targeted Patches :$approvedUpdates,Pending patches:$pendingpatches,Reboot Pending patches :$rebootpending,Compliant" | Out-File $log -append }
}
else {"$system,Unable to connect to remote system ,please check further" | Out-File $log -append }
}
|
Step 3: Run the script and it will kick off the installation of any software updates in the software center on that list of computers in Clients.txt
BONUS!
This script reboots all machines listed in clients.txt. Use with Caution!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <#
AUTHOR : Nickolas Golubev
DATE : 04-Dec-2017
COMMENT : This script reboots all machines listed in clients.txt
VERSION : 1.0
#>
Write-Host "*** WARNING *** This script will reboot all servers listed in clients.txt!"
function Get-SomeInput {
$input = read-host "Please write 'yes' to continue or 'no' to exit and press Enter"
switch ($input) `
{
'yes' {
write-host 'You wrote yes, continuing'
}
'no' {
write-host 'You wrote no, exiting'
exit
}
default {
write-host 'You may only answer yes or no, please try again.'
Get-SomeInput
}
}
}
Get-SomeInput
# Determine script location
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$log = "$ScriptDir\reboot.log"
$date = Get-Date -Format "dd-MM-yyyy hh:mm:ss"
# Get list of clients from notepad
"--------------------- Script executed on $date (DD-MM-YYYY hh:mm:ss) ---------------------" + "`r`n" | Out-File $log -append
ForEach ($system in Get-Content $ScriptDir"\clients.txt")
{
"Rebooting machine: $system" | Out-File $log -append
Try {
Restart-Computer -ComputerName $system -Force -ErrorAction Stop
}
Catch {
"Error detected: $_" | Out-File $log -append
}
}
|
Sources
⦁ https://golubev.org/?p=141
⦁ https://gallery.technet.microsoft.com/SCCM-Configmgr-Powershell-ebbb2c0e
⦁ http://eskonr.com/2016/11/sccm-configmgr-powershell-script-to-install-software-updates-on-remote-clients/
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.