Worklet: Prompt User to Close Application to Apply Updates

When an applications must be closed in order to update (e.g. Zoom, Notepad++, SnagIT) the Automox agent will not update the application if it is open, nor will it auto-close the application. This is intentional to prevent you from randomly losing unsaved work or dropping from a Zoom call.

This worklet can be used in conjunction with a patch policy by configuring its schedule to run 5 minutes before the application-specific patch policy is set to run. This increases the likelihood that applications that cannot be running during an update are indeed closed.

Evaluation:

#Force the worklet to run remediation code
exit 1

Remediation:
#This worklet takes the specified process and checks if it is running. If it isn’t, exit quietly. If it is, prompt the user to close the application so that updates can be applied.

#Set the name of the application you want to affect
$process = "notepad"

#Check if $process is running. If it's running, ask the user to close it, if it isn't running then exit quietly.
$process_running = Get-Process $process -ErrorAction SilentlyContinue

if ( !$process_running ) {
	#If the process is not running then there's nothing to do. Exit quietly.
	 Write-Host "$process was not running: nothing to close." }
else {
#Play a sound with the pop-up message
[system.media.systemsounds]::Hand.play()

#Trigger the pop-up message with a Ok/Cancel option
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$UserResponse = [System.Windows.Forms.MessageBox]::Show("$process must be updated on your system. 

Please save your work and click OK to close the application." , "Status" , 1)

#If user clicked OK, try to close the app gracefully
    if ( $UserResponse -eq "OK" ) {
	    Write-Host "Closing $process application."
	    $process_running.CloseMainWindow()
	    
        ##Optional: Uncomment the stanza below to force kill the process after X seconds if graceful exit failed
	    #Sleep 60
	    #if ( !$process_running.HasExited ) {
	    #	$process_running | Stop-Process -Force }
    	} 
    else 
    #If user clicked Cancel, exit and do nothing
    { 
    Write-Host "$process update not applied: user chose to keep the app open." 
	Exit
    }
}
2 Likes