Darth Vagrant
Darth Vagrant

Reputation: 139

Registry wildcard use

I have a script to check on uninstall status of a given software:

$pathtofile = Get-Content ComputersToCheckRegistryAndInstallationPath.txt
$registrykey = "Snagit 10.0.2 [LC 01.01 AP] EN"
$installationpath = "Program Files (x86)\TechSmith"

foreach ($name in $pathtofile) {
    if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue) {
        $name

        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $name)
        $regkey = $reg.OpenSubKey("SOFTWARE\\Wow6432Node\\Total\\Software\\$registrykey")
        if ($regkey) {
            Write-Output  "$registrykey Values are: " $regkey.GetValueNames()
        } else {
            "No $registrykey Values found"
        }
        if ($regkey) {
            Write-Output "InstallStatus Value (void if none): " $regkey.GetValue('InstallStatus')
        } else {
            "No InstallStatus value found"
        }
        if ($regkey) {
            Write-Output "UninstallStatus Value (void if none): " $regkey.GetValue('UninstallStatus')
        } else {
            "No UninstallStatus value found"
        }

        $path = Test-Path -Path "\\$name\C$\$installationpath"

        if ($path -eq $true) {
            Write-Output $name "$installationpath exists"
        } else {
            Write-Output "$installationpath does not exist"
        }
    } else {
        Write-Output $name
    }

It works fine.

My only issue is that for each software we have different packages and I would like to use a wildcard such as (for the example at hand):

$registrykey = "Snagit *"

instead of:

$registrykey = "Snagit 10.0.2 [LC 01.01 AP] EN"

I cannot use the Get and Invoke-Command methods since PSRemoting is not activated on our remote machines.

From what I have gathered, there is no way around this but maybe someone has found a workaround in the meanwhile?

Upvotes: 1

Views: 1004

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200543

Enumerate the subkey names of the Software key and filter that list for names matching your pattern.

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $name)
$basekey = $reg.OpenSubKey("SOFTWARE\\Wow6432Node\\Total\\Software")
$subkey = $basekey.GetSubkeyNames() | Where-Object { $_ -like $registrykey }
$regkey = $baskey.OpenSubKey($subkey)

Upvotes: 2

Related Questions