使用WMI获取注册表值

问题描述 投票:0回答:2

我得到了一个我无法在任何地方找到的例外情况。这是一个例外:

The following exception occurred while retrieving the type name hierarchy: "Not found".

$inParams也是空的。这是我正在使用的代码:

$endpoint = "someEndpointName"
$connectionOptions = New-Object 'System.Management.ConnectionOptions'

$scope = New-Object 'System.Management.ManagementScope'("\\$endpoint\root\cimv2", $connectionOptions)
$scope.Connect()

$registry = New-Object 'System.Management.ManagementClass'($scope, (New-Object 'System.Management.ManagementPath'("StdRegProv")), $null) 
$registrysType = $registry.GetType().Name
#The line above throws this exception: "The following exception occurred while retrieving the type name hierarchy: "Not found".

$inParams = $registry.GetMethodParameters("GetStringValue")
$inParams["hDefKey"] = 2147483650 #this represents HKEY_LOCAL_MACHINE
$inParams["sSubKeyName"] = "SOFTWARE\Company\EOS Version"
$inParams["sValueName"] = "Build S Version"

$outParams = $registry.InvokeMethod("GetStringValue", $inParams, $null)

$buildSVersion = $outParams.Properties["sValueText"].Value.ToString()

有谁知道如何解决这一问题?

powershell registry wmi
2个回答
1
投票

如果你必须使用WMI,请使用类似的东西(取自Scripting Guy博客):

$endpoint = 'someEndpointName'

$basekey  = [uint32]'0x80000002'
$subkey   = 'SOFTWARE\Company\EOS Version'
$value    = 'Build S Version'

$reg = [wmiclass]"\\$endpoint\root\default:StdRegProv"
$buildSVersion = $reg.GetStringValue($basekey, $subkey, $value).sValue

我个人更喜欢使用远程注册表访问:

$endpoint = 'someEndpointName'

$basekey  = 'LocalMachine'
$subkey   = 'SOFTWARE\Company\EOS Version'
$value    = 'Build S Version'

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($basekey, $endpoint)
$key = $reg.OpenSubKey($subkey, $true)
$buildSVersion = $key.GetValue($value)

0
投票

难道你不复杂的事情?所有这些从注册表中读取值?怎么样那样

icm -ComputerName $endpoint -ScriptBlock {Get-ItemProperty HKLM:\software\7-zip\ |select path} 
© www.soinside.com 2019 - 2024. All rights reserved.