Trouble running script and passing the parameter. Powershell - 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.. ?

Related

How to pass arguments to PowerShell script executed as administrator from batch script?

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.
Batch script
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"' -Verb
RunAs"
Powershell
param
(
[string]$FolderPath ='D:\batch_scripting'
)
echo $FolderPath
pause
I will add further functionalities in these scripts later. But I have to figure out this first.
The script can be executed if -FolderPath argument is not passed.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb
RunAs"
I have also gone through following questions but this does not work for me.
I found the solution, although it's weird.
When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"
It works fine now.

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}"

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\"}"

Get return code from "second" Powershell process

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".

Syntax error when starting powershell from bat file

I want to start a powershell script with RunAs from a bat file. This works.
#echo
SET "InstallerFolder=\\dc01\e\script"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Script.ps1""' -Verb RunAs}";
But if i add:
-RedirectStandardOutput ""%InstallerFolder%\node.txt""
It breaks.
So the line looks like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-RedirectStandardOutput ""%InstallerFolder%\node.txt"" -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -Verb RunAs}";
And resuslts in an powershell error which is gone so fast i can't see it.
Probably syntax?
Help much appreciated!
Thanks.
You get an error because powershell.exe does not have a -RedirectStandardOuptut parameter. (See Technet).
Also your syntax is way off (but since i dont see any reason to start powershell to start powershell again i wont bother explaining the syntax errors).
If you want to use RunAs from the cmd use it directly. For more info see Technet (again).
Also you can redirect output in Batch Files with > or >> if you want to append.