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.
Related
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?
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
So i'm trying to write a script which checks if the cmd is with administrative privileges. If it is I just ran the command - which is a jar file. If it isn't what I did was create a link to the cmd and in that link I made it always run as administrator.
The problem is that after it runs the command it opens the second cmd as administrator but then the command needs to be written again. Is there any chance to save the command written and as the second cmd opens run it?
net session >nul
if %errorLevel% == 0 (
java -jar %~dp0/myjar.jar%*
) else (
echo Administrative permissions required
cmdadmin.lnk
)
cmdadmin.lnk is as I said a link to cmd which in the advanced properties I set as run as administrator.
Now what I want is to pass a command when opening the link. Is it possible?
As the title suggests, I have an added parameter in my Task Scheduler Actions that logs stdout and stderr to a log.txt file. The logging works when the action is run through the command prompt, but not when the action is run by the actual Task Scheduler (at its specified time). Task scheduler reports the action runs successfully, but I can't be sure it does because there's no logging:)
Command looks like this
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I'm unfortunately not a native Windows user, so any help would be appreciated. I'm running Windows Server 2008 R2 Enterprise.
Thanks!
Cmd.exe handles command redirection. You have to run it under cmd.exe. Powershell probably also can do redirection but in your script (.NET can).
A black window just means a console program is running. Only if cmd is running does cmd features become available. By starting cmd or by putting it in a batch you canget redirection from cmd.
cmd /c powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
See for Help
cmd /?
Place the command you listed in a batch file and then schedule the batch file.
If you are doing so and it fails, then try it with your account credentials as authentication in task scheduler, to see if it is a permissions issue.
#echo off
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I want to be able to run an external cmd prompt concurrently with my Java code. While running java application need to start external cmd prompt ,and this command prompt need to updated from my java application as well as i need to get values from this command prompt into java application.Please provide suitable suggestion
Try this
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start command");
UPDATION 2
To execute commands try this
rt.exec("cmd.exe /c start cmd.exe /k \"ping localhost\"");