PowerShell syntax error on { used in foreach loop

时间:2015-04-24 21:32:40

标签: powershell

I found some awesome code on this page that has syntax error when trying to use with PowerShell version 3 or 4 can anyone help to fix the issues?

http://jeffwouters.nl/index.php/2012/06/scvmm-custom-inventory-about-vm-and-vhd-with-powershell/

$VMS = Get-VM -VMMServer localhost | Sort-Object -Property Name -Descending
“<HTML><HEAD><TITLE>VM Inventory</TITLE></HEAD><BODY><TABLE BORDER=1>”  |     Out-File $Env:Temp\VMInventory.html
“<TR><TD>VM Name</TD><TD>VM Memory</TD><TD>VHD Name</TD><TD>VHD         Length</TD><TD>VHD Maximum</TD><TD>VHD Available</TD><TD>VHD Free %</TD></TR>”     | Out-File $Env:Temp\VMInventory.html -Append
foreach ($VM in $VMS)
{
“<TR><TD>”+$VM.Name+”</TD><TD>”+$VM.Memory+”MB</TD><TD>.</TD><TD>.</TD><TD>.    </TD><TD>.</TD><TD>.</TD></TR>” | Out-File $Env:Temp\VMInventory.html     -Append
foreach ( $VHD in $VM.VirtualHarddisks)
{
$VHDLength = ($VHD.Size / 1GB)
$VHDSize = “{0:N2}” -f $VHDLength
if ($VHD.VHDType -eq “DynamicallyExpanding”)
{
$BackgroundColor = “#FF0000″
$VHDMaximumSize = “{0:N2}” -f ($VHD.MaximumSize / 1GB)
$VHDAvailable = “{0:N2}” -f (($VHD.MaximumSize – $VHD.Size) /1GB)
$VHDFree = “{0:N2}” -f (100 – (($VHD.Size * 100) / $VHD.MaximumSize))
“<TR><TD>.</TD><TD>.</TD>
<TD bgcolor=”+$BackgroundColor+”>”+$VHD.Name+”</TD>
<TD bgcolor=”+$BackgroundColor+”>”+$VHDSize+”GB</TD>
<TD bgcolor=”+$BackgroundColor+”>”+$VHDMaximumSize+”</TD>
<TD bgcolor=”+$BackgroundColor+”>”+$VHDAvailable+”</TD>
<TD bgcolor=”+$BackgroundColor+”>”+$VHDFree+” %</TD></TR>” | Out-File         $Env:Temp\VMInventory.html -Append
}
else
{
“<TR><TD>.</TD><TD>.</TD><TD>”+$VHD.Name+”</TD><TD>”+$VHDSize+”GB</TD><TD>.<    /TD><TD>.</TD><TD>.</TD></TR>” | Out-File $Env:Temp\VMInventory.html -Append
}
}
“<TR>” | Out-File $Env:Temp\vminventory.html -Append
}
“</TABLE><BODY></HTML>” | Out-File $Env:Temp\VMInventory.html -Append
Invoke-Expression $Env:Temp\vminventory.html

3 个答案:

答案 0 :(得分:2)

唯一的问题似乎是智能报价。我还花时间缩进代码,因为它使代码更具可读性。我自己无法运行,但现在语法正确。

$VMS = Get-VM -VMMServer localhost | Sort-Object -Property Name -Descending
"<HTML><HEAD><TITLE>VM Inventory</TITLE></HEAD><BODY><TABLE BORDER=1>" | Out-File $Env:Temp\VMInventory.html
"<TR><TD>VM Name</TD><TD>VM Memory</TD><TD>VHD Name</TD><TD>VHD         Length</TD><TD>VHD Maximum</TD><TD>VHD Available</TD><TD>VHD Free %</TD></TR>"     | Out-File $Env:Temp\VMInventory.html -Append
foreach ($VM in $VMS){
"<TR><TD>"+$VM.Name+"</TD><TD>"+$VM.Memory+"MB</TD><TD>.</TD><TD>.</TD><TD>.    </TD><TD>.</TD><TD>.</TD></TR>" | Out-File $Env:Temp\VMInventory.html     -Append
    foreach ( $VHD in $VM.VirtualHarddisks){
    $VHDLength = ($VHD.Size / 1GB)
    $VHDSize = "{0:N2}" -f $VHDLength
        if ($VHD.VHDType -eq "DynamicallyExpanding"){
            $BackgroundColor = "#FF0000"
            $VHDMaximumSize = "{0:N2}" -f ($VHD.MaximumSize / 1GB)
            $VHDAvailable = "{0:N2}" -f (($VHD.MaximumSize – $VHD.Size) /1GB)
            $VHDFree = "{0:N2}" -f (100 – (($VHD.Size * 100) / $VHD.MaximumSize))

            "<TR><TD>.</TD><TD>.</TD>
            <TD bgcolor="+$BackgroundColor+">"+$VHD.Name+"</TD>
            <TD bgcolor="+$BackgroundColor+">"+$VHDSize+"GB</TD>
            <TD bgcolor="+$BackgroundColor+">"+$VHDMaximumSize+"</TD>
            <TD bgcolor="+$BackgroundColor+">"+$VHDAvailable+"</TD>
            <TD bgcolor="+$BackgroundColor+">"+$VHDFree+" %</TD></TR>" | Out-File $Env:Temp\VMInventory.html -Append
        }else{
            "<TR><TD>.</TD><TD>.</TD><TD>"+$VHD.Name+"</TD><TD>"+$VHDSize+"GB</TD><TD>.<    /TD><TD>.</TD><TD>.</TD></TR>" | Out-File $Env:Temp\VMInventory.html -Append
        }
    }
    "<TR>" | Out-File $Env:Temp\vminventory.html -Append
}

"</TABLE><BODY></HTML>" | Out-File $Env:Temp\VMInventory.html -Append
Invoke-Expression $Env:Temp\vminventory.html

如果你能够做到这一点,那么这段代码看起来会受益于创建自定义对象和使用ConvertTo-HTML。是的,你会失去颜色格式,但它只是一个FYI。

答案 1 :(得分:2)

这些引号。尝试用普通的直引号替换它们。 “”vs&#34;&#34;

Mac OSX

Alt +]会产生一个开头的单引号(')

Alt + Shift +]产生一个收尾单曲引号(')

Alt + [产生一个开头双曲引语(“)

Alt + Shift + [产生一个收盘双曲引语(“)

<强>窗

Alt + 0145会产生一个开头的单引号(')

Alt + 0146产生一个收盘单曲引号(')

Alt + 0147产生一个开头的双重引号(“)

Alt + 0148会产生一个结束双重引号(“)

答案 2 :(得分:0)

这里的问题不是使用智能报价本身; Powershell支持对字符串使用智能引号U+201CU+201D。问题特别在于:

$BackgroundColor = “#FF0000″

使用有效的unicode 'LEFT DOUBLE QUOTATION MARK' (U+201C)启动字符串,但尝试使用unicode 'DOUBLE PRIME' (U+2033)字符结束它,该字符不会被识别为字符串引号。

Powershell规范中的相关规则是:

double-quote-character:
    "   (U+0022)
    Left double quotation mark (U+201C)
    Right double quotation mark (U+201D)
    Double low-9 quotation mark (U+201E)

和单引号字符串:

single-quote-character:
    '   (U+0027)
    Left single quotation mark (U+2018)
    Right single quotation mark (U+2019)
    Single low-9 quotation mark (U+201A)
    Single high-reversed-9 quotation mark (U+201B)