簡體   English   中英

Powershell 變量替換與 Start-Process 和 ArgumentList

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM