Package step dynamic based on powershell exit code

Answered

Comments

2 comments

  • Mike Nagel

    Basically I wanted to only display a message if the user had the program open and only continue if the user closed the program or after a timeout. After the program was closed I wanted to uninstall the program then install the new version of the program. This is what I came up with. I created a powershell step and put in the code listed below. You will need to run this step as the logged in user. If the user is logged in and has the program open it will still give the message box and when it times out it will kill the program and continue. I also set the Exit code to 0 so we know that the script has finished successfully and PDQ should continue to the next step in the package.

    This is should be set as the process name when the program is open. get-process -name "x"

    $ProgramProcessName = 'Project Databases'

    Setting values to null

    $Result = $null

    #checking to see if the user has the program open
    $Process = Get-Process -Name $ProgramProcessName -ErrorAction SilentlyContinue

    Doing something based on if the program is open or not

    if($Process -eq $null){ Write-Output "Process is not running" Exit 0 }
    Else{ $date = (Get-Date).ToString() $MessageboxTitle = “Message from $((Get-Date).ToString())” $Messageboxbody = “The $($ProgramProcessName) program will be updated in 3 minutes please save all work and exit out of the program then click ok to continue” $popup = new-object -comobject wscript.shell #Displaying window and saving what the user clicked. The 180=3 minutes and the 0 will just show the OK button. $Result = $popup.popup($Messageboxbody,180,$MessageboxTitle,0) if ($Result -eq 1){ # 1 means the user clicked OK or closed the window # Making sure user closed the program Stop-Process -Name $ProgramProcessName -Force -Confirm:$false -ErrorAction SilentlyContinue Exit 0 } if ($Result -eq -1){ # -1 means the user didn't click OK or the popup hit it's timeout value # Making sure user closed the program Stop-Process -Name $ProgramProcessName -Force -Confirm:$false -ErrorAction SilentlyContinue Exit 0 } }

    0
    Comment actions Permalink
  • Margus Kamenjuk

    I am using Step 1 before any deployment easy check e.g. Chrome in this case (options - quit deployment with error) :

    $Processes = Get-Process
    if ( $Processes.ProcessName -contains "chrome" ) {
        Write-Output "Process Found - stopping"
        Exit 1
    } Else {
        Write-Output "Process Not Found"
    Exit 0
    }
    

    Depends of your strategy you can in your message step ask user to close needed program also after 5 min etc and / or just in case add before install step small kill process command in your deployment like:

    Get-Process | Where-Object { $_.Name -contains "chrome" } | Stop-Process -Force -Verbose
    
    0
    Comment actions Permalink

Please sign in to leave a comment.