
Ich versuche, ein Powershell-Skript zu schreiben, das die tägliche Einrichtung meiner Arbeitsumgebung automatisiert. Dazu habe ich die folgenden beiden Skripte.
Set-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)
}
}
}
}
Start-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
Um diese Skripte auszuführen, habe ich eine Verknüpfung mit dem Ziel erstellt C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy remotesigned -File C:\Users\<username>\Projects\Personal\PowerShell\Start-Work.ps1
und diese Verknüpfung an meine Taskleiste angeheftet. Wenn ich auf die Verknüpfung klicke, läuft alles wie erwartet, mit einer Ausnahme: Das PowerShell-Programm wird nie geschlossen. Aufgrund der Ausgabe, die gelegentlich in die Konsole geschrieben wird, gehe ich davon aus, dass dies an VSCode liegt. Ich konnte dies demonstrieren, indem ich ein einfaches .ps1
Skript ausführte, das nur VS Code öffnet, und beobachten konnte, dass es ein PowerShell-Fenster öffnet. Dies geschieht jedoch nicht, wenn ich es einfach Start-Process Code
aus einem geöffneten PowerShell-Fenster ausführe.
Nachdem das alles geklärt ist: Weiß jemand, wie ich das Schließen des PowerShell-Fensters erzwingen kann?
Antwort1
Es scheint, als würde VS Code den Standardausgabestream offen halten. Bei mir war das gleiche Verhalten wie bei Ihnen, aber ich konnte Powershell schließen, indem ich die Ausgabe stattdessen in eine Datei umleitete:
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
Es wird zwar eine Datei erstellt, aber die Datei ist leer. Allerdings wurde bei mir nie eine Ausgabe auf die Konsole geschrieben.