我想重新启动 powershell 脚本以在脚本更新时加载最新版本的脚本

I want to restart a powershell script to load the newest version of the script as an when the script has been updated

我有一个 .ps1 运行 从启动开始就不断。我希望脚本更新后自动刷新此脚本,然后继续 运行 新脚本。
也许在每 5 分钟运行一次的循环结束时重新启动当前脚本就可以了。 目前,我经常在多达 250 台机器上停止并重新启动脚本。

克里斯, 与其在每台机器上加载脚本,不如将它放在一个所有机器都可以访问的共享上,然后让它们 运行 从那个地方获取它?

您的脚本中几乎肯定有一个循环。如果循环的内部是唯一发生变化的部分,那么将循环内部结构化并将其内容放在循环调用的另一个脚本中。

在下面的实验中,修改 Worker.ps1 的 Write-Host 语句导致每次循环 运行.

时输出发生变化

来电者。ps1

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

工人.ps1

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