简体   繁体   English

Powershell 变量替换与 Start-Process 和 ArgumentList

[英]Powershell variable replacement with Start-Process and ArgumentList

I have this bit of powershell code that gets the FQDN and saves it to a variable...works great:我有一点 powershell 代码,它获取 FQDN 并将其保存到变量中......效果很好:

$FQDN=(Get-WmiObject win32_computersystem).DNSHostName.ToLower()+"."+(Get-WmiObject win32_computersystem).Domain

LogWrite "[+] The system fully qualified hostname has been detected as: $FQDN" LogWrite "[+] 系统完全限定的主机名已被检测为:$FQDN"

However, when I go to insert the string of the FQDN variable, I can not get it to work correctly.但是,当我 go 插入 FQDN 变量的字符串时,我无法使其正常工作。 I have tried back ticks, double and single quotes with no success:我尝试了反引号、双引号和单引号,但没有成功:

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"""'

Where you see the "REPLACEME" string, I need that replaced with the $FQDN variable string.在您看到“REPLACEME”字符串的地方,我需要将其替换为 $FQDN 变量字符串。 Whenever I use backticks $FQDN or with double quotes"$FQDN" or even single quotes '$FQDN' I get the literal string of "$FQDN" and the snippet of code does not actually do the variable replacement.每当我使用反引号$FQDN或双引号“$FQDN”甚至单引号 '$FQDN' 时,我都会得到“$FQDN”的文字字符串,而代码片段实际上并没有进行变量替换。

What am I missing here?我在这里想念什么? I have also even tried backticks double quotes "$FQDN" .我什至还尝试过反引号双引号"$FQDN" I want the REPLACEME string to be replaced with something like the actual hostname + domain like hostname.domain.com or what is set in the $FQDN variable.我希望将 REPLACEME 字符串替换为实际主机名 + 域,例如 hostname.domain.com 或 $FQDN 变量中设置的内容。 The ArgumentList quotes need to stay the same as I can only get my command to work correctly by quoting the ArgumentList the way it is starting with a ' and ending with """'. Any help would be greatly appreciated. ArgumentList 引号需要保持不变,因为我只能通过引用 ArgumentList 以 ' 开头并以 """' 结尾的方式来让我的命令正常工作。任何帮助将不胜感激。

Single -quoted strings ( '...' ) in PowerShell are verbatim (literal) strings in PowerShell - by design, no expansion (interpolation) of embedded variable references or expression is performed. PowerShell 中的引号字符串 ( '...' ) 是 PowerShell 中的逐字(文字)字符串 - 根据设计,不执行嵌入式变量引用或表达式的扩展(插值)。

To get the latter, you must use an expandable string, which is double -quoted ( "..." );要获得后者,您必须使用引号 ( "..." ) 的可扩展字符串; eg,例如,
"...SourceName=$FQDN&Component1... "

Since your string contains embedded " characters, it is simplest to us the here-string variant of an expandable string ( @"<newline>...<newline>"@ ):由于您的字符串包含嵌入"字符,因此对我们来说,可扩展字符串 ( @"<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"""
"@

If you used a regular double-quoted string ( "..." ), you'd have to escape the embedded " characters as `" (or "" ).如果您使用常规的双引号字符串( "..." ),则必须将嵌入的"字符转义为`" (或"" )。

See the bottom section of this answer for more information about PowerShell's string literals;有关 PowerShell 字符串文字的更多信息,请参阅此答案的底部; the official help topic is about_Quoting_Rules .官方帮助主题是about_Quoting_Rules

This is how it should work using back ticks:这就是它应该如何使用反引号:

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.

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