【问题标题】:Powershell variable replacement with Start-Process and ArgumentList使用 Start-Process 和 ArgumentList 替换 Powershell 变量
【发布时间】:2020-08-17 13:00:10
【问题描述】:

我有这段获取 FQDN 并将其保存到变量的 powershell 代码...效果很好:

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

LogWrite "[+] 系统全限定主机名已被检测为:$FQDN"

但是,当我去插入 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”的文字字符串,并且代码的 sn-p 实际上并没有进行变量替换。

我在这里缺少什么?我什至还尝试了反引号双引号"$FQDN"。我希望将 REPLACEME 字符串替换为实际主机名 + 域(如 hostname.domain.com)或 $FQDN 变量中设置的内容。 ArgumentList 引号需要保持不变,因为我只能通过引用 ArgumentList 以 ' 开头并以 """' 结尾的方式来让我的命令正常工作。任何帮助将不胜感激。

【问题讨论】:

    标签: powershell variables quotes


    【解决方案1】:

    单-在 PowerShell 中引用的字符串 ('...') 是 PowerShell 中的逐字(字面量)字符串 - 根据设计,嵌入变量引用的扩展(插值)或执行表达式。

    要获得后者,您必须使用 可扩展 字符串,即 double-引号 ("...");例如,
    "...SourceName=$FQDN&Component1... "

    由于您的字符串包含嵌入 " 字符,因此对我们来说最简单的here-string 变体可扩展字符串(@"<newline>...<newline>"@):

    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 字符串文字的更多信息,请参阅this answer 的底部;官方帮助主题为about_Quoting_Rules。

    【讨论】:

    • 非常感谢。这对我来说很完美。非常感谢。
    • 很高兴听到这个消息,@user13458199;我的荣幸。请允许我在下一条评论中给你标准的建议给新人:
    • 如果您accept 回答,您将通过向他们展示有效的方法来帮助未来的读者。要接受答案,请单击答案左侧大数字下方的大 ✓ 符号(您将获得 2 点声望)。如果您至少有 15 个声望点,您还可以对其他有用的答案进行投票(也可以选择已接受的答案)。如果您的问题尚未解决,请提供反馈,或者,如果您自己找到了解决方案,请self-answer。
    【解决方案2】:

    这是使用反引号的方式:

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

    【讨论】:

    • 谢谢托马斯。感谢您的反馈。
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2018-11-02
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多