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.

4

2 回答 2

1

您可能需要在多行字符串中更改$server[$i]为。$($server[$i])但是,这很难说,因为您没有展示那么多代码。

于 2013-07-04T15:23:59.010 回答
0

您不能将数组嵌入到这样的字符串中。PowerShell 解释$server[$i]为不同的字符串,即 as $serverthen [$i]。这导致整个数组被吐出,然后是 的值$idx。我喜欢使用格式字符串而不是直接在字符串中嵌入变量。这样我就不必担心嵌入变量可能出错的所有方式:

$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]
于 2013-07-04T19:45:41.363 回答