2

I'm trying to do some processing logic - running some commands in parallel based on the tree configuration CSV file:

Operation;Parent;Enabled;Propagated;Job_ID;Status;Started;Finished
CA1;n/a;Y;N;;;;
PROD1;n/a;Y;N;;;Y;
CON1;CA1;N;N;;;Y;
CON2;CON1;N;N;;;Y;

I load the file into the variable and then I'm trying to find the next step which needs to be processed:

$Data = Import-Csv -delimiter ";" .\config.csv
$NextStep = $Data | Select-Object -first 1 | Where-Object {$_.Started -eq ""}
$NextStepText = $NextStep.Operation | ft -autosize | out-string

The problem is that it seems like $NextStep.Operation contains new line character. When I display it I get:

PS C:\temp\SalesForce> $NextStep.operation
CA1

PS C:\temp\SalesForce> $NextStep.Operation.Contains("`n")
False

Do you know what I'm doing wrong? I would like to display the content without the "dummy" new line character which is there even if contains method is saying it is not there.

Or please advise how to do it better. I'm still learning PowerShell; so far I just google the commands, and I'm trying to put it together.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pepco
  • 57
  • 1
  • 5

3 Answers3

0

The newline isn't in your data, it's being added by Out-String. Observe the output of the following (in particular, where you do and don't get the newline after CA1):

$Data = import-csv -delimiter ";" .\config.csv
$NextStep = $Data | select-object -first 1 | where-object {$_.Started -eq ""}
$NextStepText = $NextStep.Operation | ft -autosize | out-string
"hi"
$NextStepText
"hi"
$NextStep.Operation;
"hi"
$NextStep.Operation | ft -autosize
"hi"

You shouldn't be using Format-Table at that step (and Out-String is unnecessary in this script) if you intend to use $NextStepText for anything other than direct output later on. Consider Format-Table (or any of the Format-* cmdlets) the end of the line for usable data.

alroc
  • 27,574
  • 6
  • 51
  • 97
  • Alroc, thanks for the answer that will be the problem. I actually want to log the output for debugging purposes. Is there a way how I can remove this new line? Because now it messes my log file: [04.02.2014 22:36:36]:[-]: Program PS_Main.ps1 started. [04.02.2014 22:36:36]:[D]: Next step output: CA1 [04.02.2014 22:36:36]:[-]: Program PS_Main.ps1 finished – pepco Feb 05 '14 at 07:23
  • Don't use `format-table` then. Just use the raw value as I demonstrated. It's `format-table` that's adding the newline. – alroc Feb 05 '14 at 11:12
  • I have tried it without format-table and it is still adding new line. Anyway I solved it by adding -stream switch to the out-string: `$NextStepText = $NextStep.Operation | out-string -stream` – pepco Feb 05 '14 at 12:54
0

Why do you think that there is a new line character of some sort in there? If you are using the ISE then what you posted doesn't look like there is. It is normal to have a blank line between commands (in the v2/v3 ISE, not sure about v4), so what you posted would not indicate that it contains any new line characters.

You can always check the $NextStep.Operation.Length to see if it says 3 or 4. If there is a `n in there it'll show up in the length. For example (copied and pasted out of my v3 PS ISE):

PS C:\> $test = "Test`nTest2"

PS C:\> $test
Test
Test2

PS C:\> $test.Length
10

PS C:\>

That was to show that there is a new line character injected by following it with text, without any text following the new line character it looks like this:

PS C:\> $test = "Test`n"

PS C:\> $test
Test


PS C:\> $test.Length
5

PS C:\> 

You'll notice that there are 2 blank lines after the text "Test" on the second command. The first is the line injected into the variable, and the second is the obligatory line that PS puts in to show separation between commands.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
0

Out-String unexpectedly appends a trailing newline to the string it outputs.

  • This problematic behavior is discussed in GitHub issue #14444.

  • A simple demonstration:

    # -> '42<newline>'
    (42 | Out-String) -replace '\r?\n', '<newline>'
    

However, you neither need Format-Table nor Out-String in your code:

  • Format-* cmdlets output objects whose sole purpose is to provide formatting instructions to PowerShell's for-display output-formatting system. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing - see this answer for more information.

  • Out-String is capable of interpreting these formatting instructions, i.e. it does produce data - in the form of a single, multi-line string by default - that is the string representation of what would print to the display.

    • As such, the resulting string contains a representation for the human observer, not a structured text format suitable for programmatic processing.

    • In your case, Format-Table is applied to a string, which is pointless, because strings always render as themselves, in full (-AutoSize has no effect); piping to Out-String then in effect returns the original string with an (undesired) newline appended.

Therefore, use a simple variable assignment to store the property value of interest in a separate variable:

$NextStepText = $NextStep.Operation
mklement0
  • 382,024
  • 64
  • 607
  • 775