PowerShell读取主机中的多色文本,同时将输出写入CSV

时间:2017-04-12 00:34:10

标签: powershell

我有以下行,通过要求用户回答Flag来在现有CSV中创建y/n数据:

$_ | Add-Member -MemberType NoteProperty -Name Flag -Value (Read-Host "Is $($_.SO) a Planned SO? (y/n) ($($_.ProdProj) - $($_.SrvcTask))")

我希望它略有不同。我已经重新组织了文本,使其更具可读性并为关键点添加了颜色,但似乎无法使其发挥作用。

$_ | Add-Member -MemberType NoteProperty -Name Flag -Value (Write-Host "Is " -NoNewline);(Write-Host "$($_.SO)" -ForegroundColor "Red" -nonewline);(Write-Host " a Planned SO?")
(Write-Host "(" -nonewline);Write-Host "$($_.ProdProj) - $($_.SrvcTask)" -ForegroundColor "Yellow" -NoNewline; Write-Host ")"
Read-Host "(y/n)"

也尝试过:

(Write-Host "SAM reports this task is " -nonewline);(Write-Host "open (O)" -ForegroundColor "Red" -nonewline);(Write-Host ".
Press " -NoNewline);(Write-Host "[ENTER]" -ForegroundColor "Yellow" -NoNewline); Write-Host " to confirm or type correct status"
        $_ | Add-Member -MemberType NoteProperty -Name COPFVR -Value (Read-Host).ToUpper()}

问题正确显示,但似乎整个文档无法创建数据并将其正确粘贴,因为导出的数据只会转到CSV,标题为Length,值为1 。我记得那件事正在发生,因为它计算数据而不是输出数据,对吧?第一行不能是Read-Host,因为在请求答案之前它不会显示问题的其余部分。由于;的使用,我无法在整个语句中添加括号。有没有办法使这项工作?

一如既往地感谢帮助。

1 个答案:

答案 0 :(得分:2)

所以你想要一次做两件事。如何解除它们并使用foreach循环?

# pseudocode
$things = mycsv

# loop over the CSV rows
foreach($thing in $things){

    # deal with color formatted text here.
    Write-Host "Is " -NoNewline;
    Write-Host "$($thing.SO)" -ForegroundColor "Red" -nonewline);
    Write-Host " a Planned SO?";
    Write-Host "(" -nonewline);
    Write-Host "$($thing.ProdProj) - $($thing.SrvcTask)" -ForegroundColor "Yellow" -NoNewline;
    Write-Host ")";

    # deal with CSV modification here
    $thing | Add-Member -MemberType NoteProperty -Name Flag -Value (Read-Host "(y/n)")
}