运行 使用 PowerShell 远程注册文件

Run Registry File Remotely with PowerShell

我正在使用以下脚本在多个远程系统上 运行 test.reg

$computers = Get-Content computers.txt

Invoke-Command -ComputerName $computers -ScriptBlock {
  regedit /i /s "\SERVER\C$\RegistryFiles\test.reg"
}

脚本没有错误,但注册表项没有导入任何系统。

我知道 test.reg 文件是一个有效的注册表文件,因为我复制了它,运行 手动复制了它,然后导入了注册表项。我还确保在远程计算机上启用了 PowerShell Remoting。

知道注册表项未导入的原因吗?

我在一些 PowerShell 论坛上发帖,终于成功了。

我必须 1) 将 $newfile 变量移到循环内,以及 2) 注释掉存储在 $newfile 变量中的路径中的 $。

供参考,如果有人想使用它,最终脚本如下所示:

    $servers = Get-Content servers.txt

    $HostedRegFile = "C:\Scripts\RegistryFiles\test.reg"

    foreach ($server in $servers)

    {

    $newfile = "\$server\c`$\Downloads\RegistryFiles\test.reg"

    New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \$server\C$\Downloads\RegistryFiles

    Copy-Item $HostedRegFile -Destination $newfile

    Invoke-Command -ComputerName $server -ScriptBlock {

    Start-Process -filepath "C:\windows\regedit.exe" -argumentlist "/s C:\Downloads\RegistryFiles\test.reg"

    }

    }

我找到了最好的方法来避免混淆与服务器身份验证相关的问题并降低复杂性,只是将 Reg 文件作为参数传递给函数。

$regFile = @"
 Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
"MaxUserPort"=dword:00005000
"TcpTimedWaitDelay"=dword:0000001e
"@

Invoke-Command -ComputerName computerName -ScriptBlock {param($regFile) $regFile | out-file $env:temp\a.reg; 
    reg.exe import $env:temp\a.reg } -ArgumentList $regFile