Get-WmiObject returns with Access Denied error

0

I am trying to edit a registry value on a remote VM running Windows 7. I am using the following code to edit it:

$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"
}

The registry key path and credential are all correct but still I am getting the following error message:

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
powershell
asked on Stack Overflow Nov 27, 2018 by Monku • edited Nov 27, 2018 by Monku

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0