0

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
6
  • 1
    Does replacing all of those "smart quotes" with regular quotes help? I realize the formatting problems aren't yours but formatting the code more readably would likely help. Commented Apr 24, 2015 at 21:39
  • There's at least three different characters being used for smart quotes here. , , and . Mr. Wouters needs to fix his site so it doesn't corrupt code he posts. Commented Apr 24, 2015 at 21:41
  • It's more than just the quotes. There is a string in the middle with nested quotes that need to be escaped/dealt with starts with “<TR><TD>.</. Fixing that makes it look better. Commented Apr 24, 2015 at 21:42
  • The comments on that page actually discuss two different quoting issues. And Mr. Wouters claimed he doesn't see the problem (presumably because he didn't actually look at the code in the post). But everything about that code and site makes me cringe. Commented Apr 24, 2015 at 21:44
  • @Matt After I replace the quotes characters, ISE stops complaining about syntax errors. The one at the end of $BackgroundColor = “#FF0000″ is particularly nasty because it's different than the rest and appears as $BackgroundColor = “#FF0000? in ISE. Commented Apr 24, 2015 at 21:45

3 Answers 3

2

The only issue appears to be the smart quotes. I also took the time to indent the code as it makes it more readable. I cannot run this myself but the syntax appears correct now.

$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

If you feel up to it this code looks like it would benefit from creating custom object and using ConvertTo-HTML. Yes you would lose the colour formatting but it is just an FYI.

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

5 Comments

You really don't need to escape those quotes in the HTML. <TD bgcolor=#FF0000> is just as valid HTML as <TD bgcolor="#FF0000">
@BaconBits I only did that since they were there. The can be removed for sure. Code seems fine without them.... Yeah it was just the smart quotes.
Fair enough. Either way it's kind of a huge mess for something that's moderately less useful than Export-Csv, and only moderately nicer than ConvertTo-Html or Out-GridView.
@BaconBits Just added a part about ConvertTo-HTML. Not sure its worth the colouring not to consider it. To each their own.
I tested Matt's solution. It worked great. How could I use convertTo-html to make this look more attractive?
2

It's those curly quotes. Try replacing them with normal straight quotes. ““ vs ""

Mac OSX

Alt + ] produces an opening single curly quote ( ‘ )

Alt + Shift + ] produces a closing single curly quote ( ’ )

Alt + [ produces an opening double curly quote ( “ )

Alt + Shift + [ produces a closing double curly quote ( ” )

Windows

Alt + 0145 produces an opening single curly quote ( ‘ )

Alt + 0146 produces a closing single curly quote ( ’ )

Alt + 0147 produces an opening double curly quote ( “ )

Alt + 0148 produces a closing double curly quote ( ” )

Comments

1

The problem here isn't the use of smart quotes per se; Powershell supports the use of smart quotes U+201C and U+201D for strings. The problem is specifically the line:

$BackgroundColor = “#FF0000″

which starts the string with a valid unicode 'LEFT DOUBLE QUOTATION MARK' (U+201C) but tries to end it with the unicode 'DOUBLE PRIME' (U+2033) character which isn't recognised as a string quote.

The relevant rule from the Powershell specification is:

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

and for single quoted strings:

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)

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.