I have a script that gets the installed software version from all of the servers by using the registry keys but I am encountering the following errors. Sorry Im new to PowerShell. Appreciate the help.
Cannot find an overload for "OpenRemoteBaseKey" and the argument count: "1".
At C:\Users\P1334126\Scripts\GetInstalledSoftware.ps1:12 char:5
+ $reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
You cannot call a method on a null-valued expression.
At C:\Users\P1334126\Scripts\GetInstalledSoftware.ps1:16 char:5
+ $regkey = $reg.OpenSubKey($UninstallKey)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\P1334126\Scripts\GetInstalledSoftware.ps1:20 char:5
+ $subkeys = $regkey.GetSubKeyNames()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
My script is the following
## Include CSV file of all computers with header "pc"
$computers = Import-Csv "C:\Users\P1334126\Documents\Test.CSV"
$array = @()
#Define the variable to hold the location of Currently Installed Programs
foreach($pc in $computers){
$computername=$pc.computername
$UninstallKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
#Create an instance of the Registry Object and open the HKLM base key
$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine')
#Drill down into the Uninstall key using the OpenSubKey Method
$regkey = $reg.OpenSubKey($UninstallKey)
#Retrieve an array of string that contain all the subkey names
$subkeys = $regkey.GetSubKeyNames()
#Open each Subkey and use GetValue Method to return the required values for each
foreach($key in $subkeys){
$thisKey=$UninstallKey + '\\' + $key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name 'DisplayName' -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name 'DisplayVersion' -Value $($thisSubKey.GetValue("DisplayVersion"))
$obj | Add-Member -MemberType NoteProperty -Name 'Publisher' -Value $($thisSubKey.GetValue("Publisher"))
$array += $obj
}
}
$array | Select-Object ComputerName, DisplayName, DisplayVersion, Publisher | Sort-Object -Property ComputerName | Out-File InstalledSoftware.txt