簡體   English   中英

如何使用PowerShell在循環中連接字符串?

[英]How do I concatenate a string within a loop with PowerShell?

命令

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

在我的情況下,必須在循環內進行串聯,而不是隨后進行文件操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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