1

I have the following line which creates the Flag data in an existing CSV by asking the user to answer with y/n:

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

I would like it to present slightly differently. I've reorganized the text to be more readable and added color to key points, but can't seem to get this to work.

$_ | 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)"

Also tried:

(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()}

The question appears correctly, but it seems to make the entire document fail to create data and paste it appropriately because the exported data just goes to CSV with the header of Length and the value of 1. I remember that happening because it's counting the data and not outputting it, right? The first line can't be a Read-Host because then it doesn't display the rest of the question before requesting the answer. I can't put parentheses around the entire statement due to the usage of ;. Is there way to make this work?

Help is appreciated, as always.

Nate
  • 802
  • 1
  • 8
  • 28
  • I saw http://stackoverflow.com/questions/2688547/muliple-foreground-colors-in-powershell-in-one-command but that seems overkill and I didn't get it to work with its example. – Nate Apr 12 '17 at 00:53

1 Answers1

2

So you are trying to do 2 things in one go. How about de-coupling them and using a foreach loop?

# 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)")
}
Tatiana Racheva
  • 1,289
  • 1
  • 13
  • 31
G42
  • 9,791
  • 2
  • 19
  • 34
  • Yep. That fixed it. Honestly, I had already fixed the issue on another similar line by doing it that way, but when I went to post my code, I noticed I hadn't corrected the line I was complaining about not working. Because I am an idiot. Thanks man! This works. – Nate Apr 12 '17 at 22:08