使用 Powershell 腳本啟動 VSCode 會阻止 Powershell 退出

使用 Powershell 腳本啟動 VSCode 會阻止 Powershell 退出

我正在嘗試編寫一個 Powershell 腳本來自動設定每天的工作環境。為此,我有以下兩個腳本。

設定視窗.ps1

Function Set-Window {
<#
.LINK
https://superuser.com/questions/1324007/setting-window-size-and-position-in-powershell-5-and-6
#>
[cmdletbinding(DefaultParameterSetName='Name')]
Param (
    [parameter(
        Mandatory=$False,
        ValueFromPipelineByPropertyName=$True, 
        ParameterSetName='Name'
    )]
    [string]$ProcessName='*',
    [int]$X,
    [int]$Y,
    [int]$Width,
    [int]$Height,
    [switch]$HideWindow
)
Begin {
    Try { 
        [void][Window]
    } Catch {
    Add-Type @"
        using System;
        using System.Runtime.InteropServices;
        public class Window {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public extern static bool MoveWindow( 
                IntPtr handle, int x, int y, int width, int height, bool redraw);

            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public extern static bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        }
"@
    }
}
Process {
    If ($PSBoundParameters.ContainsKey('ProcessName')) {
        $Processes = Get-Process -Name "$ProcessName"
    } else {
        throw 'No processes match criteria specified'
    }

    If ($PSBoundParameters.ContainsKey('HideWindow')) {
        $Processes | ForEach-Object {
            # 0 is the value that represents "hide".
            # see https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/show-or-hide-windows
            # for more details
            [Window]::ShowWindowAsync($_.MainWindowHandle, 0)
        }
    } else {
        $Processes | ForEach-Object {
            [Window]::MoveWindow($_.MainWindowHandle, $X, $Y, $Width, $Height, $True)
        }
    }
}
}

開始工作.ps1

. C:\Users\<username>\Projects\Personal\PowerShell\Set-Window.ps1

# Start all necessary applications
Start-Process "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\Code.exe" '--log=off'
Start-Process Chrome '--profile-directory="Profile 2"'
Start-Process "C:\Users\<username>\AppData\Roaming\Spotify\Spotify.exe"
Start-Process "C:\Users\<username>\AppData\Roaming\Zoom\bin\Zoom.exe"
Start-Process "C:\Users\<username>\AppData\Local\slack\slack.exe"

# Some applications can be moved right away, but still best to wait a bit
Start-Sleep -Seconds 1
Set-Window -ProcessName Spotify -X 400 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Chrome -X 200 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Slack -X 600 -Y 0 -Height 600 -Width 1200

# Others need a more time to load everything
Start-Sleep -Seconds 3
Set-Window -ProcessName Code -X 0 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Zoom -HideWindow

Start-Sleep -Milliseconds 500
Exit

為了運行這些腳本,我建立了一個快捷方式,並將目標設定為C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy remotesigned -File C:\Users\<username>\Projects\Personal\PowerShell\Start-Work.ps1,並將該捷徑固定到工作列。當我單擊該快捷方式時,一切都會按預期運行,但有一個例外:PowerShell 程式永遠不會關閉。根據偶爾寫入控制台的輸出,我認為這是因為 VSCode。我已經能夠透過運行一個簡單的.ps1腳本來演示這一點,該腳本僅打開 VS Code,並且可以觀察到它打開了一個 PowerShell 視窗。但是,如果我只是Start-Process Code從打開的 PowerShell 視窗運行,則不會發生這種情況。

說了這麼多,有誰知道如何強制 PowerShell 視窗真正關閉嗎?

答案1

VS Code 似乎可能會保持標準輸出流開啟。我的行為與您相同,但我可以透過將輸出重定向到檔案來關閉 powershell:

Start-Process "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\Code.exe" '--log=off' `
  -RedirectStandardOutput "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\output.log"
exit

它確實創建了一個文件,但該文件是空的 - 但我從未將任何輸出寫入控制台。

相關內容