Passing commands to cmd from powershell - powershell

I want to make changes to a registry key through this command:
REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f
This has to happen in a cmd, which i ran as administrator through powershell with this command in a batch file:
powershell.exe Start-Process cmd.exe -Verb runAs
I need a UAC Prompt for the user to input his admin credentials to make it as user friendly as possible.
Now my question: How do i pass the reg add command to the console which i started as administrator?

You need to pass your command in -ArgumentList parameter like this:
powershell.exe "Start-Process powershell -ArgumentList 'REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f' -Verb RunAs"
This will execute PowerShell which tries to execute another PowerShell window asking you for credentials and then execute REG ADD command and close PowerShell at the end.
Keep in mind that you don't have error handling or anything like this so you may want to add them later as they might be very useful.

Related

batch file to open two (or more) powershells in two (or more) specific folders

I am trying to do something really basic in a batch script, but it is not working.
I want to open two PowerShell windows each with a different current working directory.
I am using the following script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
Unfortunately it opens only the first window.
For sure it is only a syntax error but I could not find a solution yet.
As it happens often: I posted the question after researching a lot and then I immediately found the answer!
This is what you should write in your batch file:
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
I hope my answer will help someone in the same situation.
The Start command already has a 'working directory', option, /D.
#Start /D "C:\Users\User1" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
#Start /D "C:\Users\User2" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
You could of course do it with a simple For loop:
#For %%G In ("C:\Users\User1" "C:\Users\User2") Do #Start /D "%%~G" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
You just need to add "start" at the beginning

Running a Batch file using CMD as admin through Powershell

Don't ask me why, but I'm trying to run a batch file from CMD as admin by using Powershell. I have the following:
Start-Process -FilePath "cmd.exe" `
-ArgumentList "/K cd /d C:\Users\$($User)\Desktop\Activation\'win and off 2013 act.bat'" `
-Verb "runas"
The CMD opens as admin, but I get an error saying that "The system cannot find the path specified." I know it's something to do with how I've written the path to the batch file, but can't figure it out.
As I said in the comment, the quotations are at the wrong place, and I just checked, and cmd would not accept single quotes anyway. So use
"/K cd /d ""C:\Users\$User\Desktop\Activation\win and off 2013 act.bat"""

Calling a Powershell script from Batch script..But need the batch script to wait for the Powershell script to complete

I have a batch script being that has the following code in it:
Here is the batch script (Test.bat):
call powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
exit /b
The Powershell script (TheScriptToBeExecuted.ps1) has some wait built into it. But when this file is being called, the batch script is NOT waiting for the Powershell script to finish.
I ever tried:
START /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
To no effect.
When I double click on the Test.bat file, it seems to be waiting. I have checked out a lot of answers, but couldnt find any that correspond to this issue.
Please help.
start /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1 | ECHO > nul
Try the above. Holds the command until scripts executed, without the Teminate Batch Prompt. Use start /min /wait if not needing to display or interact with the script, or Alternately:
start /wait powershell.exe -executionpolicy remotesigned -WindowStyle Hidden -File TheScriptToBeExecuted.ps1 | ECHO > nul
-WindowStyle Value
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
* | * pipes the output of the command, however as your just starting powershell.exe and not using it in the context of utilizing a command like follows, It shouldn't be an issue.
powershell -command "((Get-date).AddDays(0)).ToString(':dd:MM:yyyy') | set-content 'captureVar.txt'" && set /p Yesterday=<captureVar.txt
It worked for the script I tested, but it's not thoroughly tested.
let me know how it goes.
I think the issue is more likely to be within "TheScriptToBeExecuted.ps1" than your bat. In fact several powershell command are working as Asyncronous.
I tested a Sleep-Start, which is not Asyncronous at all using the following code and the bat is waiting the end of it.
This is the code I used in my .bat
REM RUNTEST.BAT
time /T
echo started
powershell -executionpolicy remotesigned -File C:\Users\Alex\Desktop\DA_PROVARE\test.ps1
time /T
echo done
exit /b
The ps1 content is just one row, here below:
Start-Sleep -Seconds 25
The problem you are facing is probably because of the account under which the scheduler runs. Have faced similar problems where the .bat and/or .ps1 doesn't behave the same if launched by a scheduler or other tools
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'"
This should do the it, if not then we will add < NUL to the end
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'" < NUL
By using dot sourcing, we are opening powershell in the cmd window with the powershell reference in the above solution. Then the period allows the script to run in cmd window also. I did a double test and made a .ps1 with start-sleep -s 3 in it, and the above did wait for the powershell script to finish.
method 2
Using Start /wait
START /wait PowerShell.exe "& "'C:\path\TheScriptToBeExecuted.ps1'"
This method will not run inside the cmd window. It opens a powershell terminal and runs there then goes back to the bat.

Supress PowerShell prompting "Press any key to continue..."

I'm running a powershell script from a .bat to make it double click friendly. Here's the code:
powershell.exe -ExecutionPolicy unrestricted -NoExit -command "Start-Process powershell -Verb RunAs" {<Location of .ps1 file here>; <arguments>; pause }
It runs fine but when it completes it says "Press any key to continue" and the powershell window closes but I need it to remain open because at the end of the script it displays some additional commands we need to run to complete the process.
Any ideas? Thanks!
To keep a PowerShell window open (its session alive) after executing a command with -Command (or -File), use the CLI's -NoExit parameter, as already shown in your question for the outer PowerShell call.
You then have no need for an interactive delay-the-closing command such as pause (which is a function that simply calls $null = Read-Host 'Press Enter to continue...').
Therefore:
Add a -NoExit parameter to the inner, Start-Process-launched PowerShell instance as well or - more likely - instead; you need to scroll to the right to see it in the command below.
Remove the pause command from the inner command string.
Note: I'm assuming that you don't actually need the outer -NoExit, as it would block the batch file indefinitely by entering an interactive PowerShell session, so it is omitted from the command below.
powershell.exe -ExecutionPolicy unrestricted -command "Start-Process powershell -Verb RunAs '-NoExit', '-Command', '...'"
Note that there's no point in using a script block ({ ... }) with Start-Process, because only strings are supported as arguments by Start-Process; hence, '...' is used above to represent the command string to pass to the inner PowerShell instance.

How to call .cmd file as administrator?

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