Reputation: 45
I am new to Powershell and trying to run a PS script using another PS script to run it as admin in Jenkins, the script gives its desired output but it also gives the error in the output.
Powershell : Start-Process : A positional parameter cannot be found that accepts argument At line:4 char:1 + Powershell -Command "Start-Process powershell ""cd "F:\RiskLink\RiskL ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Start-Process :...cepts argument :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
Code:
Set-Location -Path F:\abd\abc
$path = ".\xyz.ps1"
Powershell -Command "Start-Process powershell ""cd "F:\abd\abc"; & $path"" -Verb RunAs"
Upvotes: 2
Views: 4493
Reputation: 30103
Long ago, I have created a simple command line parser (scripts cliparser.ps1
and cli parser.ps1
):
PS D:\PShell> Get-Content ".\cliparser.ps1"
if ( $args -ne $null ) { $aLen=$args.Count } else { $aLen=0 }
"$(Split-Path -Path $PSCommandPath -Leaf): $aLen parameters"
for ( $i = 0; $i -lt $aLen ; $i++ ) { "{0}. [{1}]" -f $i, $args[$i] }
Used instead of yours xyz.ps1
, it shows that something must go wrong not only with Start-Process
arguments but with quoting as well (shows supplied values -Verb
and RunAs
). Fixed with some little effort… Improved for space(s) in paths and file names, see the following script:
$cd = (Get-Location -PSProvider Filesystem).Path
$path = ".\cliparser.ps1"
'... original (known error)'
Write-Host Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
pause
'... fixed'
Write-Host Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
$path = ".\cli parser.ps1"
Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
'done'
Note that PShell's parameters -NoProfile
and -NoExit
are here merely for debugging purposes.
Result:
PS D:\PShell> D:\PShell\SO\53638416.ps1
... original (known error)
Powershell -Command Start-Process powershell "pushd D:\PShell; & .\cliparser.ps1" -Verb RunAs
Start-Process : A positional parameter cannot be found that accepts argument 'D:\PShell'.
At line:1 char:1
+ Start-Process powershell pushd D:\PShell; & .\cliparser.ps1 -Verb Ru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.Start
ProcessCommand
cliparser.ps1: 2 parameters
0. [-Verb]
1. [RunAs]
Press Enter to continue...:
... fixed
Powershell -NoProfile -Command Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'';' -Verb RunAs
done
PS D:\PShell>
Called (elevated) Powershell window looks as follows (Pop-Location
used to ensure that popd
ran well):
To run from a cmd
prompt and pass arguments to the called script:
powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
Works for pwsh
instead of powershell
as well:
pwsh -NoProfile -Command "Start-Process pwsh -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
Upvotes: 3