在脚本中将 powershell 模式更改为 STA

Change powershell mode to STA in script

我有一个 powershellscript,它使用 XAML 显示 WPF 表单。要执行此脚本,必须在 STA 模式下启动 powershell 控制台(PS MTA 模式下默认为 2.0)。

我知道有机会使用以下命令在控制台中更改模式:

powershell -sta

但将来我的脚本将由其他程序自动执行,无需任何参数。所以控制台不能先用-sta参数调用。

在启动 WPF/XAML 之前,是否有机会直接在 powershell 脚本中更改模式?

这里有一个简单的例子:

$x = {
    for($i= 0;$i -le 10;$i++){
        Write-host $i
    }
}

Start-Process Powershell.exe -argumentlist "-sta -NoExit -NoProfile -ExecutionPolicy Bypass -Command $X"