[英]Editing Registry key on remote computer using Powershell
我正在尝试在运行Windows 7的远程VM上编辑注册表项值。我正在使用以下命令来执行此操作:
$RegistryBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', "WIN-MONKU")
$RegKey= $RegistryBase.OpenSubKey("SOFTWARE\lalaland\node")
$RegistryValue = $RegKey.GetValue("HostAddress")
Write-Host "HostAddress: $RegistryValue"
但是我收到以下错误信息:
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
在我看来,这似乎是授权问题。 我不确定应该在哪里提供凭据。
有任何想法吗 ?
您尝试将其包装到函数中,然后使用具有-Credential
参数的Invoke-Command
进行Invoke-Command
:
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)
让我知道您是否仍然遇到身份验证错误
你说...
'无法在远程计算机上调用Powershell。 我想从台式机访问注册表。”
但是,您的帖子标题显示...
“使用Powershell在远程计算机上编辑注册表项”。
因此,在本地PC上,您启用了Hyper-V,并且有Win7 guest虚拟机,因此这是一个远程主机。 假设您没有使用域部署,则需要使用PC和VM之间的工作组启用PSRemoting。
# configure the machine to allow access.
Enable-PSRemoting –force
如果计算机上的其中一张网卡的网络连接类型设置为“公共”,则在防火墙设置中不会打开所需的端口。 如果您不想更改网络连接类型,则必须手动配置防火墙以允许流量通过。 如果计划使用特定端口进行连接,请确保正确设置防火墙规则。 如果您仅使用默认端口,请参阅最近的博客文章以找出要打开的端口。
# 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
将服务器计算机的名称添加到WinRM配置中的TrustedHosts设置中,这使您的客户端计算机可以使用不对服务器进行身份验证的身份验证机制(例如Kerberos进行身份验证)连接到服务器计算机:
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
如果要使用服务器计算机的IP地址而不是其名称,则在连接时必须指定显式凭据。
请注意:通过将服务器添加到TrustedHosts列表,您可以将凭据信息发送到服务器而无需验证其身份。 仅当您知道从客户端计算机到服务器计算机的网络路径安全时,才将服务器添加到此列表中。
# 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*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.