0

We're creating a big script and need to generate some HTML-code. This all works fine and I've collected all the info I need in the array $HTMLcode.

I'm still a bit of a noob in array/string manipulation, so I was wondering if its possible to insert a line of text above every line in the array HTMLcode that starts with the text ERROR?

$HTMLcode=@()
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"

Desired result would be:

$HTMLcode=@()
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"

Thank you for your help.

3 Answers 3

1

Unless you take care, if an operation (cmdlet) in a pipe returns two objects they will be both be placed in the pipe as separate objects.

eg.

2,4,6 | % { %_/2; $_ }

returns

1
2
2
4
3
6

Thus you could do something like:

$updatedHtmlCode = $HTMLCode |
       Select-Object { if ($_ -clike 'ERROR*') { '--------------------------<br>' } $_ }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the help Richard, but it's not really giving the desired result. It's making 6 different arrays with a comma seperator between them.. Which makes it not clean for use in HTML.
@DarkLite1 something more is going on then. Just tested "foo","bar","bax" | % { if ($_ -eq 'bar') { "xyzzy" } $_ } | % { write-host ($i++); $_ } and the counts clearly show the last select-object executes 4 times.
I'm now trying another approach which might be more readable by converting the array to a list $list = New-Object System.Collections.ArrayList(,$HTMLcode) and then insert the vallue with $list.Insert(2,"text"). Now I only need to figure out how to find the correct ID number Int StartIndexvalue for all text starting with ERROR, as it doesn't accept text strings.
Hi Richard, your way is indeed the best way of doing it. Thank you for your help, I marked your answer as Solved.
1

Finally found this answer:

$Array = @"
computer1
computer2
computer2
"@ -split "`n" | % { $_.trim() }

The trick is the split and trim. This makes it work down the pipeline. Source: http://pleasework.robbievance.net/howto-easily-convert-block-of-text-into-an-array-in-powershell/

Comments

0

Another possibility:

$htmlcode = @()
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"

$htmlcode  -replace '^(?=ERROR)',"--------------------------<br>`n"


--------------------------<br>
ERROR: Problem 1<br>
You did this wrong.. <br>
--------------------------<br>
ERROR Problem 2<br>
Something happened here.. <br>
--------------------------<br>
ERROR Porblem 3<br>
Did you try.. <br>

This doesn't add any additional elements to the array, it just replaces the Error lines with a new string that contains the additional text with an embedded newline before the original text.

For html, it's probably going to get sent through Out-String when you're done, and the final result should be the same as if you'd inserted new lines into the array.

1 Comment

Awesome! This works great too, thank you for the tip on -replace :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.