How to call .cmd file as administrator? - powershell

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.

Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd

Related

Powershell scriptopen a window and send command to window

I'm trying to open a CMD as system using PowerShell and then send a command to that system window.
what i've got so far is this:
$opensystempromt = '"/c PsExec.exe -is cmd.exe'
[System.Diagnostics.Process]::Start('cmd.exe',$opensystempromt)
$systemcommand = 'CustomerService.exe deploy aa.txt bb.txt'
what i can't figure out is how to send the system command to the newly opened CMD.
and there's no documentation for it anywhere, that i could find
I'd use Start-Process to open cmd.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k Insert your variables here}
with -verb runas you'll open cmd as an administrator. ArgumentList is your "command part". /k leaves the cmd open, after the commands are done. If you want cmd to close instead, use /c.

Run a Powershell script from Batch file with elevated privileges?

I want to run example.ps1 from code.bat
I realize that the start command would run it but require that it be run as admin, how can I do this?
In your batch file:
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"
...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.
Full explanation:
Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.
We can't call Start-Process from a batch file as it's a PowerShell command.
But we can run powershell from a batch file, then using the -command parameter to run Start-Process.
We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

How to run PowerShell Start-Process without closing the output window?

I am trying to run the following PowerShell command in CMD:
powershell -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
I'm running this in PowerShell so that I can get the UAC (for elevated privileges). I'm not sure if there is an equivalent in CMD.
Now, I run the PowerShell script from within a batch file, so that I can double-click and execute. (or put it in the $Path location and call it from anywhere)
But the problem is as soon as it finishes running, it immediately closes, and I cannot see the build error message if any.
How can I wait or pause when MSBuild.exe has finished executing in a new window?
The noexit command keeps your PowerShell window open.
powershell -noexit -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
in your batch file, add pause to the end
powershell -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
pause
The issue you're seeing is the command window closing after executing everything in the batch file, rather than powershell closing the shell after it executes.

Start-Process -wait doesn't work when script is launched from command prompt opened with runas or as a scheduled task

I've got a script that's I want to run as a scheduled task, but it's not doing the thing it's supposed to. I'm trying to call an executable with Start-Process and the -Wait switch before continuing. Line is
Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Wait
If I call it from a command prompt, ie:
powershell .\script.ps1
it works. It runs the command and waits for it to finish before moving on. There's more to the script that has to be run after that command is finished. The problem is that when it's a scheduled task, it doesn't wait. Doing some basic troubleshooting, I first tried opening a cmd window with runas using the scheduled task account, named "Scripts." So I run
runas /env /user:Scripts cmd
to open a command prompt window with the task account. From that command prompt, I try again the "powershell .\script.ps1" and this time, it doesn't wait. It runs the command and moves on immediately before the command is finished. So I thought it might be an issue with the "Scripts" account, until I opened a new command prompt with runas Administrator
runas /env /user:Administrator cmd
When I call the script from this Administrator command prompt, the -Wait switch is also ignored, and the script moves along immediately after calling it without waiting for it to finish.
The odd part about this is that when I call it from the command prompt from Administrator account without doing runas, it works. Same account, two different results. Any ideas as to what the hell is going on here, and equally importantly, how to fix it?
OS is Server 2008 R2, running powershell 3.0
Can't tell you why it's doing it, but I think this might work around it:
$proc = Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Passthru
do {start-sleep -Milliseconds 500}
until ($proc.HasExited)
The -Passthru switch will make it return a Process object for the process, and you can test that to see when the process has exited.

Run powershell in new window

I would like to run new powershell window with parameters. I was trying to run following:
powershell -Command "get-date"
but everything happens in same console. Is there a simple way to do this?
To open a new PowerShell Window from PowerShell:
Start-Process PowerShell
Or my personal favorite:
Start-Process PowerShell -WindowStyle Maximized
Then you could typeGet-Datewithout having to deal with the -ArgumentList's tendency to close itself. I still haven't found a way to open a new PowerShell process with the -ArgumentList Parameter, without it immediately closing after it runs. For Instance:
Start-Process PowerShell -ArgumentList "Get-Date"
or simply
Start-Process PowerShell -ArgumentList Get-Date
Will Close Immediately after running the process.
In order to get it to wait before closing you could add:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Since the -Wait Parameter seems to do nothing at all in this case.
FYI - PowerShell Suggested Syntax is actually:
Start-Process -FilePath "powershell.exe"
But since PowerShell is a standard Windows Application in the %SystemRoot%\system32 Environment Variables the command line(s) should recognize a simple
Powershell
Command
Use the start command. In a CMD prompt, try:
start powershell -noexit -command "get-date"
For Start/Run (or Win+r) prompt, try:
cmd /c start powershell -noexit -command "get-date"
The -noexit will tell Powershell to, well, not to exit. If you omit this parameter, the command will be executed and you are likely to just see a glimpse of a Powershell window. For interactive use, this is a must. For scripts it is not needed.
Edit:
start is an internal command for CMD. In Powershell it is an alias for Start-Process. These are not the same thing.
As for why the window is black, that's because the shortcut for Powershell.exe is configured to set the background blue.
To call a PowerShell (PS) script in a second terminal window without exiting, you can use a script similar to:
Start-Process PowerShell -ArgumentList "-noexit", "get-date"
or if you need to run another script from a specific location:
Start-Process PowerShell -ArgumentList "-noexit", "-command .\local_path\start_server.ps1"