Powershell スクリプトを使用して VSCode を起動すると、Powershell が終了しなくなる

Powershell スクリプトを使用して VSCode を起動すると、Powershell が終了しなくなる

毎日の作業環境の設定を自動化する Powershell スクリプトを作成しようとしています。これを行うには、次の 2 つのスクリプトを使用します。

ウィンドウの設定.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 プログラムが閉じないという 1 つの例外を除いて、すべてが期待どおりに実行されます。コンソールに時々書き込まれる出力に基づいて、これは VSCode が原因であると推測します。VS .ps1Code のみを開く簡単なスクリプトを実行してこれを実証し、PowerShell ウィンドウが開くことを確認できました。ただし、開いている PowerShell ウィンドウから実行しただけでは、これは発生しませんStart-Process Code

それで、そうは言っても、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

ファイルは作成されますが、ファイルは空です。ただし、コンソールに出力が書き込まれることはありません。

関連情報