如何使用powershell遍历远程注册表项? [英] How to iterate through remote registry keys using powershell?
问题描述
我有一个 powershell 脚本,它遍历注册表项中的名称值对列表并进行一些操作.所有这些都发生在下面的 foreach-object 循环中
I have a powershell script that goes through a list of name value pairs in a registry key and do some maniuplations. All of this happens inside the below foreach-object loop
Get-ChildItem "HKLM:\SOFTWARE\PathA\pathB" -Recurse | ForEach-Object {
$regkey = (Get-ItemProperty $_.PSPath) | Where-Object { $_.PSPath -match 'debug' }
if ($masterList.Contains($_.Name)) #Check if the reg key is in master list
{
Set-ItemProperty -Path $regkey.PSPath -Name $_.Name -Value 1
}
}
这在我的本地机器上工作得很好.我必须做同样的事情,但在远程机器注册表上.如上所示,我如何迭代?
This works perfectly fine in my local machine. I have to do the same but on remote machines registry. How can i iterate through as seen above?
我试过了:
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
$key="HKLM:\SOFTWARE\PathA\pathB"
foreach ($subkey in $reg.OpenSubKey($key).GetSubKeyNames())
{
}
但这只会返回注册表文件夹的名称,我无法遍历它
But this will just return me the name of the registry folderand i am not able to iterate through it
推荐答案
Invoke-Command 应该做你想做的.像这样:
Invoke-Command should do what you want. Something like this:
Invoke-Command -ComputerName DC1 -ScriptBlock {
Get-ChildItem "HKLM:\SOFTWARE\PathA\pathB" -Recurse | ForEach-Object {
$regkey = (Get-ItemProperty $_.PSPath) | Where-Object { $_.PSPath -match 'debug' }
if ($Using:masterList.Contains($_.Name)) #Check if the reg key is in master list
{
Set-ItemProperty -Path $regkey.PSPath -Name $_.Name -Value 1
}
}
}
由于您在本地定义了 $masterlist,您可以使用 $Using:masterlist - 请参阅 远程变量
Since you have $masterlist defined locally, you can use $Using:masterlist - see Remote Variables
您还应该考虑使用 PSSessions
这篇关于如何使用powershell遍历远程注册表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!