Reputation: 761
I am having trouble getting the --%
parameter to work as expected. My $TaskParams
variable has the character '<'
which is interpreted as a redirection by powershell, therefore needs to be escaped.
However the following does not work:
$CreateTask = Start-Process PowerShell.exe "$ScriptLocation --% $TaskParams" -Wait -PassThru
Without the --%
, and when I manually remove any '<'
characters, it works:
$CreateTask = Start-Process PowerShell.exe "$ScriptLocation $TaskParams" -Wait -PassThru
error received:
Start-Process : A positional parameter cannot be found that accepts argument
'--%'.
note: I am using PS 5.0
Am I using the --%
parameter wrong? Any help or pointers is appreciated. Thanks
Upvotes: 1
Views: 1913
Reputation: 24535
You do not need to spin up a new copy of powershell.exe or use Start-Process
to run a script from within another script. Just put the script command and its parameters as a line from within the other script. For example, suppose you have script2.ps1:
param(
[String] $Name
)
Write-Host "Hello, $Name"
Now suppose you also have script1.ps1:
Write-Host "This is script1.ps1"
.\Script2.ps1 -Name "Bill Stewart"
Write-Host "script1.ps1 is finished"
If you now run script1.ps1:
PS C:\> .\Script1.ps1
This is script1.ps1
Hello, Bill Stewart
script1.ps1 is finished
Upvotes: 3
Reputation: 36297
If you really want to use Start-Process
you could encode the argument, and run it as such. I use something similar to this when elevating past UAC:
$Code = ". '$ScriptLocation' $TaskParams"
$Encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
Start-Process PowerShell.exe -ArgumentList "-EncodedCommand",$Encoded -Wait -PassThru
I'm fairly certain that would accomplish what you're looking for.
Upvotes: 3
Reputation: 47792
The stop-parsing symbol --%
only works when calling executables directly or with the call operator &
; it's not for use when calling PowerShell scripts / functions / cmdlets.
Upvotes: 4