Popular Posts

Sunday, September 2, 2018

PowerShell scripts to run Config Mgr actions for Machine policy update and software update cycles and check number of missing updates



Run CM actions script

function RecurssiveTimer([int]$time){

if ($time -eq 0){
return
}

write-host "$time*" -NoNewline

$time--

Start-sleep -s 60

RecurssiveTimer -time $time

}


function Run-CMactions{

Write-Host "##### Running Configuration Manager Client Actions #####"

try{

Write-Host "## Running Machine Policy Retrieval & Eval. Cycle ##"

Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule “{00000000-0000-0000-0000-000000000021}” | Out-null

Write-Host "Sleeping for 15 minutes..."

RecurssiveTimer -time 15

try{

Write-Host "## Running Software Updates Deployment Eval. Cycle ##"

Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule “{00000000-0000-0000-0000-000000000108}” | Out-null

Write-Host "## Running Software Updates Scan Cycle ##"

Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule “{00000000-0000-0000-0000-000000000113}” | Out-null

Write-Host "Sleeping for 5 minutes..."

RecurssiveTimer -time 5

return 0

}

catch{

throw

Write-Host -BackgroundColor Red "Error Running Software Update Cycles"

return -1

}

}

catch{

throw
Write-Host -BackgroundColor Red "Error Running Machine Policy Retrieval & Eval. Cycle"

return -1

}

}


Run-CMactions



Check compliance script

write-host "Checking compliance"

$Deployments = Get-WmiObject -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -Query "Select * from CCM_AssignmentCompliance" -ErrorAction SilentlyContinue

if($Deployments){

$Updatestargeted = Get-WmiObject -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -Query "Select * from CCM_TargetedUpdateEX1 where UpdateState = 0" -ErrorAction Stop

if($Updatestargeted){

$iMissing=0
$Updatestargeted | ForEach-Object {

$iMissing++

}
write-host -BackgroundColor Red "Number of Missing Updates: $iMissing"
write-host $iMissing
return $iMissing

}

else {

Write-Host -BackgroundColor Green -ForegroundColor Black "No Missing Software Updates Found"
return 0

}

}

else{

write-host -BackgroundColor Red "No Deployments Assigned"
return 0
}