
매일 작업 환경 설정을 자동화하는 Powershell 스크립트를 작성하려고 합니다. 이를 위해 다음 두 개의 스크립트가 있습니다.
설정-Window.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)
}
}
}
}
시작-Work.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
파일을 생성하지만 파일이 비어 있습니다. 하지만 콘솔에 출력이 기록된 적이 없습니다.