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

PowerShell读取主机中的多色文本,同时将输出写入CSV,powershell,Powershell,我有下面一行代码,它通过要求用户回答y/n,在现有CSV中创建标志数据: $_ | Add-Member -MemberType NoteProperty -Name Flag -Value (Read-Host "Is $($_.SO) a Planned SO? (y/n) ($($_.ProdProj) - $($_.SrvcTask))") 我希望它的呈现方式略有不同。我重新组织了文本,使其更具可读性,并为关键点添加了颜色,但似乎无法实现这一点 $_ | Add-Member -Mem

我有下面一行代码,它通过要求用户回答
y/n
,在现有CSV中创建
标志
数据:

$_ | 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()}
该问题显示正确,但似乎使整个文档无法创建数据并正确粘贴,因为导出的数据只是以标题
Length
和值
1
进入CSV。我记得那是因为它在计算数据而不是输出数据,对吗?第一行不能是
读取主机
,因为在请求答案之前,它不会显示问题的其余部分。由于使用了
,我不能在整个语句周围加括号。有没有办法让这一切顺利


一如既往,我们非常感谢您的帮助。

因此,您正在尝试一次完成两件事。如何将它们解耦并使用
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)")
}

我看到了,但这似乎有些过分,我没有让它与它的例子一起工作。是的。这就解决了问题。老实说,我已经通过这种方式在另一行上解决了这个问题,但是当我发布代码时,我注意到我没有纠正我抱怨的那行代码不起作用。因为我是个白痴。谢谢你,伙计!这很有效。