【问题标题】:Display a value of a powershell array in html在 html 中显示 powershell 数组的值
【发布时间】:2013-07-04 14:41:35
【问题描述】:

我有这个数组:

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

还有这个数组,它将成为表格的一部分:

$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>
"@

但输出是:

值 1 值 2[1]

值 1 值 2[2]

我该如何修复它,第二个数组是 for 循环的一部分,$i 是在其中定义的。

【问题讨论】:

    标签: html arrays powershell output


    【解决方案1】:

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

    【讨论】:

    • 这正是我想要的:) ty!
    【解决方案2】:

    您不能在这样的字符串中嵌入数组。 PowerShell 将$server[$i] 解释为不同的字符串,即$server 然后[$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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多