简体   繁体   English

在PowerShell中添加字符行尾

[英]Add character end of line in PowerShell

I'm looking for a solution for the following challenge: 我正在寻找以下挑战的解决方案:

I run the following command in PowerShell. 我在PowerShell中运行以下命令。

Import-Module servermanager
Get-WindowsFeature | where {$_.Installed -eq "True"} | ft DisplayName, Installed > output.txt

Now I want to add a character at the end of each row. 现在,我想在每行的末尾添加一个字符。 How can I do that? 我怎样才能做到这一点?

I think I have to add the content into an array, but I don't know how to finish the code. 我想我必须将内容添加到数组中,但是我不知道如何完成代码。

At the end I have to load the content into the EventViewer. 最后,我必须将内容加载到EventViewer中。 If I send it directly, the event description isn't formatted well. 如果我直接发送,则事件说明的格式不正确。

You could add another field to the records like this: 您可以像这样在记录中添加另一个字段:

Get-WindowsFeature | ? { $_.Installed } |
    select DisplayName, Installed, @{n='Other',e={'c'}} | ft

It sounds like instead of using ft > output.txt , you want something like: 听起来好像不是要使用ft > output.txt ,而是要这样:

foreach { echo ( $_.DisplayName + " " + $_.Installed + " extra_stuff" ) } > output.txt

It doesn't give you a nicely formatted table though. 但是,它不能为您提供格式良好的表。

It's a little outside the scope of what you directly asked, but I'd suggest skipping the 'write to text file' stage and pass directly to the destination. 这有点超出您直接要求的范围,但是我建议跳过“写入文本文件”阶段,然后直接传递到目标位置。

Import-Module servermanager
$installedFeatures = @()    # Creates a blank array for the features
$output = @()    # Creates a blank array for formatted results

$yourChar    # The character/ string you want to add at the end of each row

$installedFeatures = Get-WindowsFeature | Where-Object {$_.Installed -eq "True"}
foreach($feature in $installedFeatures)
{
    $output += "$($feature.displayName) $($feature.installed) $yourChar"
}

Once you've iterated through all the Windows features, your $output variable will have an array of strings in the format of displayName installed $yourChar . 遍历所有Windows功能之后, $output变量将具有一个以displayName installed $yourChar格式的字符串数组。 You can then write to disk, or send the object somewhere else (this is the beauty of PowerShell objects!). 然后,您可以写入磁盘,或将对象发送到其他地方(这就是PowerShell对象的美!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM