![](/img/trans.png)
[英]Powershell - Start-Process ArgumentList accepts only single variable with spaces
[英]Powershell variable replacement with Start-Process and ArgumentList
我有一点 powershell 代码,它获取 FQDN 并将其保存到变量中......效果很好:
$FQDN=(Get-WmiObject win32_computersystem).DNSHostName.ToLower()+"."+(Get-WmiObject win32_computersystem).Domain
LogWrite "[+] 系统完全限定的主机名已被检测为:$FQDN"
但是,当我 go 插入 FQDN 变量的字符串时,我无法使其正常工作。 我尝试了反引号、双引号和单引号,但没有成功:
Start-Process -Wait $OUTPATH64 -ArgumentList '/s /v"/qn INSTALLDIR=\"C:\Program Files\IBM\WinCollect\" LOG_SOURCE_AUTO_CREATION_ENABLED=TRUE LOG_SOURCE_AUTO_CREATION_PARAMETERS=""Component1.AgentDevice=DeviceWindowsLog&Component1.Action=create&Component1.LogSourceName=REPLACEME&Component1.LogSourceIdentifier=REPLACEME&Component1.Dest.Name=test.domain.com&Component1.Dest.Hostname=test.domain.com&Component1.Dest.Port=514&Component1.Dest.Protocol=TCP&Component1.Log.Security=true&Component1.Log.System=true&Component1.Log.Application=true&Component1.Log.DNS+Server=false&Component1.Log.File+Replication+Service=false&Component1.Log.Directory+Service=false&Component1.RemoteMachinePollInterval=3000&Component1.EventRateTuningProfile=Default+(Endpoint)&Component1.MinLogsToProcessPerPass=100&Component1.MaxLogsToProcessPerPass=150"""'
在您看到“REPLACEME”字符串的地方,我需要将其替换为 $FQDN 变量字符串。 每当我使用反引号$FQDN
或双引号“$FQDN”甚至单引号 '$FQDN' 时,我都会得到“$FQDN”的文字字符串,而代码片段实际上并没有进行变量替换。
我在这里想念什么? 我什至还尝试过反引号双引号"$FQDN"
。 我希望将 REPLACEME 字符串替换为实际主机名 + 域,例如 hostname.domain.com 或 $FQDN 变量中设置的内容。 ArgumentList 引号需要保持不变,因为我只能通过引用 ArgumentList 以 ' 开头并以 """' 结尾的方式来让我的命令正常工作。任何帮助将不胜感激。
PowerShell 中的单引号字符串 ( '...'
) 是 PowerShell 中的逐字(文字)字符串 - 根据设计,不执行嵌入式变量引用或表达式的扩展(插值)。
要获得后者,您必须使用双引号 ( "..."
) 的可扩展字符串; 例如,
"...SourceName=$FQDN&Component1... "
由于您的字符串包含嵌入的"
字符,因此对我们来说,可扩展字符串 ( @"<newline>...<newline>"@
) 的here-string变体是最简单的:
Start-Process -Wait $OUTPATH64 -ArgumentList @"
/s /v"/qn INSTALLDIR=\"C:\Program Files\IBM\WinCollect\" LOG_SOURCE_AUTO_CREATION_ENABLED=TRUE LOG_SOURCE_AUTO_CREATION_PARAMETERS=""Component1.AgentDevice=DeviceWindowsLog&Component1.Action=create&Component1.LogSourceName=$FQDN&Component1.LogSourceIdentifier=$FQDN&Component1.Dest.Name=test.domain.com&Component1.Dest.Hostname=test.domain.com&Component1.Dest.Port=514&Component1.Dest.Protocol=TCP&Component1.Log.Security=true&Component1.Log.System=true&Component1.Log.Application=true&Component1.Log.DNS+Server=false&Component1.Log.File+Replication+Service=false&Component1.Log.Directory+Service=false&Component1.RemoteMachinePollInterval=3000&Component1.EventRateTuningProfile=Default+(Endpoint)&Component1.MinLogsToProcessPerPass=100&Component1.MaxLogsToProcessPerPass=150"""
"@
如果您使用常规的双引号字符串( "..."
),则必须将嵌入的"
字符转义为`"
(或""
)。
有关 PowerShell 字符串文字的更多信息,请参阅此答案的底部; 官方帮助主题是about_Quoting_Rules 。
这就是它应该如何使用反引号:
Start-Process -Wait $OUTPATH64 -ArgumentList "/s /v /qn INSTALLDIR=`"C:\Program Files\IBM\WinCollect\`" LOG_SOURCE_AUTO_CREATION_ENABLED=TRUE LOG_SOURCE_AUTO_CREATION_PARAMETERS=`"Component1.AgentDevice=DeviceWindowsLog&Component1.Action=create&Component1.LogSourceName=$FQDN&Component1.LogSourceIdentifier=$FQDN&Component1.Dest.Name=test.domain.com&Component1.Dest.Hostname=test.domain.com&Component1.Dest.Port=514&Component1.Dest.Protocol=TCP&Component1.Log.Security=true&Component1.Log.System=true&Component1.Log.Application=true&Component1.Log.DNS+Server=false&Component1.Log.File+Replication+Service=false&Component1.Log.Directory+Service=false&Component1.RemoteMachinePollInterval=3000&Component1.EventRateTuningProfile=Default+(Endpoint)&Component1.MinLogsToProcessPerPass=100&Component1.MaxLogsToProcessPerPass=150`""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.