在继续当前脚本的同时异步运行单独的 PowerShell 脚本

2023-12-22

PowerShell 脚本 #1 执行以下操作:

Performs FTP ops ending with saving updated remote directory data in a local file

该脚本快速运行,直到必须使用 FTP 获取远程目录数据。最好将远程目录数据检索删除到不同的 PowerShell 脚本#2 中。

这个帖子 https://stackoverflow.com/questions/5549516/run-a-powershell-script-from-another-one解释从脚本内启动脚本。但在这种情况下,第一个脚本似乎在第二个脚本执行时被挂起。

我如何编写脚本#1,以便脚本#2 启动并被遗忘,脚本#1 继续并快速完成,而脚本#2 在后台完成。


您可以使用Start-Job https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-5.1启动 PowerShell 后台作业。这样作业就可以在不与当前会话交互的情况下运行,并且在作业异步运行时会立即返回。如果您希望收到作业结果,您可以使用Receive-Job得到结果。

Example

  • Start-Job -ScriptBlock {Get-Process}
  • Start-Job -Command "Get-Process"
  • Start-Job -FilePath "D:\script.ps1"

您还可以使用Start-Process https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1开始另一个进程。您可以指定程序可执行文件或脚本文件,或者可以使用计算机上的程序打开的文件。当您启动不可执行文件的进程时,与该文件类型关联的程序将像您使用时一样运行Invoke-Item cmdlet.

Example

  • Start-Process PowerShell -Argument "C:\MyScript.ps1"
  • Start-Process "C:\SomeFile.txt"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在继续当前脚本的同时异步运行单独的 PowerShell 脚本 的相关文章

随机推荐