Reputation: 11
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
Upvotes: 0
Views: 4384
Reputation: 14705
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'
Upvotes: 1