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"
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"
Related
I try to install .msu package using Poweshell. My code:
$Args = #(('"' + $Path_Temp + '\' + $Hotfix[1] + '.msu' + '"'), '/norestart')
echo $Args
Start-Process -FilePath ($Env:SystemRoot + '\System32\wusa.exe') -ArgumentList $Args -Verb RunAs -Wait
echo outputs this:
"C:\Users\MyUser\AppData\Local\Temp\Windows6.1-KB2758857-x64.msu"
/norestart
All looks good, but in the end wusa warns me about incorrect usage. It works well when I pass only the first argument (which is the path to .msu file) to ArgumentList. My code does not work when I pass multiple arguments, why? I use Powershell v5.1.14409.1005
I have a script that takes in a max of 3 parameters - username, password and distro.
The script needs to run as administrator since it checks certain things are enabled and can only be done as an admin.
# the command line is:
# linux-docker <username> <password> <distro>
# if no distro is specified then Debian is used.
param ([Parameter(Mandatory)]$username, [Parameter(Mandatory)]$password, $distro='Debian')
# check if we are in admin mode and switch if we aren't
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-Not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs;
Read-Host -Prompt "Failure: Press Enter to exit"
exit;
}
}
# ... rest of script
However restarting the script prompts the user for username and password. I would like to pass the arguments to the promoted script.
I first thought that adding $username,$password and $distro to the Start-Process command.
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $username $password $debian" -Verb RunAs;
but that exits with the "Failure" message.
So I looked at -ArgumentList but that dies processing that line:
Start-Process : A positional parameter cannot be found that accepts argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Users\Graham Reeds\Documents\linux-docker.ps1"'.
At C:\Users\Graham Reeds\Documents\linux-docker.ps1:15 char:9
+ Start-Process powershell.exe "-NoProfile -ExecutionPolicy Byp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
Failure: Press Enter to exit:
It is probably something simple but I am a noob with Powershell.
Using the -ArgumentList parameter should work, but for the parameters sent to the script, you need to
get their names and values from the BoundParameters hash:
$currentPrincipal = [Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (!$isAdmin) {
$argList = '-NoLogo', '-NoProfile', '-NoExit', '-ExecutionPolicy Bypass', '-File', ('"{0}"' -f $PSCommandPath)
# Add script arguments
$argList += $MyInvocation.BoundParameters.GetEnumerator() | ForEach-Object {"-$($_.Key)", "$($_.Value)"}
try {
Start-Process PowerShell.exe -WorkingDirectory $pwd.ProviderPath -ArgumentList $argList -Verb Runas -ErrorAction Stop
# exit the current script
exit
}
catch {
Write-Warning "Failed to restart script '$PSCommandPath' with runas"
}
}
# rest of the script
P.S. Personally I like to always type-cast parameters and when possible make it clear in what order the mandatory parameters should be given if not used Named:
param (
[Parameter(Mandatory = $true, Position = 0)][string] $username,
[Parameter(Mandatory = $true, Position = 1)][string] $password,
[string] $distro='Debian'
)
I am trying to run a powershell script which will have run only after passing a parameter value as an argument. The script has few commands that run only with elevated access so trying to use -runAs verb like below:
PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~reset-password-services_logs.ps1 'h5rU!J5L_sitL0Y'""' -Verb RunAs}"
I need to pass the argument 'h5rU!J5L_sitL0Y' with the script in order to successfully execute the process. Error i get is below:
Start-Process : A positional parameter cannot be found that accepts argument
'h5rU!J5L_sitL0Y'.
At line:1 char:4
+ & {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypas ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterB
indingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.StartProcessCommand
I am a student and am very new to PowerShell.
I am trying to get my msi to download remotely but i keep encountering an error.
A positional parameter cannot be found that accepts argument '\\share\folder\Path to msi'.
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
+ PSComputerName : RemoteDesktopName
Here is my script:
$msi = #(\\share\folder\Path to msi)
Invoke-Command -ComputerName PCname -ScriptBlock {param($msi) Start-Process msiexec.exe /i "\\Path to msi" /qn /passive -Wait
Start-Sleep 5 } -ArgumentList $msi
Could anyone please help me? Any feedback will be greatly appreciated.
You dont have to make $msi an array if the only thing you parse is a string of the Path. Also, why would you use "\Path to msi" inside the Invoke-Command if you parse $msi?
Edit: You should probaly parse the arguments to the msiexec.exe via -Argumentlist.
Try this:
$msi = "Path to msi"
Invoke-Command -ComputerName PCname -ScriptBlock {param($msi) Start-Process msiexec.exe -Wait -ArgumentList "/I $msi /qn /passive"} -ArgumentList $msi
Here's the code
$tool = "E:\Experiments\Popup\latest\xperf.exe"
$toolOutput = "XPerfOutput.log"
$toolError = "XPerfError.log"
$command = "-stop"
$x = Start-Process -FilePath $tool -ArgumentList $command -RedirectStandardOutput $toolOutput -RedirectStandardError $toolError -WindowStyle Hidden -PassThru -Wait
And Here's there error:
Start-Process : Parameter set cannot be resolved using the specified named parameters. At E:\Experiments\Popup\asd.ps1:9 char:1
+ Start-Process -FilePath $tool -ArgumentList $command -RedirectStandardOutput $toolOutput RedirectStandardError $toolError -WindowStyle Hidden
-PassThru -Wait
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
I want to run the process in a hidden window, wait for it to return and get the error, output and exit code.
According to the documentation for Start-Process, the combination of the redirection parameters (RedirectStandardOutput and RedirectStandardError) and the WindowStyle parameter is invalid since they exist in separate parameter sets.
This means that they cannot be used together. This is why you're receiving that particular error.