我正在尝试编写一个 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 窗口。但是,如果我只是从打开的 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
它确实创建了一个文件,但是该文件是空的——尽管我从未将任何输出写入控制台。