How to launch cmd running a command from powershell - windows

I want to write a powershell script that sets up a development environment. Firstly, I want to launch two command prompts which are running yarn run dev:client and yarn run dev:server respectively.
I have tried start cmd and Start-Process cmd -Argument yarn,run,dev:server, but am unable to get the newly launched command prompt to run the command.
How do I do this?

The generic way is:
cmd /c "insert_your_command_here"
This means in your case you would execute:
cmd /c "yarn run dev:client"
cmd /c "yarn run dev:server"

What about:
$arg="run dev:server"
Start-Process -FilePath path_to_yarn -Args $arg -passthru -RedirectStandardError err.log

Related

How to keep the PowerShell process alive after closing powershell terminal in windows

I need to run a PowerShell command in windows PowerShell, its running fine as expected. The problem is when I close the Windows PowerShell terminal, it kills the process, whereas I want the process to continue running forever.
Is there a way to close the terminal and have process working in the background.
Here is my command that I need to run:
.\start.ps1 -accountUuid '{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX}' `
-repositoryUuid '{XXXXXX-XXXXX-XXXX-XXXX-XXXXXXXXXX}' `
-runnerUuid '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX}' `
-OAuthClientId XXXXXXXXXXXXXXXXX `
-OAuthClientSecret XXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXX `
-workingDirectory '..\temp'
You can try with the command Start-process with option -NoNewWindow to run the process in the background. Add all your arguments in -ArgumentList.
Start-Process -FilePath ".\start.ps1" -ArgumentList "-accountUuid ...", "-repositoryUuid ..." -NoNewWindow
More info here

Run commands in new terminals, then kill later

I have a start.bat similar to the following:
start npx tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch
start caddy run
It launches 2 new cmd prompts and starts processes within them (which both run indefinitely). Is there a way for this bat file to send a kill command to the cmds it spawned? Ideally I'd have a single command to kill all "child" processes which were created.
A PowerShell solution - which is acceptable as both implied by this question's tagging and as explicitly stated by icanfathom (the OP) in a comment ("No, PS would be fine") - using the Start-Process and Stop-Process cmdlets:
$processes = & {
Start-Process -PassThru npx 'tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch'
Start-Process -PassThru caddy run
}
# ...
# Stop (kill) all launched processes.
$processes | Stop-Process
set "wintitle=my process %time%"
start "%wintitle%" npx tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch
start "%wintitle%" caddy run
....
taskkill /FI "WINDOWTITLE eq %wintitle%"
[untested] - should work in theory...
Always best to start with a quoted-title as the first quoted string seen by the start command may be eaten and used as a title otherwise.

Dynamically rename a command prompt window title via Powershell

I have a powershell script which launches a ffmpeg command
$process = Start-Process -FilePath "cmd" -ArgumentList "/c c:\bin\ffmpeg\bin\ffmpeg.exe -rtsp_transport tcp -i........
This spawns a command prompt window.
I would like to rename the title of the command prompt window and be able to do that for every iteration I run of the ffmpeg command.
I have seen how to rename the title of the window directly via the command prompt and how to rename the powershell window title.
I cannot find any information pertaining to powershell being able to dynamically assign a new title to the command prompt window when created.
Any assistance/pointers would be greatly appreciated.
Once you pass your code off to the cmd instance you started it would be up to the running process to update it's title window (without getting into pinvoke/Windows API calls.) If ffmpeg.exe provides you with the current file as it's running then simply use that to set the title. If not, then it's most likely you'd need to adjust your commands to first get the list of files then iterate over those files setting the title and running the ffmpeg command. Here's a small example of letting the commands set the title.
Start-Process -FilePath "cmd" -ArgumentList "/c for /l %a in (1,1,10) do (title %a & cls & timeout 3)"
If you are instead referring to each time you do Start-Process then simply set the title before the other commands.
Start-Process -FilePath "cmd" -ArgumentList "/c title ffmpeg command running & c:\bin\ffmpeg\bin\ffmpeg.exe -rtsp_transport tcp -i........"
The & character instructs CMD to run the first command AND the next command. && says run the first command and only run the second if the first succeeds. || says run first command and if it fails run second.
By the way, unless you use the -Passthru on Start-Process then it is not collecting anything. With the passthru parameter it would collect a System.Diagnostics.Process object. That could be used for tracking, closing, etc.
$host.ui.RawUI.WindowTitle = "Changed Title"
Something like this?

Is there a way to open multiple instances of CMD's with Azure-Pipelines?

I'm using Azure-Pipelines for my CI. In my pipeline, I need to use multiple instances of a CMD (Windows). For example, one CMD needs to start and continuously run a server, while the other needs to then deploy an app while the server is still running.
When using the CMD task, it only uses one instance of the CMD, so when I run the server, it never finishes and never gets to running the app.
Is there a way to run multiple instances of a CMD with Azure-Pipelines?
You can start cmd.exe on the agent machine from a powershell task using Start-Process command.
So you can add multiple powershell tasks to start multiple CMD instances on the agent machine. Or you can start multiple CMD instances in one powershell task. Se below example:
steps:
- powershell: |
'Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k echo "hello"}'
- powershell: |
'Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k echo "world"}'

how to execute a command in CMD from a WPF application?

i need help with a small programm, i just want to start a CMD and then run a command like: dir
so i did the following:
Process.Start("cmd", "dir");
the problem is, that dir is not executed in the cmd.
how can i do this?
Try
Process.Start("cmd", "/c dir");
or
Process.Start("cmd", "/k dir");
to keep the command window open.

Resources