【发布时间】:2022-06-11 02:05:04
【问题描述】:
当我在没有管理员的情况下通过批处理文件运行脚本时,它会传递参数,但是当我以管理员身份运行脚本时,它不会传递参数。
我正在尝试下面链接中的命令,但没有成功:
run-script-within-batch-file-with-parameters
以管理员身份通过批处理文件执行脚本的命令:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "D:\z_Batchs e Scripts\Batchs\Normaliza_LUFS\ArqsNorms_LUFS_pass.ps1' '%_vLUF%' -Verb RunAs}"
%_vLUF% 是要传递的参数。
错误信息:
No line:1 character:4
+ & {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
powershell脚本中接收参数的命令:
Param(
[decimal]$env:_vLUF
)
可能有什么问题,批处理文件中的命令还是 powershell 脚本中的命令?
Test:
当脚本执行时,不是管理员,通过批处理文件和powershell脚本中的参数定义为:
powershell中的参数:
Param(
[decimal]$env:_vLUF
)
以管理员身份运行脚本的批处理文件中的命令:
powershell.exe -executionpolicy remotesigned -File "D:\z_Batchs e Scripts\Batchs\Normaliza_LUFS\ArqsNorms_LUFS_pass.ps1" %_vLUF%
注意:
无需将命名参数与目标参数名称一起使用。
结论:
当脚本运行时,无需管理员身份,通过批处理文件,即使脚本中使用的参数被定义为环境参数,它也可以正常工作,例如:[decimal]$env:_vLUF 并且无论参数值是否为负,例如:@ 987654332@.
为什么不以管理员身份运行脚本时,Powershell 会正确解释参数中的减号,而当以管理员身份运行时,它不能正确解释减号,这是我留给专家的问题!
不过,@mklement0 先生很好地回答了我的问题。
【问题讨论】:
-
您的报价是个问题。在你的路径之前,你有一个双引号终止那里的第一个调用。
-
@Abraham Zinala 正确的方法是什么?
-
转义引号(也提供它的结束引号),或转义路径中的空格。
-
@Abraham Zinala 我在路径的末尾加了双引号,是吗?
"D:\z_Batchs e Scripts\Batchs\Normaliza_LUFS\ArqsNorms_LUFS_pass.ps1"' -
查看 mklements 答案。
标签: powershell batch-file windows-10-universal powershell-5.0