.net OpenRemoteBaseKey() 凭据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1133335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenRemoteBaseKey() credentials
提问by sgibbons
I'm attempting to use powershell to access a remote registry like so:
我正在尝试使用 powershell 访问远程注册表,如下所示:
$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."
使用“1”个参数调用“OpenSubKey”时出现异常:“不允许请求的注册表访问。”
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)
System.UnauthorizedAccessException: 试图执行未经授权的操作。在 Microsoft.Win32.RegistryKey.Win32ErrorStatic(Int32 errorCode, String str) 在 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 whichusers are allowed to access the registry remotely.
很明显,这是因为我正在运行 powershell 脚本的用户没有访问远程注册表的适当凭据。我希望能够提供一组用于远程注册表访问的凭据,但我找不到任何文档来说明如何执行此操作。我也不清楚具体在哪里指定允许哪些用户远程访问注册表。
回答by Ben Taylor
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:
只是想我也会向任何有此问题的人添加我的答案。似乎无法使用 RemoteRegistry 添加凭据。但是,您可以使用 WMI 使用备用凭据查询远程注册表,如下所示:
$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 :)
希望这对某人有所帮助:)
回答by ravikanth
Are you running remote registry service? It is disabled by default and that must be causing the issue. Check the status of this service on all remote machines you are trying to access.
您是否正在运行远程注册表服务?默认情况下它是禁用的,这一定是导致问题的原因。在您尝试访问的所有远程计算机上检查此服务的状态。
回答by ddncn
I couldn't comment directly on bentaylr's entry above, but I've taken what he contributed and added PSCredentials creation (figured out from here) to allow you to hard code credentials into the script.
我无法直接对上述 Bentaylr 的条目发表评论,但我已经采纳了他的贡献并添加了 PSCredentials 创建(从这里找出)以允许您将凭据硬编码到脚本中。
Peace of mind disclaimer: Be careful when using plaintext credentials in a script. In my case, I'm using generic credentials on machines I'm launching. Depending on your case, you might consider creating an encrypted credential file to store the password in (see link above).
安心免责声明:在脚本中使用纯文本凭据时要小心。就我而言,我在启动的机器上使用通用凭据。根据您的情况,您可能会考虑创建一个加密的凭证文件来存储密码(请参阅上面的链接)。
The credentials you use would need to be able to access the registry if you were logged into that user on the machine you are targeting.
如果您在目标计算机上登录到该用户,则您使用的凭据需要能够访问注册表。
$user = "Domain\Username"
$pass = ConvertTo-SecureString "Password" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$pass
$reg = Get-WmiObject -List -Namespace root\default -ComputerName $server -Credential $cred | Where-Object {$_.Name -eq "StdRegProv"}
$HKLM = 2147483650
$value = $reg.GetStringValue($HKLM,"Software\Microsoft\.NetFramework","InstallRoot").sValue
回答by Hansel
PS C:\>$regKey.OpenSubKey
OverloadDefinitions
重载定义
Microsoft.Win32.RegistryKey OpenSubKey(string name, **bool Writable**)
try
尝试
PS C:\>$key.OpenSubKey($subkeyName,**$true**)
http://msdn.microsoft.com/en-us/library/xthy8s8d%28v=vs.110%29.aspx
http://msdn.microsoft.com/en-us/library/xthy8s8d%28v=vs.110%29.aspx
回答by Wolfgang K
$key.OpenSubKey($subkeyName) opens the subkey in write protected mode, $key.OpenSubKey($subkeyName,$true) opens it in writable mode
$key.OpenSubKey($subkeyName) 在写保护模式下打开子键, $key.OpenSubKey($subkeyName,$true) 在可写模式下打开它
Therefore after $key.OpenSubKey($subkeyName,$true) you should be able to create a new subkey or value
因此,在 $key.OpenSubKey($subkeyName,$true) 之后,您应该能够创建一个新的子项或值
If you try the same thing after $key.OpenSubKey($subkeyName) you will get "UnauthorizedAccessException"
如果你在 $key.OpenSubKey($subkeyName) 之后尝试同样的事情,你会得到“UnauthorizedAccessException”
回答by pmorrisonfl
Came looking for the answer to your question, but in a little googling this morning I noticed that the first parameter is a type rather than a String... hope this helps:
来寻找您问题的答案,但今天早上在谷歌上搜索了一下,我注意到第一个参数是一个类型而不是一个字符串......希望这会有所帮助:
$machine = "<Machine Name Goes Here>"
$type = [Microsoft.Win32.RegistryHive]::LocalMachine
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type,$machine)
$subkey = $regKey.OpenSubKey($key)
foreach ($sub in $regKey.GetSubKeyNames()){$sub}
回答by user5790768
I wanted to first thank all for answers above really helpful, wanted to add that you can use Get-Credential command to collect credentials without having to hard code it in your script. I have written using the above suggestions into my script the following code and query:
我想首先感谢以上所有回答真的很有帮助,想补充一点,您可以使用 Get-Credential 命令来收集凭据,而无需在脚本中对其进行硬编码。我已使用上述建议将以下代码和查询写入脚本中:
$userCredentials = Get-Credential -Credential <domain\username>
$objReg = Get-WmiObject -List -Namespace root\default -ComputerName $server -Credential $userCredentials | Where-Object{$_.Name -eq "StdRegProv"}
$subKeyNames = $objReg.EnumKey($HKLM,"SOFTWARE\Microsoft\Updates\Microsoft .Net Framework 4.5.1").sNames
The above code returns all sub key names in the specified key so that I can determine installed updates other than OS which have been applied to a server. If you want to determine all collection possibilities with the $objReg variable then run:
上面的代码返回指定键中的所有子键名称,以便我可以确定已应用到服务器的操作系统以外的已安装更新。如果要使用 $objReg 变量确定所有收集的可能性,请运行:
$objReg | Get-Member
You will see a list of all possible queries which can be performed against the registry. Hope this helps!
您将看到可以针对注册表执行的所有可能查询的列表。希望这可以帮助!

