如何在 Powershell 中同时 运行 两个命令?

How to run two command simultaneously in Powershell?

我需要在 Powershell 中编写一个循环,强制鼠标定位,直到另一个脚本 运行。

#Mouse blocking starts now
Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$screen.Width = 1
$screen.Height = 1
do

{
    [Windows.Forms.Cursor]::Position = "$($screen.width),$($screen.height)"
}while (.\file.ps1)

这不起作用。 你有什么想法吗?

您可以使用 PSJob 启动第一个脚本并检查作业是否已在 while 条件内完成:

$MyFilePath = Join-Path $PSScriptRoot file.ps1
$MyJob = Start-Job { & $args[0] } -ArgumentList $MyFilePath 

do
{
    [Windows.Forms.Cursor]::Position = "$($screen.width),$($screen.height)"
    # Maybe wait a little 
    Start-Sleep -MilliSeconds 200
} while ($MyJob.State -eq "Running")