使用WMI获取注册表值

时间:2014-12-01 05:30:50

标签: powershell registry wmi

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

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()

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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

icm -ComputerName $endpoint -ScriptBlock {Get-ItemProperty HKLM:\software\7-zip\ |select path} 

答案 2 :(得分:0)

这两种方法都可以使用,但是在我使用measure-command时,我发现icm慢10倍,即0.47秒,而使用ICM则为4.8秒。