我有一个 powershell 脚本,它通过注册表项中的名称值对列表并执行一些 maniupulations。所有这些都发生在下面的 foreach-object 循环中
Get-ChildItem "HKLM:\SOFTWARE\PathA\pathB" -Recurse | ForEach-Object {
$regkey = (Get-ItemProperty $_.PSPath) | Where-Object { $_.PSPath -match 'debug' }
if ($List.Contains($_.Name)) #Check if the reg key is in list
{
Set-ItemProperty -Path $regkey.PSPath -Name $_.Name -Value 1
}
}
这在我的本地机器上工作得很好。我必须做同样的事情,但在远程机器注册表上。
我尝试过:
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
$key="HKLM:\SOFTWARE\PathA\pathB"
foreach ($subkey in $reg.OpenSubKey($key).GetSubKeyNames())
{
}
但这只会返回注册表文件夹的名称,我无法遍历它
Invoke-Command应该做你想做的。像这样:
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:List.Contains($_.Name)) #Check if the reg key is in list
{
Set-ItemProperty -Path $regkey.PSPath -Name $_.Name -Value 1
}
}
}
由于您在本地定义了 $list,因此可以使用 $Using:list-请参阅Remote Variables
您还应该考虑使用PSSessions

这种方法呢?
应该递归地获取远程计算机上的所有子键 & amp;& amp;root_key:
Function remote_registry_query($target, $key)
{
Try {
$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $target)
ForEach ($sub in $registry.OpenSubKey($key).GetSubKeyNames())
{
$subkey = $registry.OpenSubKey("$($key)\$($sub)")
ForEach ($value in $subkey.GetValueNames())
{
Write-Output $subkey.GetValue($value)
}
remote_registry_query -target $computer -key "$($key)\$($sub)"
}
} Catch [System.Security.SecurityException] {
"Registry - access denied $($key)"
} Catch {
$_.Exception.Message
}
}
$computer = "remote computer name"
$root_key = 'HKLM:\SOFTWARE\PathA\pathB'
remote_registry_query -target $computer -key $root_key
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(56条)