Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenRemoteBaseKey() credentials

I'm attempting to use powershell to access a remote registry like so:

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $server)
$key = $reg.OpenSubkey($subkeyPath)

Depending on some factors that I'm not yet able to determine I either get

Exception calling "OpenSubKey" with "1" argument(s): "Requested registry access is not allowed."

Or

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. at Microsoft.Win32.RegistryKey.Win32ErrorStatic(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive hKey, String machineName)

It seems pretty clear that this is because the user I'm running the powershell script as doesn't have the appropriate credentials to access the remote registry. I'd like to be able to supply a set of credentials to use for the remote registry access, but I can find no documentation anywhere of a way to do this. I'm also not clear on exactly where to specify which users are allowed to access the registry remotely.

like image 382
sgibbons Avatar asked Jul 15 '09 19:07

sgibbons


Video Answer


1 Answers

Just thought I'd add my answer to anyone with this problem as well. It seems there is no way to add Credentials using RemoteRegistry. You can however use WMI to query a remote registry using alternative credentials as follows:

$reg = Get-WmiObject -List -Namespace root\default -ComputerName RemotePC -Credential "Domain\User" | Where-Object {$_.Name -eq "StdRegProv"}

From here you can call standard Registry methods. The below example will return the operating system.

$HKLM = 2147483650
$reg.GetStringValue($HKLM,"SOFTWARE\Microsoft\Windows NT\CurrentVersion","ProductName").sValue

Hope this helps someone :)

like image 168
Ben Taylor Avatar answered Sep 20 '22 14:09

Ben Taylor