如何使用 PowerShell 连接循环中的字符串



命令

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

写入要输出的列表.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的方法。

就我而言,必须在循环中进行串联,而不是之后进行文件操作。

而不是foreach { $_.Name },写foreach { "$($_.Name),xxx" }

最新更新