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

 喝喝88地盘 发布于 2023-02-12 16:37

命令

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

将列表写入output.txt,其中只有列名称将在表单中打印:

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.

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

1 个回答
  • 而不是foreach { $_.Name }foreach { "$($_.Name),xxx" }

    2023-02-12 16:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有