Get return code from "second" Powershell process - powershell

I am trying to execute a powershell in a cmd.
powershell.exe -noprofile -command "&{ start-process -FilePath 'powershell.exe' -ArgumentList '-noprofile get-service' -verb RunAs}"
This executes a powershell.exe and a powershell.exe again as an admin.
What I want is the errorcode from the "second" powershell.exe and NOT from the "first".

Related

Executing PowerShell from a Window command batch script using the Start-Process command with named parameters

I am attempting to execute a Powershell Start-Process command with a named parameter list from a Windows command script. I tried including the named parameters;(-productname, -platform, -version, -exepath, -exe, -installMode) in the ArgumentList, however, they when the PowerShell process starts it appears to fail to see them. The command I am using is below:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""setadminstartandcreateshortcut.ps1"" -productname %PRODUCTNAME% -platform %PLATFORM% -version %VERSION% -exepath %TARGET% -exe %PRODUCTNAME%.exe -installMode %INSTALLMODE%' -Verb RunAs}"

Run a powershell command as administrator in a bat file

I have this powershell command that must run as administartor in powershell. I want to run as a batch file as administator. Would you please help me?
Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }
To avoid the problem of double/double quote, i suggest you to put your command in a script.ps1 then do this command:
PowerShell -windowstyle hidden -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -windowstyle hidden -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""YourScript.ps1""' -Verb RunAs}"
Try this batch
<# : batch script
#echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
setlocal
cd %~dp0
powershell -executionpolicy remotesigned -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
endlocal
goto:eof
#>
# here write your powershell commands...
Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage "$($_.InstallLocation)\appxmanifest.xml" -DisableDevelopmentMode -register }
save it as .bat or .cmd and run it.
You can run it like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command ""& {Get-AppxPackage -allusers ContentDeliveryManager | foreach {Add-AppxPackage ""$($_.InstallLocation)\appxmanifest.xml"" -DisableDevelopmentMode -register }"" -Verb RunAs}"
UPDATE
Fixed double quotes

How do I run a PowerShell script as administrator using a shortcut?

I'm trying to run a PowerShell script as administrator using a shortcut. I have tried many ways, but it still does not work:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1'
With this command, it will create two PowerShell windows and one window will close.
I also tried this one:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit Start-Process powershell.exe -Verb RunAs -File 'C:\project\test.ps1'
Can some one please help?
Tl;dr
This will do the trick:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"
Explanation
First, you have to call PowerShell to be able to execute Start-Process. You don't need any additional paramters at this point, because you just use this first PowerShell to launch another one. You do it like this:
powershell.exe -Command "& {...}"
Inside the curly braces you can insert any script block. First you will retrieve your current working directory (CWD) to set it in the new launched PowerShell. Then you call PowerShell with Start-Process and add the -Verb RunAs parameter to elevate it:
$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList ...
Then you need to add all desired PowerShell parameters to the ArgumentList. In your case, these will be:
-ExecutionPolicy ByPass -NoExit -Command ...
Finally, you pass the commands that you want to execute to the -Command parameter. Basically, you want to call your script file. But before doing so, you will set your CWD to the previously retrieved directory and THEN call your script:
Set-Location $wd; C:\project\test.ps1
In total:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file?

The following script works fine in Powershell:
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Webrequest
'https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1' -OutFile
C:\script.ps1; C:\script.ps1
I'm trying to convert it so that it runs as a CMD/BAT file. I simply need to double click to run it. I need the CMD/BAT file to run Powershell as administrator and from there, it will run the script from above. Here's what I have. It'll just quit straight away without doing anything.
powershell -ExecutionPolicy Bypass -c Start-Process -Verb RunAs -Wait
powershell.exe '-ExecutionPolicy Bypass -Noexit -c Set-Location
"\"\\\"%CD%\\\"\""; -c "& Invoke-Webrequest
\"https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1\"
-OutFile C:\script.ps1; C:\script.ps1" '
Update:
I got it working now. The full output looks like this:
powershell -Command "& ({Start-Process powershell -Verb RunAs -ArgumentList '-ExecutionPolicy Bypass -NoExit -Command Invoke-WebRequest -Uri https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1 -OutFile C:\script.ps1; C:\script.ps1'})"
If you use the -NoExit parameter when starting powershell.exe PowerShell should not close after the script has finished running. Afterwards if you do not see errors, you can look at the $error variable, to see any errors which occurred when running your script.
You are trying to run your PowerShell code, using PowerShell.exe from command prompt, so you should be able to use the same code as you did before, when you were running your code directly in PowerShell before.
To my understanding you want to run powershell code in a batch-file.well Depending upon your preference there are multiple solutions to this problem(well i haven't checked any of them):
Solution:1
make a batch file and give your powershell script to it as a parameter.
Without Admin access:
#ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
PAUSE
With Admin access:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
Solution:2
If you don't want an external .ps1 file,then you can try this.just save it as something.bat
powershell -command if ($true)^
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Webrequest 'https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1' -OutFile C:\script.ps1; C:\script.ps1^
This should do the trick:
powershell -Command "& {Start-Process powershell -Verb RunAs -ArgumentList '-ExecutionPolicy Bypass -NoExit -Command Set-Location -Path C:\whatever\working\directory\you\need; Invoke-WebRequest -Uri https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1 -OutFile C:\script.ps1; C:\script.ps1'}"
Explanation:
When executing commands in PowerShell using the -Command argument, ExecutionPolicies do not apply as your are executing a single command and not a script. Even the execution of a scriptblock consisting of multiple commands counts as the execution of a single command. That's why you can directly call PowerShell from the command prompt like this (without anything else):
powershell -Command ...
-Command expects - to read from stdin or a scriptblock (read more). Scriptblocks have to be enclosed in curly braces ({...}). If you pass a scriptblock from the command prompt to PowerShell, you also have to add the call operator &:
powershell -Command "& {...}"
As you need an elevated PowerShell, you start a new PowerShell process from the previous PowerShell with Start-Process in combination with the -Verb RunAs argument. You add all arguments that you want to pass to the elevated PowerShell to the -ArgumentList argument:
... Start-Process powershell -Verb RunAs -ArgumentList '...' ...
As you want to call a script file, you now need the corresponding ExecutionPolicy. If you don't want to change it on the system, you can bypass the ExecutionPolicy with -ExecutionPolicy Bypass as you already did. And you also add -NoExit here. To pass your desired PowerShell commands, you use the -Command argument again. This time, you don't need the call operator and you can also omit the curly braces as we are now in PowerShell and not in the command prompt anymore.
... -ExecutionPolicy Bypass -NoExit -Command Set-Location -Path C:\whatever\working\directory\you\need; Invoke-WebRequest -Uri https://blah.blob.core.windows.net/laps/AutoAutoPilot.ps1 -OutFile C:\script.ps1; C:\script.ps1 ...

Trouble running script and passing the parameter. Powershell

I am having trouble with this method for launching my powershell script:
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""c:\Program Files\FileMaker\FileMaker Server\Data\Scripts\updateM2.ps1""' -Verb RunAs}";
The above works.. But I would like to pass the parameter to the script...:
UpdateSandP
So where do I put this.. ?