2017-11-10 83 views
0

我有'computers.txt'中的计算机列表。如何在列表上运行命令并让PsExec或Powershell自动跳转到此列表上的下一个项目

我想在列表中的每个远程计算机名称上运行一个.exe。我必须在每台正确安装.exe的计算机上执行.ps1脚本。在PsExec中,我必须在每个计算机名称之间的一分钟或2分钟之后按回车。这将通过远程计算机列表并运行每台计算机上的.exe。 在PowerShell中,只有第一台计算机运行.exe,其余的则不执行任何操作。

有没有什么办法可以在脚本运行时不需要在计算机名称之间按下Enter来完成列表?我希望它能够自动运行。

这是我在PsExec中使用的。

psexec -s @C:\App\computers.txt cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file "C:\SpeedInstall.ps1"" 

以下是我在PowerShell中

我试图
$a = Get-Content "C:\App\computers.txt" 
foreach($line in $a) { 
psexec -s \\$line cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file C:\SpeedInstall.ps1" 
} 

回答

0

您可以使用/d交换机PSEXEC,使其不等待前一个命令就移动到下一个之前完成。有一个权衡,你不会看到该命令可能产生的任何错误消息,但它可以让你更快地完成你的列表。您的命令将如下所示:

$a = Get-Content "C:\App\computers.txt" 
foreach($line in $a) { 
    psexec -s -d \\$line cmd /c "Powershell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass && PowerShell -noninteractive -file C:\SpeedInstall.ps1" 
} 
0

为什么你甚至想要使用psexecPSRemoting怎么样?

$Computers = Get-Content -Path C:\computers.txt 

foreach ($Computer in $Computers) { 
Copy-Item -Path C:\install.exe -Destination \\$Computer\c$\Windows\Temp\install.exe 
} 

$Script = 
@" 
# Write down your installation script here 
& C:\install.exe /silent 
Set-ItemProperty -Path HKLM:\SOFTWARE\Install -Name Setting -Value 1 -Type DWord 
"@ 

$ScriptBlock = [Scriptblock]::Create($Script) 


$PSSession = New-PSSession -ComputerName $Computers -SessionOption (New-PSSessionOption -NoMachineProfile) 
Invoke-Command -Session $PSSession -ScriptBlock $ScriptBlock 
相关问题