通过多个服务器上的powershell运行bat文件

时间:2015-11-04 11:42:36

标签: powershell powershell-v3.0

我正在使用powershell尝试在多台服务器上远程运行安装脚本,但是有点卡住了。

以下是我到目前为止的情况。 Computers.txt包含我要在其上运行安装的所有服务器的列表。这些都坐在同一个域上。然后,我映射驱动器以浏览到脚本所在的共享,然后运行安装脚本。

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

New-PSDrive –Name “S” –PSProvider FileSystem –Root “\\dc1-app01\apps” –Persist

Start-Process -FilePath S:\createfile.bat

为了让这个工作,我希望我失踪了很多? bat文件本身非常复杂,所以目前我不想将其更改为powershell。

我运行的PC也是这些服务器上的可信主机。

感谢您的意见,我是一名名副其实的新手

由于

1 个答案:

答案 0 :(得分:1)

我认为你错过了贯穿服务器列表(数组)的循环:

$VerbosePreference = 'Continue'

$Computers = Get-Content -Path c:\temp\Computers.txt

Foreach ($C in $Computers) {

    Write-Verbose "Start batch file as a job on $C"

    Invoke-Command -ComputerName $C -ScriptBlock {
        New-PSDrive –Name 'S' –PSProvider FileSystem –Root '\\dc1-app01\apps' –Persist
        Start-Process -FilePath S:\createfile.bat -Wait
    } -AsJob
}

Write-Verbose 'Waiting for all jobs to finish'
Wait-Job
Write-Verbose 'Showing job results:'
Get-Job | Receive-Job

我也做了一份工作,所以你可以同时在多台服务器上运行它。

为了更加简化,您无需映射驱动器只需在ScriptBlock Invoke-Command中尝试此操作:

& '\\dc1-app01\apps\createfile.bat'