【发布时间】:2020-11-26 02:30:00
【问题描述】:
我正在 Powershell 中编写服务安装程序脚本,该服务需要复杂的引用命令行。
所以我试图简化它并将每个选项分解为单个变量,但是在创建最终的命令行字符串时,字符串不会转义它们的引号。因此命令行不起作用。
我想将所有选项分开,以便其他管理员可以配置和安装服务,而无需担心转义引号。
我想我需要先执行搜索和替换或使用特定于 shell 的安全/转义字符串命令来操作各个字符串。
不知道服务的命令行是怎么解析的,所以不知道用什么shell转义方法。
我已经对字符串中的引号进行了搜索,但它们似乎从来没有处理带引号的字符串嵌套在带引号的字符串中。
这是我的安装脚本,我确实可以控制应用程序服务,所以如果您知道将参数输入服务的更好方法,也将不胜感激。
$installpath = (get-location)
$name="landingZone"
$displayName="LandingZone Starter"
$description="Sony CI automated download client"
$sessionuser="Engineering"
$processname="explorer"
$logname="Landing Zone"
$programpath="C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe"
$programarguments='"C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe -jar C:\Program Files (x86)\Sony CI\Sony-Ci-LZ-1.4.22\Sony-Ci-LZ-1.4.22.jar"'
$WorkDirectory="C:\Program Files (x86)\Sony CI\Sony-Ci-LZ-1.4.22"
$Visible=1
$TerminateTimeout=1000
# Arg! help me!
$binpath = $installpath.toString() + "\applicationservice.exe ""SessionUser=$sessionuser"" ""ProcessName=$processname"" ""LogName=$logname"" ""ProgramPath=$programpath"" ""ProgramArguments=$programarguments"" ""WorkDirectory=$workdirectory"" Visible=$visible TerminateTimeout=$terminatetimeout"
New-Service -Name $name -BinaryPathName $binPath -DisplayName $displayname -Description $description -StartupType Manual
谢谢
【问题讨论】:
-
您需要创建的格式应该是什么?
"SessionUser"="Engineering"?或"SessionUser='Engineering'"或?? -
它们在命令行上被解析(我假设是 cmd),只需要用空格分隔。使用此代码在 OnStart 方法中读取它们;
string[] clArgs = Environment.GetCommandLineArgs();所以命令行传递的最后一个参数不应该有任何引号;SessionUser=Engineering
标签: powershell quotes string-literals