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 {
    param(
        [string]$WindowTitle,
        [string]$InitialDirectory,
        [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.

param(
    [ValidateSet("local machine","remote machine")]
    [string]$GetKeysFrom
   )

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

gif1


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

Switch:
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.

If($Keys.name.count):
Check how many object names has been stored in $keys variable.

-EQ / -GT:
Comparison operators

Return:
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:

WordPress.com Logo

You are commenting using your WordPress.com 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.