我有一个脚本,它通过使用注册表项从所有服务器获取安装的软件版本,但是我遇到了以下错误。对不起,我是PowerShell的新手。感谢你的帮助。
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
我的脚本如下
## 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,DisplayName,DisplayVersion,Publisher Select-Object -Property ComputerName -Property Out-File InstalledSoftware.txt
发布于 2020-12-08 04:36:06
OpenRemoteBaseKey有两个重载,两个参数都没有。
OverloadDefinitions
-------------------
static Microsoft.Win32.RegistryKey OpenRemoteBaseKey(Microsoft.Win32.RegistryHive hKey,
string machineName)
static Microsoft.Win32.RegistryKey OpenRemoteBaseKey(Microsoft.Win32.RegistryHive hKey,
string machineName, Microsoft.Win32.RegistryView view)
示例
$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',SomeComputerName)
如果您不是在查询远程计算机,则使用OpenBaseKey
https://stackoverflow.com/questions/65192699
复制相似问题