【问题标题】:Is there a way to run multiple powershell scripts in different tabs in windows terminal?有没有办法在 Windows 终端的不同选项卡中运行多个 powershell 脚本?
【发布时间】:2021-08-15 14:10:12
【问题描述】:

我有一个封装的 powershell 脚本,它运行一些类似这样的 powershell 脚本:

Start-Process PowerShell {-NoExit .\b.ps1}
Start-Process Powershell {-NoExit .\c.ps1}
Start-Process PowerShell {-NoExit .\d.ps1}

我使用 conemu 运行包装脚本,它会在选项卡中打开所有其他脚本。 有没有办法在新的 windows 终端中做同样的事情?

我在包装脚本中试过这个:

wt new-tab PowerShell -c .\a.ps1
wt new-tab PowerShell -c .\b.ps1
wt new-tab PowerShell -c .\c.ps1

它为每个窗口打开了单独的窗口,所有这些窗口都因找不到命令错误而出错。 还有其他方法吗?

【问题讨论】:

  • ISE 在不同的选项卡中打开多个会话。

标签: windows powershell windows-terminal


【解决方案1】:

您可以通过将所有这些作为一个命令运行来简化这一点。每个wt.exe 调用都会尝试创建一个新窗口。但是,您可以将多个命令链接在一起:

wt new-tab -d . -p "Windows PowerShell" powershell -noexit -file .\a.ps1 ; new-tab -d . -p "Windows PowerShell" powershell -noexit -file .\b.ps1 ; new-tab -d . -p "Windows PowerShell" powershell -noexit -file .\c.ps1

或者可能,甚至更简单:

wt -d . powershell -noexit -file .\a.ps1 ; -d . powershell -noexit -file .\b.ps1 ; -d . powershell -noexit -file .\c.ps1
  • 您可以使用nt 作为new-tab 的简写。事实上,您实际上可以完全省略子命令,终端会假定您的意思是 new-tab
  • 如果这是您的默认配置文件,您可以省略 -p "Windows PowerShell"

【讨论】:

    【解决方案2】:

    在包装脚本中试试这个:

    wt.exe --window 0 new-tab --profile "Windows PowerShell" PowerShell -noexit -command .\a.ps1
    wt.exe --window 0 new-tab --profile "Windows PowerShell" PowerShell -noexit -file .\b.ps1
    wt.exe --window 0 new-tab --profile "Windows PowerShell" PowerShell -noexit -file .\c.ps1
    

    解释主要来自Using command line arguments for Windows Terminal

    1. 语法--window <window-id>:在特定窗口中启动终端。这里的--window 0 总是指当前的wt 窗口(参见wt -?)。
    2. 语法new-tab --profile <profile-name> [command]:
    • <profile-name> 可能取决于安装;我用了"Windows PowerShell"
    • [command](方括号表示可选性):PowerShell -noexit -file .\c.ps1。请注意,我使用了-command-file,请参阅PowerShell -?

    最后说明:考虑使用 --startingDirectory 参数(或脚本文件的绝对路径),因为 facultative startingDirectoryWT 配置文件中的目标可能不是.\wt.exe 命令中的目标…

    【讨论】:

    • 谢谢!我必须添加startingDir 参数。所以命令看起来像:wt.exe --window 0 new-tab --profile "Windows PowerShell" -d . PowerShell -noexit -file a.ps1
    猜你喜欢
    • 2021-06-30
    • 2020-11-27
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2022-01-08
    相关资源
    最近更新 更多