简体   繁体   中英

Getting registry value using 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".

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?

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)

don't you over complicate things ? all of these to read a value from registry ? what about someting like that

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

两种方法都有效,但使用 measure-command 我发现 icm 慢 10 倍,0.47 秒与使用 ICM 的 4.8 秒的示例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM