2
\$\begingroup\$

I'm new at PowerShell and I like it.

I had the change to write a script that uninstall Erlang and RabbitMQ and I want the same script to then remove the Erlang folder and the RabbitMQ folder.

But in order to do so I need the script to respect the tasks order.

My way out was to use Write-Verbose (line 13) so the script needs to wait for the task to end in order to pass to the next task:

# Let's set some verbosity
$VerbosePreference = 'Continue'

function Check_Program_Installed($programName){
    $check = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"  |
    Get-ItemProperty | Where-Object {$_.DisplayName -like "*$programName*" } | Select-Object -Property DisplayName, UninstallString

    If ($check.DisplayName) {
        $result = $check.DisplayName
        Write-Output "I found: $result, let's uninstall it!"
        $uninst = $check.UninstallString

        Write-Verbose $uninst
        Start-Process -FilePath $uninst -ArgumentList "/qn" -Wait #-NoNewWindow

        Check_Program_Installed($programName)
    }
    Else{
        Write-Output "$programName is not installed"
        }   
}

# Erlang check and Uninstall
Write-Output "Checking if Erlang exist" 
Check_Program_Installed("Erlang")
# RabbitMQ check and Uninstall
Write-Output "Checking if RabbitMQ exist" 
Check_Program_Installed("Rabbit")

# These actions need to be executed ONLY after Erlang and RabbitMQ uninstall
Write-Output "Then we do action 1" 
Start-Sleep -s 3
Write-Output "Then we do action 2" 
Start-Sleep -s 3
Write-Output "Then we do action 3" 
Start-Sleep -s 3
Write-Output "Then we do action 4" 
Start-Sleep -s 3
Write-Output "Then we do action 5" 

enter image description here

This is a very newbie approach but it's the only thing I came out with.

Is there a more elegant way to handle the task and avoid PowerShell to pass to the next task if the previous one isn't completed?

\$\endgroup\$
  • \$\begingroup\$ Can you show us the content of the UnInstall strings? \$\endgroup\$ – Theo Mar 3 '19 at 15:57
  • \$\begingroup\$ If the uninstall string has the \i switch, then I suggest replacing that by \x \$\endgroup\$ – Theo Mar 3 '19 at 16:41
  • \$\begingroup\$ @Theo, the content is "C:\Program Files\{your Erlang Version}\Uninstall.exe" \$\endgroup\$ – Francesco Mantovani Mar 9 '19 at 8:12
  • \$\begingroup\$ I'm afraid the /qn switch is for .msi files. In this case it is a .exe. Apparently, the Erlang Installer has a /S switch for silently install. Perhaps that also applies for the Uninstall.exe? There may be more switches possible. In a commandline you could try "C:\Program Files\{your Erlang Version}\Uninstall.exe" /? to hopefully get help about accepted switches. \$\endgroup\$ – Theo Mar 9 '19 at 9:47
  • \$\begingroup\$ Thank you @Theo but is not a silent installation what I need. I want the script not to pass to the next step while is uninstalling Erlang. For now I'm piping everything into a Write-Verbose but this is a mere workaround \$\endgroup\$ – Francesco Mantovani Mar 10 '19 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.