0

I've been trying to create a script that checks which applications are installed on a remote server. So far I've been able to get it working to do the job when everything is in place. However, one of my servers are a bit broken and the "uninstall" registry key is missing from the registry.

So before doing Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" and Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" I want to check if the registry key is present and if that isn't the case, I wish to run Get-WmiObject -Query "select * from win32_product"

Currently when I run it on the machine that is missing the "Uninstall" registry key I get

Cannot find path 'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (HKEY_LOCAL_MACH...rsion\Uninstall:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : ServerName001

Current script:

# Change to computer you wish to search.
$ComputerName = "ServerName001"
$ScriptPath = Get-Location

$GetWmiObject_Win32Product = $ScriptPath.ToString() + $ComputerName + "_Get-WmiObject-win32_Product.csv"
$GetItemProperty = $ScriptPath.ToString() + $ComputerName + "_Get-ItemProperty.csv"

Invoke-Command -ComputerName $ComputerName -ScriptBlock {
    $directoryInfo64bit = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Measure-Object
    $directoryInfo32bit = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Measure-Object

    $Apps = @()
    if($directoryInfo64bit.count -eq 0){
        Write-Output "No 64bit Uninstall folder"
    }else{
        $Apps += Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
    }
    if($directoryInfo32bit.count -eq 0){
        Write-Output "No 32bit Uninstall folder"
    }else{
        $Apps += Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
    }

    $Apps | Select-Object -Property DisplayName, Publisher, DisplayVersion, InstallDate, InstallLocation, PSComputerName
} | Export-Csv -Path $GetItemProperty -NoTypeInformation -Encoding utf8

Get-WmiObject -Query "select * from win32_product" -ComputerName $ComputerName | Select-Object -Property Name, Vendor, Version, PSComputerName | Export-Csv -Path $GetWmiObject_Win32Product -NoTypeInformation -Encoding utf8
zett42
  • 25,437
  • 3
  • 35
  • 72
duxck
  • 171
  • 1
  • 8
  • Why use the `Get-WmiObject` cmdlet at all when you can get all info from the registry? Have a look [here](https://stackoverflow.com/a/65215516/9898643) for instance – Theo Nov 23 '22 at 12:39
  • Another thought: If you only get the error for `HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\...`, then the machine might run a 32-bit Windows version, in which case `Wow6432Node` doesn't exist. There is nothing broken in this case, so you can simply exclude this case using `Test-Path` and continue to use `Get-ItemProperty` for the "normal" registry key. – zett42 Nov 23 '22 at 13:05
  • You can always use get-package to check for software installs. – js2010 Nov 23 '22 at 13:06
  • As a side note: [try avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. – iRon Nov 23 '22 at 13:35
  • @Theo want to use Get-WmiObject in the cases where the registry is FUBAR and uninstall information is missing. I.e. in my case the machine displays completely blanked if I go to programs and features to try and uninstall something. – duxck Nov 23 '22 at 14:25
  • Netbeans puts in an invalid registry key that gives an exception in get-itemproperty. – js2010 Nov 23 '22 at 15:51

0 Answers0