简体   繁体   中英

Editing Registry key on remote computer using Powershell

I am trying to edit a registry key value on a remote VM running Windows 7. I am using the following commands to do that:

$RegistryBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', "WIN-MONKU")
$RegKey= $RegistryBase.OpenSubKey("SOFTWARE\lalaland\node")
$RegistryValue = $RegKey.GetValue("HostAddress")
Write-Host "HostAddress: $RegistryValue"

But I am getting errors as:

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "Attempted to perform an unauthorized operation."
At D:\workspace\Scripts\Update.ps1:35 char:1
+ $RegistryBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Loc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException

You cannot call a method on a null-valued expression.
At D:\workspace\Scripts\Update.ps1:36 char:1
+ $RegKey= $RegistryBase.OpenSubKey("SOFTWARE\lalaland\node")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\workspace\Scripts\Update.ps1:37 char:1
+ $RegistryValue = $RegKey.GetValue("HostAddress")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

To me it looks like an issue with authorization. I am not sure where I should provide the credentials.

Any ideas ?

You try wrapping that into a function and calling it with Invoke-Command which has a -Credential parameter:

Function Get-RemoteRegistryKey()
{
    $RegistryBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', "WIN-MONKU")
    $RegKey= $RegistryBase.OpenSubKey("SOFTWARE\lalaland\node")
    $RegistryValue = $RegKey.GetValue("HostAddress")
    Write-Host "HostAddress: $RegistryValue"
}

Invoke-Command { Get-RemoteRegistryKey } -ComputerName 'WIN-MONKU' -Credential $(Get-Credential)

Let me know if you get auth errors still

You say...

'can't invoke powershell on remote computer. I want to access registry from my desktop pc.'

Yet, your post title says...

'Editing Registry key on remote computer using Powershell'.

So, on your local PC, you have Hyper-V enabled and you have a Win7 guest, thus, this is a remote host. Making this assumption, you are not using a domain deployment, you need to enable PSRemoting using workgroup between you PC and your VM.

PowerShell remoting between two workgroup machines

# configure the machine to allow access.
Enable-PSRemoting –force

If one of the network cards on your computer has the network connection type set to “Public” then the required port won't be opened in your firewall settings. If you'd rather not change your network connection type, you'll have to manually configure your firewall to allow traffic through. If you plan on connecting using a specific port, be sure to set your firewall rules appropriately. If you're just using the default ports, see this recent blog post to figure out which ports to open.

# configure the client machine.
Start-Service WinRM
Set-ItemProperty –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System –Name  LocalAccountTokenFilterPolicy –Value 1 –Type DWord

# running on Windows XP
Set-ItemProperty –Path HKLM:\System\CurrentControlSet\Control\Lsa –Name ForceGuest –Value 0

add the name of your server machine to the TrustedHosts setting in the WinRM configuration, which enables your client machine to connect to your server machine using an authentication mechanism that does not authenticate the server (like Kerberos does):

Set-Item WSMan:\localhost\Client\TrustedHosts –Value <ServerMachineName> -Force

# If there is an existing list of servers 
Set-Item WSMan:\localhost\Client\TrustedHosts –Value <ServerMachineName> -Force -Concatenate 

If you want to use your server machine's IP address instead of its name, you must specify explicit credentials when you connect.

A word of caution: by adding a server to the TrustedHosts list, you are allowing your credential information to be sent to a server without verifying its identity. Only add a server to this list if you know that the network path from your client machine to the server machine is secure.

# check if the WinRM service is running:
Get-Service WinRM
Test-WSMan –Auth default
winrm enumerate winrm/config/listener

# check the remoting configuration
Get-PSSessionConfiguration
New-PSSession

# check if the local account token filter policy is enabled 
Get-ItemProperty –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System –Name LocalAccountTokenFilterPolicy*

# check if the network access policy
Get-ItemProperty –Path HKLM:\System\CurrentControlSet\Control\Lsa –Name ForceGuest*

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