我想以下面的格式输出我的txt文件中的内容。PowerShell输出文件格式
"Web-ISAPI-Filter", "Web-Mgmt-Tools"
我使用下面的脚本,但想要一些帮助来修改输出格式,如果有的话。
Get-Service | Out-File process.txt
我想以下面的格式输出我的txt文件中的内容。PowerShell输出文件格式
"Web-ISAPI-Filter", "Web-Mgmt-Tools"
我使用下面的脚本,但想要一些帮助来修改输出格式,如果有的话。
Get-Service | Out-File process.txt
这应做到:
(Get-Service | %{ '"{0}"' -f $_.Name }) -join ',' | Out-File 'process.txt'
我能想到做到这一点的最佳方法是使用自定义函数来分析字符串到一个单一的线。你可以简化这一点,如果你想要的话,可以把它作为一个单线程来使用,但是这样可以更容易阅读。
function Build-String{
[cmdletbinding()]
param(
[parameter(valuefrompipeline=$true)]$string
)
Begin{
$result = ""
}
Process{
foreach($s in $string){
$result += "$($s),"
}
}
end{
return $result.TrimEnd(",")
}
}
Get-Service | select -ExpandProperty Name | Build-String | Out-File 'process.txt'
我想说的简洁和可读性Martin Brandl's helpful answer之间的良好平衡是要走的路,但这里有一个较短的选择:
"`"$((Get-Service).Name -join '", "')`"" > process.txt
感谢马丁。这很好。 – riftha