【问题标题】:How do I concatenate a string within a loop with PowerShell?如何使用 PowerShell 在循环中连接字符串?
【发布时间】:2013-12-22 18:30:53
【问题描述】:

命令

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt

向 output.txt 写入一个列表,其中仅列 Name 将打印在表单中:

a
b
c
...

现在我想要实现的是在某种循环中将,xxx 附加到每一行,以便我得到以下内容:

a,xxx
b,xxx
c,xxx
...

我尝试附加字符串,但这似乎不起作用:

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt | Add-Content output.txt ",xxx"

我对PowerShell真的不熟悉,也没有找到连接,xxx的方法。

在我的情况下,必须在循环中进行连接,而不是之后的文件操作。

【问题讨论】:

    标签: string loops powershell foreach concatenation


    【解决方案1】:

    foreach { "$($_.Name),xxx" }代替foreach { $_.Name }

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2015-12-16
      • 2013-07-07
      • 1970-01-01
      相关资源
      最近更新 更多