使用 WMI 获取注册表值 [英] Getting registry value using WMI

查看:46
本文介绍了使用 WMI 获取注册表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个异常,我在任何地方都找不到记录.这是一个例外:

I'm getting an exception which I can't find documented anywhere. Here is the exception:

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

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

The $inParams is null as well. and here is the code I'm using:

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

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

Does anyone know how to fix this?

推荐答案

如果您必须使用 WMI,请使用类似这样的内容(取自 脚本专家博客):

If you must use WMI use something like this (taken from the Scripting Guy blog):

$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

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

Personally I'd prefer using remote registry access, though:

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

这篇关于使用 WMI 获取注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆