0

I have this array:

$server = @("value 1", "value 2")

and this array, that will be a part of a table:

$HtmlHeader= @"
<tr>
<th class="vcenter" colspan="5">$server[$i]</th>
</tr>
<tr>
<th class="colnames">Nome</th>
<th class="colnames">Dimensione (MB)</th>
<th class="colnames">VM</th>
<th class="colnames">Stato VM</th>
<th class="colnames">Data Creazione</th>
</tr>
"@

but the output is:

value 1 value 2[1]

and

value 1 value 2[2]

How can I fix it, the second array is part of a for cycle and $i is defined in that.

2 Answers 2

1

You probably need to change $server[$i] to $($server[$i]) in the multiline string. However, it's hard to tell, since you didn't show that much of your code.

Sign up to request clarification or add additional context in comments.

1 Comment

it's exactly what i want :) ty!
0

You can't embed arrays in strings like that. PowerShell interpets $server[$i] as different strings, i.e as $server then [$i]. This results in the entire array getting spit out, followed by the value of $idx. I like to use format strings instead of embedding variables directly in strings. That way I don't have to worry about all the ways embedding variables can go wrong:

$HtmlHeader= @"
<tr>
  <th class="vcenter" colspan="5">{0}</th>
</tr>
<tr>
  <th class="colnames">Nome</th>
  <th class="colnames">Dimensione (MB)</th>
  <th class="colnames">VM</th>
  <th class="colnames">Stato VM</th>
  <th class="colnames">Data Creazione</th>
</tr>
"@ -f $server[$i]

Comments

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.