简体   繁体   中英

Running bat file via powershell on multiple servers

I'm using powershell to try and run an installation script remotely on multiple servers, but have become a bit stuck.

Below is what I have so far. Computers.txt contains a list of all the servers I want to run the installation on. These all sit on the same domain. I then map a drive to browse to the share where the script is, and then run the installation script.

$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

I expect I am missing quite a bit in order for this to work? The bat file itself is pretty complex so at the moment I do not want to change this to powershell.

The PC I am running from is also a trusted host on these servers.

Appreciate your input, I'm a powershell newbie

Thanks

I think you're missing the loop that runs through the list (array) of servers:

$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

I've also made it a job, so you can run it on multiple servers at the same time.

To even more simplify things, you don't have to map a drive just try this in the ScriptBlock of Invoke-Command :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM