Home > Software engineering >  I want to restart a powershell script to load the newest version of the script as an when the script
I want to restart a powershell script to load the newest version of the script as an when the script

Time:03-08

I have a .ps1 running continually from start up. I want this script refreshed automatically when the script has been updated and then continue running the new script.
Maybe a restart current script at the end of the loop that it runs every 5 minutes would do it. Currently I'm manyally stopping and restarting the script on up to 250 machines.

CodePudding user response:

Chris, Rather than loading a script on every machine, why not place it on a share which is accessible to all machines and have them run it from that one place?

CodePudding user response:

You almost certainly have a loop in your script. If the guts of the loop are the only part that is changing, then gut your loop and place its content in another script that your loop is calling.

In the following experiment, modifying Worker.ps1's Write-Host statements resulted in changes to the output each time the loop was ran.

Caller.ps1

for ($i = 0; $i -lt 200; $i  ) {
    Write-Host "About to call"
    .("$PSScriptRoot\Worker.ps1")
    Write-Host "After calling"        
}

Worker.ps1

Write-Host "Working!"
Start-Sleep -Seconds 5
Write-Host "Done working!"
  • Related