Windows 终端:具有不同根路径的拆分窗格(Powershell)

问题描述 投票:0回答:1

我目前正在使用带有 PowerShell 的 Windows 终端,我想找到一种解决方案来记住会话之间创建的不同终端,因为内部 Visual Studio 命令行工具不提供此功能(如果提供,我竖起耳朵:))

为了实现这一目标,我尝试在 Windows 终端中构建一个脚本,该脚本将创建一个新选项卡并将其分为三个部分,每个部分都有不同的根。这是我在错误日志旁边尝试过的脚本:

$frontendPath = "C:\Path_Example\frontend"
$backendPath = "C:\Path_Example\backend"
$rootPath = "C:\Path_Example"

$command = "wt ; split-pane -p 'pwsh -NoExit -Command `""cd '$frontendPath'; pwsh`""" +
           " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$backendPath'; pwsh`""" +
           " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$rootPath'; pwsh`"""

Start-Process -FilePath wt.exe -ArgumentList "-d", ".", "-e", $command
At C:\Users\speak\split.ps1:5 char:58
+ $command = "wt ; split-pane -p 'pwsh -NoExit -Command `""cd '$fronten ...
+                                                          ~~
Unexpected token 'cd' in expression or statement.
At C:\Users\speak\split.ps1:6 char:59
+            " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$backen ...
+                                                           ~~
Unexpected token 'cd' in expression or statement.
At C:\Users\speak\split.ps1:7 char:59
+            " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$rootPa ...
+                                                           ~~
Unexpected token 'cd' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

如您所见,我对脚本编写经验不是很丰富,因此我向 ChatGPT 寻求帮助。不幸的是,尽管尝试了不同的提示,我仍无法解决该问题。如果有人可以提供一些指导或帮助我解决问题,我将不胜感激。

非常感谢。

powershell terminal splitpane
1个回答
3
投票
  • wt.exe
    ,尽管是 Windows 终端的 CLI,但它是一个 GUI 子系统应用程序,因此默认情况下会异步启动;因此,您可以直接调用它 - 不需要 Start-Process
    
    

  • pwsh.exe

    ,PowerShell(核心)7+ CLI,有一个 
    -WorkingDirectory (
    -wd
    ) 参数。
    
    

  • 更一般地说,正如
  • Mehrdad Qasemkhani

    指出的那样,wt.exe 本身有一个

    --startingDirectory
    (
    -d
    ) 参数,该参数允许指定启动工作目录,而不管正在启动什么 shell。
    
    

  • 因此:

  • -d

    (

    --startingDirectory
    ) 与显式 shell 可执行文件 (
    pwsh
    ) 结合使用:
    wt.exe -d $frontEndPath pwsh `; split-pane -H -d $backendPath pwsh `; split-pane -H -d $rootPath pwsh
    

  • 如果您的
  • 默认

    个人资料是pwsh

    -d
    单独
    就足够了: wt.exe -d $frontEndPath `; split-pane -H -d $backendPath `; split-pane -H -d $rootPath

  • 或者,使用
  • pwsh

    -wd
    (
    -WorkingDirectory
    ) 参数:
    wt.exe pwsh -wd $frontEndPath `; split-pane -H pwsh -wd $backendPath `; split-pane -H pwsh -wd $rootPath
    

    
    
  • 这将打开一个

Windows 终端窗口,其中有一个选项卡水平分为 3 个窗格,每个窗格都运行一个 PowerShell(核心)实例,并将指定目录作为当前位置(工作目录)。 请注意需要将 ;

转义为

`;

,以便 PowerShell 不会将其解释为其语句分隔符。
如果您想


现有

窗口中打开新选项卡,则需要做更多工作:

    -w 0
  • 定位最近活动的窗口

    但是,该窗口不会自动
  • 激活
  • (如果有办法通过选项来做到这一点,请告诉我们)。

    $frontendPath = 'C:\' $backendPath = 'C:\Windows' $rootPath = 'C:\Users' # Get the PID (process ID) of what is presumed to be the most # recently active window, if any. $existingPid = (Get-Process -ErrorAction Ignore WindowsTerminal | Select-Object -Last 1).Id # -w 0 targets the most recently active window, if any. wt.exe -w 0 -d $frontEndPath pwsh `; split-pane -H -d $backendPath pwsh `; split-pane -H -d $rootPath pwsh `; ft 0 # If a preexisting window was targeted, it must now be activated explicitly. if ($existingPid) { (New-Object -ComObject WScript.Shell).AppActivate($existingPid) }

© www.soinside.com 2019 - 2024. All rights reserved.