jeudi 5 juillet 2018

Trying to use Powershell to Eliminate unwanted installs of software we use. (GUID)

I am attampting to create a script to uninstall unwanted instances (or old ones) of software that we use on our workstations. I can't seem to get the filtering right, though.

function Get-InstalledSoftware2 {
    [OutputType([System.Management.Automation.PSObject])]
    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Name , [string] $OurName
    )

    $UninstallKeys = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    $null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
    $UninstallKeys += Get-ChildItem HKU: -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' } | ForEach-Object { "HKU:\$($_.PSChildName)\Software\Microsoft\Windows\CurrentVersion\Uninstall" }
    if (-not $UninstallKeys) {
        Write-Verbose -Message 'No software registry keys found'
    } else {
        foreach ($UninstallKey in $UninstallKeys) {
#            if (!$PSBoundParameters.ContainsKey('OurName')) {
            if ($PSBoundParameters.ContainsKey('Name')) {
                $WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName') -like "$Name*") }
            } else {
                $WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName')) }
            }
            $gciParams = @{
                Path        = $UninstallKey
                ErrorAction = 'SilentlyContinue'
            }
            $selectProperties = @(
                @{n='GUID'; e={$_.PSChildName}}, 
                @{n='Name'; e={$_.GetValue('DisplayName')}}
            )
            Get-ChildItem @gciParams | Where $WhereBlock | Select-Object -Property $selectProperties
#            msiexec /x 'GUID' /qn /norestart
        }
    }
}
# }

Get-InstalledSoftware2 -Name 'ScreenConnect' -OurName 'ScreenConnect Client (b3d049b2cd879dd9)'

with the commands commented out, I get the following output:

GUID                                   Name                                   
----                                   ----                                   
{80E0C92B-A22E-4CCA-BB15-E7F8CAE95A96} ScreenConnect                          
{B92DB068-8FAF-4F4E-8ECC-13FF34DA74A5} ScreenConnect Client (b3d049b2cd879dd9)

But if I remove the hashes on the If statement, I get 0 output. Shouldn't I get the 1st result?

GUID                                   Name                                   
----                                   ----                                   
{80E0C92B-A22E-4CCA-BB15-E7F8CAE95A96} ScreenConnect                          

Thanks, everyone!

-Dave

Aucun commentaire:

Enregistrer un commentaire