Win7 runas command: How to capture output of command that is run? - runas

I'm trying (under Windows 7) to use the runas command to stop then restart a service. (Win7 requires admin privs to do this; thus the use of runas.)
Stopping the service works fine, but starting it does not. Here's the command I'm using for stopping the service:
runas /user:myDomain\myUserId "net stop serviceName"
Here's the command for starting the service:
runas /user:myDomain\myUserId "net start serviceName"
When I run the above command another command window opens, but flashes away before I can see anything in it; thus I have no idea what's going wrong.
So, my question is: How might I capture stdout and/or stderr from the net start command when run via runas? I've tried just using redirection but just get an empty file. Another solution would be to get the window opened by runas for the subtask to remain open.
Thanks in advance.

Launch cmd.exe instead with the command to run, and specify that the output be written to a file.
runas /user:myDomain\myUserId "cmd.exe /c net stop serviceName > output.txt"
You can use 2> for error output from net stop.

Also, if you don't want to bother with the output file, you can use cmd.exe /k instead of /c to launch the command and it will leave the session window open for you. Might be easier/quicker if you just want a quick peek.

Try gsudo and run the command:
gsudo net stop serviceName

Related

how to run several cmd command prompts from powershell

So I am trying to write a script that allows me to open several cmd command prompt and write in them the same command but with different variables.
The solution that I came with was to write a PowerShell script that calls inside a loop a cmd file and pass a variable each time to the cmd file but I'm stuck, the PowerShell script execute only one cmd.
Can someone help to figure this out ?
Thanks :)
You can use the following :
cmd.exe /c [command]
for example
$x = 1..100
foreach ($n in $x){cmd.exe /c ping 192.168.1.$n}
mohamed saeed's answer shows you to execute cmd.exe commands synchronously, in sequence in the current console window.
If, by contrast, you want to open multiple interactive cmd.exe sessions, asynchronously, each in its separate, new window, use cmd.exe's /k option and invoke cmd.exe via Start-Process:
# Open three new interactive cmd.exe sessions in new windows
# and execute a sample command in each.
'date /t', 'ver', "echo $PSHOME" | ForEach-Object {
# Parameters -FilePath and -ArgumentList implied.
Start-Process cmd.exe /k, $_
}
Note:
Unless your cmd.exe console windows have the Let the system position the window checkbox in the Properties dialog checked by default, all new windows will fully overlap, so that you'll immediately only see the last one opened.
The last new window opened will have the keyboard focus.
The commands passed to /k are instantly executed, but an interactive session is then entered.
If you would like to keep in purely batch, you can use the start command. The /k switch keeps the command line open. You would use /c if you want to carry out the command and terminate :
start "" %comspec% /k ping 192.168.1.1
From powershell, you can use the Start-Process command with an ArgumentList:
Start-Process cmd.exe -ArgumentList "/k ping 192.168.1.1"

Powershell execute commands on newly started Command Prompt process

Trying to start a new process in a separate command prompt on Windows 10, but can't find how to execute commands in opened prompt. With Powershell, I could use -Command:
Start-Process PowerShell "-Command tasklist"
But how to do that in Command Prompt window? This, obviously, doesnt work:
Start-Process cmd '-Command tasklist'
You're using the PowerShell arguments for cmd.exe. cmd /? will give you the usage, but what you want is cmd /c COMMAND [ARGUMENTS]:
Start-Process cmd "/c ping -n 4 google.com"
#Jeff Zeitlin was kind enough to provide a link to the SS64 CMD usage.
Worth mentioning, you don't need to use Start-Process when running external commands unless:
You are running a GUI application and wait to use the -Wait parameter to wait until the program exits
You want to run something in a different process asynchronously and check the result later
In this case don't forget to use -PassThru and assign the process to a variable, so you can check the result when ready

Powershell ISE breaks when using CMD /C Pause

I'm newer to PowerShell, transitioning from batch, so forgive me if I make a stupid mistake. I really like using Powershell ISE. But with the current script I'm trying to run, I use the cmd /c pause | Out-Nul command and it breaks ISE by not allowing me to continue any further.
I was trying for a long time to find some way to pause but not show output from the command and I found the cmd /c pause command. I can run it fine in a normal PowerShell prompt but ISE just isn't having it.
Anyone have any ideas why this happens or a better alternative to cmd /c pause Out-Nul? Thanks and any advice is more than welcome!
Powershell ISE doesn't redirect STDIN (which pause needs).
Shelling out to cmd for the pause command is unneeded.
See this post for more info on the ISE not being able to take stdin. You won't find your specific issue but the problem is the EXACT same problem.
On that page, you will also find a better/native way to do "pause" from powershell...
(aka [System.Console]::ReadKey($false) )

force kms sync with powershell hangs

I have a command in my script.ps1 which is run by a next build step in tfs 2015. I then navigate to the location of office, run the command to force a KMS push like this:
cmd.exe dir "C:\apps\MSOffice\Office15\"
cmd.exe "cscript ospp.vbs /act"
It gets executed but then it just hangs and nothing happens. If I execute the command by hand it works without problems.
This only occurs if I do it through powershell - running the command by hand works as intended.
thanks Micky!, /C helped me debugging.
solution is like this:
cmd.exe /c "cd C:\apps\MSOffice\Office15\"
cmd.exe /c "cscript.exe C:\apps\MSOffice\Office15\ospp.vbs /act"
need to execute the script VB handler separately from the VB script.
BTW: the reason why im doing this is because i have a build task that will execute this upon 20 build machines simultaneously.
Not sure why you would call CMD from PowerShell to execute commands that PowerShell can execute directly.
Anyway
Use /C to return when the command is performed
cmd.exe /C dir "C:\apps\MSOffice\Office15\"
cmd.exe /C "cscript ospp.vbs /act"

Is there a way to start commandline processes in a new commandline window from a powershell script?

I am using
Start-Process "<PathToFile>.bat"
For .bat files from a lengthy script in Powershell (v3). However, the commandline window pops up for a moment and is immediately closed and the process that normally runs on the commandline, runs in the background with no indication wether it's finished or if any errors occured.
Is there a way to force the command window to stay open until the user exits the window (after the .bat ran)? I suppose even if there is a way that the command window stays open, the PS script will continue to run in the background?
As said by Alex K CMD /K opens a CMD window and then keeps it open.
If you use CMD /C it will call the file, run the commands/process and then exit.
Sadly, it never worked for me with cmd /c. Since I also needed parameters to be handed over to the programm called from the commandline, I opted to write a temporary proxy bat that held the call to the program and the parameters.
Pseudocode here:
"ProgramName /Parameters" | Out-File DirOfTempBatFile -Encoding ascii
$output = Start-Process DirOfTempBatFile -wait
This calls the program (sqlplus with parameters) with keeping the command window open. Additionally, you can access the output of the sqlplus debug messages via $output.