Clean-up on script_install_updates.ps1

This commit is contained in:
Floris van Enter 2019-02-27 11:56:33 +01:00
parent 4f93611834
commit c405bdb54e

View File

@ -15,91 +15,91 @@ https://github.com/beakerflo/EnterMI
[CmdletBinding()] [CmdletBinding()]
param( param(
[boolean]$preventReboot = $False [boolean]$preventReboot = $False
) )
$updates = Get-WmiObject -Class CCM_SoftwareUpdate -Namespace root\CCM\ClientSDK | Where -FilterScript { $_.EvaluationState -eq "0" } $updates = Get-WmiObject -Class CCM_SoftwareUpdate -Namespace root\CCM\ClientSDK | Where -FilterScript { $_.EvaluationState -eq "0" }
$reboot = $False $reboot = $False
$rebootTimeout = Get-Random -Maximum 120 -Minimum 10 $rebootTimeout = Get-Random -Maximum 120 -Minimum 10
# exit script if no updates are found # exit script if no updates are found
If ( $updates -eq $Null ) { If ( $updates -eq $Null ) {
Write-Host "No updates found." Write-Host "No updates found."
break break
} }
Write-Host "" # line break Write-Host "" # line break
Write-Host "Available Updates:" Write-Host "Available Updates:"
$updateNames = $updates | Select -ExpandProperty Name $updateNames = $updates | Select -ExpandProperty Name
# display numbered list of update names # display numbered list of update names
foreach($updateName in $updateNames) { ForEach($updateName in $updateNames) {
Write-Host $updateName Write-Host $updateName
} }
Write-Host "" # line break Write-Host "" # line break
Write-Host "Beginning Installation" # shows that updates have started Write-Host "Beginning Installation" # shows that updates have started
Write-Host "" # line break Write-Host "" # line break
# declare array for failed updates # declare array for failed updates
$failedUpdates = @() $failedUpdates = @()
# formats updates by just getting those that are required (ComplianceState=0). Converts updates to WMI so that they can be installed. # formats updates by just getting those that are required (ComplianceState=0). Converts updates to WMI so that they can be installed.
ForEach ( $update in $updates ) { ForEach ( $update in $updates ) {
$f_update = @($update | ForEach-Object {if($_.ComplianceState -eq 0){[WMI]$_.__PATH}}) $f_update = @($update | ForEach-Object { if($_.ComplianceState -eq 0){[WMI]$_.__PATH} })
$installName = $update | Select Name $installName = $update | Select Name
# Installs updates # Installs updates
$uWmiOutput = "" $uWmiOutput = ""
$uWmiOutput = ([wmiclass]'ROOT\ccm\ClientSdk:CCM_SoftwareUpdatesManager').InstallUpdates($f_update) $uWmiOutput = ([wmiclass]'ROOT\ccm\ClientSdk:CCM_SoftwareUpdatesManager').InstallUpdates($f_update)
# wait until update process is seen, then disappears # wait until update process is seen, then disappears
Do { Do {
$upCheck = "" $upCheck = ""
$upCheck = Get-Process | Where -filterscript { $_.ProcessName -eq "wuauclt" } $upCheck = Get-Process | Where -filterscript { $_.ProcessName -eq "wuauclt" }
} Until ( $upCheck -ne $Null ) } Until ( $upCheck -ne $Null )
Wait-Process -Name wuauclt Wait-Process -Name wuauclt
Start-Sleep -s 2 Start-Sleep -s 2
# write result # write result
$s_install_name = "$installName" $s_install_name = "$installName"
$f_install_name = $s_install_name.substring(7).trim("}") $f_install_name = $s_install_name.substring(7).trim("}")
$eval_state = "" $eval_state = ""
$eval_state = (Get-WmiObject -Class CCM_SoftwareUpdate -Namespace root\CCM\ClientSDK | Where -filterscript { $_.Name -like "*$f_install_name*" }).EvaluationState $eval_state = (Get-WmiObject -Class CCM_SoftwareUpdate -Namespace root\CCM\ClientSDK | Where -filterscript { $_.Name -like "*$f_install_name*" }).EvaluationState
If ( $eval_state -eq "13") { If ( $eval_state -eq "13") {
$failedUpdates += $f_install_name $failedUpdates += $f_install_name
} Else { } Else {
Write-Host "Success: $f_install_name" -foregroundcolor green Write-Host "Success: $f_install_name" -ForegroundColor green
$reboot = $True $reboot = $True
} }
} }
If ( $failedUpdates -ne $Null ) { If ( $failedUpdates -ne $Null ) {
Write-Host "" Write-Host ""
Write-Host "Failed Updates:" Write-Host "Failed Updates:"
ForEach ($failedUpdate in $failedUpdates) { ForEach ($failedUpdate in $failedUpdates) {
Write-Host $failedUpdate -foregroundcolor red Write-Host $failedUpdate -ForegroundColor red
} }
} }
Write-Host "" # line break Write-Host "" # line break
# determine if there is a pending reboot # determine if there is a pending reboot
$u_reboot = [wmiclass]"\\localhost\root\ccm\ClientSDK:CCM_ClientUtilities" $u_reboot = [wmiclass]"\\localhost\root\ccm\ClientSDK:CCM_ClientUtilities"
$u_result = $u_reboot.DetermineIfRebootPending() | Select RebootPending $u_result = $u_reboot.DetermineIfRebootPending() | Select RebootPending
$u_p_reboot = $u_result.RebootPending $u_p_reboot = $u_result.RebootPending
# does check for any pending reboots (eval state 8) and alerts if they are required # does check for any pending reboots (eval state 8) and alerts if they are required
If ( ($u_p_reboot -eq $True) -and ($reboot = $True) -and ($preventReboot = $False) ) { If ( ($u_p_reboot -eq $True) -and ($reboot = $True) -and ($preventReboot = $False) ) {
Write-Host "Reboot Required." Write-Host "Reboot Required."
Write-Host "The server will reboot within $rebootTimeout seconds" Write-Host "The server will reboot within $rebootTimeout seconds"
Start-Sleep -s $rebootTimeout Start-Sleep -s $rebootTimeout
Restart-Computer Restart-Computer
} ElseIf ( ($u_p_reboot -eq $True) -and ($reboot = $True) -and ($preventReboot = $True) ) { } ElseIf ( ($u_p_reboot -eq $True) -and ($reboot = $True) -and ($preventReboot = $True) ) {
Write-Host "Reboot is Required." Write-Host "Reboot is Required."
Write-Host "Please reboot!!!" -ForegroundColor Red Write-Host "Please reboot!!!" -ForegroundColor Red
} Else { } Else {
Write-Host "Reboot not Required." Write-Host "Reboot not Required."
} }