首页 > 解决方案 > 用于将 .reg 文件导入远程计算机挂起的 PowerShell 脚本

问题描述

我正在使用以下脚本将 .reg 文件导入远程计算机。我需要发生的事情如下:

  1. 连接到远程计算机并杀死正在运行的进程(如果它正在运行)。
  2. 将 .reg 密钥文件从网络共享位置复制到远程计算机。
  3. 将复制的 .reg 密钥导入远程计算机的注册表。
  4. 从远程计算机中删除复制的 .reg 密钥文件。
  5. 启动一开始就被杀死的进程。

现在我对 PowerShell 脚本编写相对较新,并且我拥有的脚本部分是从我在互联网和我的 PowerShell Cookbook 中找到的脚本蚕食的。

当我运行脚本时会发生什么,它将连接到远程计算机并终止进程,但随后脚本被挂起,并且不执行步骤 2-5。它也没有抛出任何错误。我错过了什么或做错了什么?

提前致谢!

#Variables
$Computers = Get-Content C:\computer.txt
$LocalRegFileName = "regfile.reg"
$HostedRegFile = "\\NETWORK-SHARE\Folder\Folder\regfile.reg"
$ProcessName = "Process"
$FilePath = "C:\Program Files (x86)\Folder\$ProcessName.exe"

foreach ($Computer in $Computers) {
    Invoke-Command -ComputerName $Computer {
        Get-Process -Name $ProcessName | Stop-Process -Force
    }

    $NewFile = "\\$Computer\C'$\TEMP\$LocalRegFileName"
    New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \\$Computer\C$\TEMP
    Copy-Item $HostedRegFile -Destination $NewFile
    Invoke-Command -ComputerName $Computer -ScriptBlock {
        Start-Process -FilePath "C:\Windows\regedit.exe" -ArgumentList "/s C:\TEMP\$LocalRegFileName"
    }

    Invoke-Command -ComputerName $Computer -ScriptBlock {
        Remove-Item "C:\TEMP\$LocalRegFileName"
    }

    Invoke-Command -ComputerName $Computer -ScriptBlock {
        Start-Process -FilePath "$FilePath\$ProcessName.exe"
    }
}    

标签: powershellforeachregistryinvoke-command

解决方案


推荐阅读