aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2025-02-04 19:20:23 -0500
committerXe Iaso <me@xeiaso.net>2025-02-04 19:20:23 -0500
commitb996f0568052b0416ff5913c35d7ee9aeeea1c37 (patch)
tree1af3f35ec1db77ab1160bf911a240d10d4ca31a5
parent939999921b07686d716b38574488ffb7aa294c56 (diff)
downloadxesite-b996f0568052b0416ff5913c35d7ee9aeeea1c37.tar.xz
xesite-b996f0568052b0416ff5913c35d7ee9aeeea1c37.zip
start a gui application in the foreground with powershell
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--lume/src/notes/2025/pwsh-start-process.mdx38
1 files changed, 38 insertions, 0 deletions
diff --git a/lume/src/notes/2025/pwsh-start-process.mdx b/lume/src/notes/2025/pwsh-start-process.mdx
new file mode 100644
index 0000000..00e54a9
--- /dev/null
+++ b/lume/src/notes/2025/pwsh-start-process.mdx
@@ -0,0 +1,38 @@
+---
+title: "Life pro tip: How to run a gui application in the foreground with PowerShell"
+desc: "TL;DR: Start-Process -Wait -NoNewWindow"
+date: 2025-02-04
+---
+
+Among other things, I am the systems administrator for the home network and all of its services. My husband wanted to run the dedicated server for a game on the homelab but was having some trouble setting it up. Eventually it resulted in us giving up and renting a Windows VPS in the cloud.
+
+I wanted a script that I can leave running that will update the dedicated server program, start up the server, and then try to update the dedicated server when it crashes. In order to do this in PowerShell, you need a script like this:
+
+```powershell
+Set-Location -Path C:\Games\Motortown
+
+while ($true) {
+ & C:\Users\Administrator\Software\steamcmd.exe blah blah
+ Start-Process \
+ -FilePath C:\Games\Motortown\server.exe \
+ -Wait \
+ -NoNewWindow \
+ -ArgumentList "Jeju_World?listen?","-server","-log","-useperfthreads","-Port=7777","-QueryPort=27015"
+}
+```
+
+The important part is the [`Start-Process`](https://lazyadmin.nl/powershell/start-process/#using-arguments-with-start-process) cmdlet, this is what lets you start a GUI program and then block execution until the program exits. If you don't do this, then the script will continue to the next line while the program is started in the background. This makes steamcmd very unhappy.
+
+In order to pass command-line arguments to the process, you need to use the `-ArgumentList` flag with the arguments for the program. This is similar to how you would pass arguments to a program in a shell script:
+
+```sh
+some-program arg1 arg2 arg3
+```
+
+Except in PowerShell it looks worse:
+
+```powershell
+Start-Process -FilePath some-program.exe -ArgumentList "arg1","arg2","arg3"
+```
+
+I hope this helps you.