首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PowerShell中使用注册表项获取软件版本

在PowerShell中使用注册表项获取软件版本
EN

Stack Overflow用户
提问于 2020-12-08 03:33:08
回答 1查看 304关注 0票数 0

我有一个脚本,它通过使用注册表项从所有服务器获取安装的软件版本,但是我遇到了以下错误。对不起,我是PowerShell的新手。感谢你的帮助。

代码语言:javascript
运行
复制
   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

我的脚本如下

代码语言:javascript
运行
复制
## 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-08 04:36:06

OpenRemoteBaseKey有两个重载,两个参数都没有。

代码语言:javascript
运行
复制
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)  

示例

代码语言:javascript
运行
复制
$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',SomeComputerName)

如果您不是在查询远程计算机,则使用OpenBaseKey

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65192699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档