So basically I need to run my ps1 script from command line and pass my custom arguments into my script. My script expects a String array, but when I run the command, I get an error that a positional parameter cannot be found that accepts argument 'X1'
This is my command line:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File "C:\Program Files\MSBuild\MyScript.ps1" -builds "X1” “X2" "X3" "X4"
My understanding is it knows what to do with the first parameter 'X1' but not the second and so it crashes? Any ideas whay?
You need to use the -Command parameter instead of the -File parameter. Notice the change in behavior from the screenshot below, and the sample script.
[CmdletBinding()]
param (
[int[]] $MyInts
)
foreach ($MyInt in $MyInts) {
$MyInt + 1;
}
I can't quite explain why -file is not working but that parameter has other known issues. When you use it you don't get a proper exit code from PowerShell. Using -command does work:
Powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -Command "& {& 'C:\Program Files\MSBuild\myscript.ps1' -builds x1,x2,x3}"
Related
I'm writing a Batch File, and in this batch file i execute a script.
Batch File:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Public\File\SomeScript.ps1""' -Verb RunAs}"
Now this works fine.
Is it possible to execute the SomeScript.ps1 with parameters ?
Like
#echo off
echo %1
echo %2
echo %3
echo %4
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Public\File\SomeScript.ps1 Arg1 %1 Arg2 %2 Arg3 %3 Arg4 %4""' -Verb RunAs}"
The Batch File echos the values I'm giving. But after that nothing happens. So I'm not sure if I'm passing the Arguments correctly.
Any help appreciated :)
The arguments must be outside the quoted script path, ""C:\Public\File\SomeScript.ps1""
To be safe, you should double-quote the arguments too.
Use \" to escape embedded double quotes when calling the CLI of Windows PowerShell (powershell.exe) as in your case), and "" for PowerShell (Core) (pwsh.exe).
Note: Depending on the specific values of the pass-through arguments, use of \" with Windows PowerShell can break, due to cmd.exe's limitations; the - ugly - workaround is to use "^"" (sic).
Therefore (limited to passing %1 for brevity):
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -ExecutionPolicy Bypass -File \"C:\Public\File\SomeScript.ps1\" Arg1 \"%1\"' -Verb RunAs"
As an aside: There's no reason to use & { ... } in order to invoke code passed to PowerShell's CLI via the -Command (-c) parameter - just use ... directly, as shown above. Older versions of the CLI documentation erroneously suggested that & { ... } is required, but this has since been corrected.
I'm trying to call these powershell scripts in a batch file, as follows:
Powershell.exe -executionpolicy remotesigned -File d:\1.ps1
Powershell.exe -executionpolicy remotesigned -File d:\2.ps1
Powershell.exe -executionpolicy remotesigned -File d:\3.ps1
Powershell.exe -executionpolicy remotesigned -File d:\4.ps1
I run that batch file and is giving me the following error for 2.ps1,3.ps1 and4.ps1
the argument d:\2.ps1 for the parameter -file doesnt exist. Provide the path of access to the file d:\2.ps1 (2.3.4) existent as argument for the parameter -file"
The files are in the drives where i am calling them, 3 and 4 fails to execute because are connected to script 2.ps1.
I dont know what is the problem in calling 2.ps1, after 1.ps1 because it is the same commnd, if I execute the files manually, they run just fine.
Try calling it like this:
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -File "d:\1.ps1"
I am trying to call a powershell script using -command argument from a batch file. The powershell script accepts an argument which may contain spaces. The command fails to accept argument with spaces.
Here is there the batch file.
#echo off
set arg1=%*
call powershell.exe -ExecutionPolicy Bypass -command ".\build.ps1 %arg1%; exit $LASTEXITCODE"
exit /b %ERRORLEVEL%
This is how the batch file is called - .\build.bat '-testToolPath=as sdaf'
I am using -command instead of -file just to return the LASTEXITCODE
Have you tried to use escaped doublequotes?
call powershell -ExecutionPolicy Bypass -command ".\build.ps1 ^"%arg1%^"; exit $LASTEXITCODE"
Maybe you have to remove the other quotes before:
set "arg1=%arg1:'=%"
I tried to launch a long powershell script with the name "long name here.ps1" from command prompt. But I am also trying to ensure that it runs as an administrator command in powershell. I have all execution policies in powershell set accordingly I used the ss64 set-executionpolicy command guide for powershell to get powershell working. But I am trying to use the solution from another stackoverflow question that talks about running commands as administrator. I am running a batch script that needs to execute a powershell script (.ps1) as admin, and I don't mind if the user is prompted by UAC or for the password. I am currently using the following command:
powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file "C:\long name here.ps1"' -verb RunAs}"
I found this command at https://ss64.com/ps/powershell.html at the bottom where there are details on how to run a powershell command as administrator. The problem with that code is that my powershell script 1. has arguments, and 2. has a long name. I have tried many different iterations of this command with no success, and the ones that DON'T work are listed below:
powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file C:\long` name` here.ps1' -verb RunAs}"
powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file:"C:\long name here.ps1' -verb RunAs}"
Also, I am completely lost as to how to send arguments to the actual script.
If I'm reading your question correctly - powershell wont find the file as it stops reading the path name when it encounters a blank space?
The example given here specifies that; powershell commands to be run from command prompt as an administrator should have the following syntax:
powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs}"
Couple of ways to achieve what you're looking for. But the easiest method would be to escape the quotes using a ` character. So something similar to;
powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file `"C:\long file name.ps1`"' -verb RunAs}"
Also might be worth checking out other answers here
Use a Freeware Third Party Utility
If a freeware third-party executable is permissible, you can use a short tool I wrote called elevate32.exe (32-bit) and elevate64.exe (64-bit) to launch powershell.exe as administrator with the -File parameter and the script arguments you want to use:
elevate64 -- powershell.exe -File "<path>\<long script name>.ps1" -Arg "<long script argument>"
You can get the tool from www.westmesatech.com (copyrighted freeware, free to use anywhere, no installation needed).
Use a WSH Script
If you can't use an external executable, you can also do this (although it does not handle quoting in as robust a manner as the elevate tool's -- parameter) using a Windows Script Host (WSH) script, elevate.js:
var args = WScript.Arguments;
if ( args.Length >= 1 ) {
var exec = args.Item(0);
var cmdLine = "";
for (var i = 1; i < WScript.Arguments.Length; i++ ) {
cmdLine += cmdLine == "" ? '"' + args.Item(i) + '"' : ' "' + args.Item(i) + '"';
}
var shellApp = new ActiveXObject("Shell.Application");
shellApp.ShellExecute(exec, cmdLine, "", "runas");
}
You can call as follows:
wscript.exe "d:\path\elevate.js" powershell.exe -File "C:\long path\script name.ps1" "long script argument"
Self-Elevate your PowerShell Script
Another option is to write a self-elevating PowerShell script. You can check for elevation in the script; if not elevated, it can launch itself elevated and run any command you need. Example:
$isElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ( -not $isElevated ) {
Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}
& "d:\long path name\script name.ps1" "Long Argument 1" "Long Argument 2"
When you use PowerShell.exe -Command you don't need to use quotes. For example, you can run the following:
PowerShell.exe -Command Get-Service 'wuauserv'
Everything after -Command is interpreted as the command. Note also that double quotes in CMD need escaping with a backslash. Therefore:
powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\"' -Verb RunAs
If your file has arguments:
powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\" \"Arg1\" \"Arg2\"' -Verb RunAs
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.