简体   繁体   English

启动过程和停止分析(-%)参数

[英]Start-Process and the Stop Parsing (--%) parameter

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. 我的$TaskParams变量具有字符'<'$TaskParams将其解释为重定向,因此需要进行转义。

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 注意:我正在使用PS 5.0

Am I using the --% parameter wrong? 我是否使用--%参数错误? Any help or pointers is appreciated. 任何帮助或指针表示赞赏。 Thanks 谢谢

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. 调用PowerShell脚本/函数/ cmdlet时不使用它。

If you really want to use Start-Process you could encode the argument, and run it as such. 如果您确实想使用Start-Process ,则可以对参数进行编码,并以此方式运行它。 I use something similar to this when elevating past UAC: 通过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. 我相当确定这可以满足您的需求。

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. 您不需要Start-Process powershell.exe的新副本,也不需要使用Start-Process从另一个脚本中运行一个脚本。 Just put the script command and its parameters as a line from within the other script. 只需将脚本命令及其参数放在另一个脚本中的一行即可。 For example, suppose you have script2.ps1: 例如,假设您有script2.ps1:

param(
  [String] $Name
)
Write-Host "Hello, $Name"

Now suppose you also have script1.ps1: 现在假设您还有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: 如果现在运行script1.ps1:

PS C:\> .\Script1.ps1
This is script1.ps1
Hello, Bill Stewart
script1.ps1 is finished

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM