簡體   English   中英

使用 WMI 獲取注冊表值

[英]Getting registry value using 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()

有誰知道如何解決這一問題?

如果您必須使用 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)

你不會把事情復雜化嗎? 所有這些都是為了從注冊表中讀取一個值? 這樣的事情怎么樣

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

兩種方法都有效,但使用 measure-command 我發現 icm 慢 10 倍,0.47 秒與使用 ICM 的 4.8 秒的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM