Using Powershell to Distribute Script Resources on Remote Servers

More theory issue ...

I have a powershell script that exists on three servers. There are three servers in this example:

  • server1
  • server2
  • server3

I am using another server4 machine to invoke the script C: \ ExampleScript.ps1 remotely using Invoke-Command , indicating the remote machine using the ComputerName parameter. The ultimate goal of the script is to determine if powershell is working, if it is not, then the computer is "not busy" and can open a script that is called remotely. If the computer is busy, go to the next server and continue working through three machines until all the parameter values ​​are exhausted. If all the machines are busy, it would be ideal if there was a way to periodically check the processes and see if they are all open. Thus, script execution can be balanced on different machines, albeit primitively.

Consider the following code:

$servers = "server1","server2","server3" $data = "param1", "param2", "param3", "param4", "param5", "param6" #somehow loop through the different servers/data using the above arrays $job = Invoke-Command $servers[0] { $ProcessActive = Get-Process powershell -ErrorAction SilentlyContinue if($ProcessActive -eq $null) { "Running" Invoke-Command -ComputerName $env:computername -FilePath C:\ExampleScript.ps1 -ArgumentList $data[0] } else { "Busy go to next machine" } } -AsJob Wait-Job $job $r = Receive-Job $job $r 

The expected result to be achieved is an attempt to load the balance of the script on the machines based on whether there is an active powershell process if it does not move to the next computer and does not perform the same test and subsequent possible execution. The script should go through all the values ​​specified in the $ data array (or something else).

+6
source share
1 answer

I found this question interesting, so I wanted to try.

 $servers = "server1","server2","server3" $data = New-Object System.Collections.ArrayList $data.AddRange(@("param1", "param2", "param3", "param4", "param5", "param6")) $jobs = New-Object System.Collections.ArrayList do { Write-Host "Checking job states." -ForegroundColor Yellow $toremove = @() foreach ($job in $jobs) { if ($job.State -ne "Running") { $result = Receive-Job $job if ($result -ne "ScriptRan") { Write-Host " Adding data back to que >> $($job.InData)" -ForegroundColor Green $data.Add($job.InData) | Out-Null } $toremove += $job } } Write-Host "Removing completed/failed jobs" -ForegroundColor Yellow foreach ($job in $toremove) { Write-Host " Removing job >> $($job.Location)" -ForegroundColor Green $jobs.Remove($job) | Out-Null } # Check if there is room to start another job if ($jobs.Count -lt $servers.Count -and $data.Count -gt 0) { Write-Host "Checking servers if they can start a new job." -ForegroundColor Yellow foreach ($server in $servers) { $job = $jobs | ? Location -eq $server if ($job -eq $null) { Write-Host " Adding job for $server >> $($data[0])" -ForegroundColor Green # No active job was found for the server, so add new job $job = Invoke-Command $server -ScriptBlock { param($data, $hostname) $ProcessActive = Get-Process powershell -ErrorAction SilentlyContinue if($ProcessActive -eq $null) { # This will block the thread on the server, so the JobState will not change till it done or fails. Invoke-Command -ComputerName $hostname -FilePath C:\ExampleScript.ps1 -ArgumentList $data Write-Output "ScriptRan" } } -ArgumentList $data[0], $env:computername -AsJob $job | Add-Member -MemberType NoteProperty -Name InData -Value $data[0] $jobs.Add($job) | Out-Null $data.Remove($data[0]) } } } # Just a manual check of $jobs Write-Output $jobs # Wait a bit before checking again Start-Sleep -Seconds 10 } while ($data.Count -gt 0) 

Basically, I create an array and constantly fill it with one task for each server.

Data is removed from the list when starting a new task and is added back if the task fails. This is done so that the servers running the script with the same data / parameters.

I lack the proper environment to check it right at the moment, but tomorrow it will give you a twist and update my answer with any changes if necessary.

+5
source

All Articles