Software Uninstall Automation [Part 2/3]

  • Get computer list values.
  • Get WMI key values: Parameters.
  • Get WMI key values: Switch parameters.
  • Build classkey.

In the part PART 1/3 we learned how to speed up a WMI query to quickly find and uninstall a specific software in our infrastructure, now we will build a script to run it in the infrastructure.

I’ll assume that you already have your computer list file, so we’ll build a function called OpenFileDialog which will prompt us a dialog where we can select our file, then we’ll use this function later in our script.

function OpenFileDialog {
        [string]$Filter = "All files (*.*)|*.*"

    Add-Type -AssemblyName System.Windows.Forms
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = $WindowTitle
    $OpenFileDialog.InitialDirectory = $InitialDirectory
    $OpenFileDialog.Filter = $Filter
    $OpenFileDialog.ShowHelp = $true
    $OpenFileDialog.ShowDialog() > $null
    return $openFileDialog.Filename

Get WMI key values: Parameters

In the first part of this article, we’ve already seen how to build the classkey, but we still need to automate the process of getting the key values to build the classkey after.

We know that we can get the software wmi keys from our local machine or remote machine.

    [ValidateSet("local machine","remote machine")]

by using ValidateSet we can limit the possible parameters values for our parameter
and get advantage of intellisense which will recognize it.


Get WMI key values: Switch Parameters

Since we have more than one parameter option, the script must know what to do with each possibility.

    switch($GetKeysFrom) {
        'Local Machine' {
            $Keys = gwmi win32_product | ? {$ -like "*$Software*"} | select name, version, id*
                if($ -eq 1){
                    Write-Host "`n$($Keys.Name) keys successfully captured on local machine" -ForegroundColor Green
                elseif($ -gt 1){
                    Write-Host "`nMore than one key found for '$Software', try to better specify the software name next time!`n" -ForegroundColor Yellow
                    Write-Host "`n$Software keys not found on local machine!" -ForegroundColor Red
        'Remote Machine' {
            $Keys = gwmi win32_product -CN $ComputerName | ? {$ -like "*$Software*"} | select name, version, id*
                    Write-Host "`n$($Keys.Name) keys successfully captured on $ComputerName" -ForegroundColor Green
                    Write-Host "`n$Software keys not found on $ComputerName!" -ForegroundColor Red
    }<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Handle multiple if statements, we could use if/else but,
for this purpose the switch statement is a best practice
and better to read, you can use if statements inside of it though.

Check how many object names has been stored in $keys variable.

-EQ / -GT:
Comparison operators

Exit the current scope, which can be a function, script,
or script block.

Build classkey

If the $keys variable returns true, then the script is ready to build the classkey like we’ve already seen in the part 1/3

$IdentifyingNumber = "IdentifyingNumber=`"$($Keys.IdentifyingNumber)`","
$Name = "Name=`"$($Keys.Name)`","
$Version = "Version=`"$($Keys.Version)`""
$classKey = "$IdentifyingNumber$Name$Version"

This was the second part of this article, I can’t wait to bring you the last part tomorrow!
Please feel free to leave a coment, follow and share!

Some of the skills presented here I’ve learned with Adam Bertram from ADAM THE AUTOMATOR.  
Be sure to check out his great content!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.