我正在尝试在运行Windows 7的远程VM上编辑注册表值。 我正在使用以下代码对其进行编辑:
$password = "<password>" | ConvertTo-SecureString -asPlainText -Force
$username = "<username>"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$hklm = 2147483650
$path = "SOFTWARE\crat\ler"
$key = "HostAddress"
$wmi = Get-WmiObject -List "StdRegProv" -Namespace root\default -ComputerName <IPAddress> -Credential $credential
if($wmi)
{
$value = ($wmi.GetStringValue($hklm,$path,$key)).svalue
Write-Host "Registry Value: $value"
}
else
{
Write-Host "Failed to get registry value"
}
注册表项路径和凭据都正确,但仍然收到以下错误消息:
Get-WmiObject : Could not get objects from namespace root\default. Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At D:\workspace\scripts\Update.ps1:29 char:8
+ $wmi = Get-WmiObject -List "StdRegProv" -Namespace root\default -Comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : INVALID_NAMESPACE_IDENTIFIER,Microsoft.PowerShell.Commands.GetWmiObjectCommand