I have a lengthy script that scans my VMware hosts and gathers information. Then an HTML page is created. It works. But it isn’t as efficient as it probably could be. My “problem” is i have the IF and 3 ElseIf statements that repeat the same code except for the

For Each Section of Original Script:

$vmServers = $vmServers | Sort

ForEach ($vmserver in $vmservers)

{	Write-Host "Testing $vmServer..."
$vmhost = get-vmhost -name $vmserver".eldoradocourt.org"
#$vmg = $vmserver | get-vmguest

$vmhostnametext=$vmserver
$vmhostConnectionStatetext=$vmhost.ConnectionState
$vmhostNumCputext=$vmhost.NumCpu
$vmhostCpuUsagetext=$vmhost.CpuUsageMhz
$vmhostCpuTotaltext=$vmhost.CpuTotalMhz
$vmhostMemUsuagetext=$vmhost.MemoryUsageGB  
 $vmhostMemUsuagetext = "{0:N3}" -f $vmhostMemUsuagetext 
$vmhostMemTotaltext=$vmhost.MemoryTotalMB
 $vmhostMemTotaltext = "{0:N3}" -f $vmhostMemTotaltext 
$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString("#.##")}
$vmhtotcpughz = (([int]$vmhost.cputotalmhz))
$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))
$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString("#.##")}
$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))
$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))
$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString("#.##")}

if ($vmhostConnectionStatetext -ne "Connected" )
					   
{$vmDetailHTML += "
  <tr>
<td rowspan=""2"">$vmhostnametext</td>
<td down"">$vmhostConnectionStatetext</td>
<td down"">$vmhostNumCputext</td>
<td down"">$vmhcpupercent</td>
<td down""></td>
</tr><tr><td down"">$vmhuptimetext</td><td down""></td>
<td down"">$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"

  }	

Elseif ($vmhmempercent -lt 50)
					   
{$vmDetailHTML += "
  <tr>
<td rowspan=""2"">$vmhostnametext</td>
<td up"">$vmhostConnectionStatetext</td>
<td up"">$vmhostNumCputext</td>
<td up"">$vmhcpupercent</td>
<td up""></td>
</tr><tr><td up"">$vmhuptimetext</td><td up""></td>
<td low"">$vmhmempercent</td>
<td up"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"

  }		  

Elseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))
					   
{$vmDetailHTML += "
  <tr>
<td rowspan=""2"">$vmhostnametext</td>
<td up"">$vmhostConnectionStatetext</td>
<td up"">$vmhostNumCputext</td>
<td up"">$vmhcpupercent</td>
<td up""></td>
</tr><tr><td up"">$vmhuptimetext</td><td up""></td>
<td med"">$vmhmempercent</td>
<td up"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"

  }		    

elseif ($vmhmempercent -gt 74) 
{$vmDetailHTML += "
 <tr>
<td rowspan=""2"">$vmhostnametext</td>
<td up"">$vmhostConnectionStatetext</td>
<td up"">$vmhostNumCputext</td>
<td up"">$vmhcpupercent</td>
<td up""></td>
</tr><tr><td up"">$vmhuptimetext</td><td up""></td>
<td high"">$vmhmempercent</td>
<td up"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"
}
}

Attempt to streamline:

$vmServers = $vmServers | Sort

ForEach ($vmserver in $vmservers)

{	Write-Host "Testing $vmServer..."
$vmhost = get-vmhost -name $vmserver".eldoradocourt.org"
#$vmg = $vmserver | get-vmguest

$vmhostnametext=$vmserver
$vmhostConnectionStatetext=$vmhost.ConnectionState
$vmhostNumCputext=$vmhost.NumCpu
$vmhostCpuUsagetext=$vmhost.CpuUsageMhz
$vmhostCpuTotaltext=$vmhost.CpuTotalMhz
$vmhostMemUsuagetext=$vmhost.MemoryUsageGB  
 $vmhostMemUsuagetext = "{0:N3}" -f $vmhostMemUsuagetext 
$vmhostMemTotaltext=$vmhost.MemoryTotalMB
 $vmhostMemTotaltext = "{0:N3}" -f $vmhostMemTotaltext 
$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString("#.##")}
$vmhtotcpughz = (([int]$vmhost.cputotalmhz))
$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))
$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString("#.##")}
$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))
$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))
$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString("#.##")}

if ($vmhmempercent -lt 50)
{
$percentclass = "low"
}
Elseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))
{
$percentclass = "med"
}
elseif ($vmhmempercent -gt 74) 
{
$percentclass = "high"
}
if ($vmhostConnectionStatetext -ne "Connected")
{
$connectionclass = "down"
}
Elseif ($vmhostConnectionStatetext -eq "Connected")
{
$connectionclass = "up"
}

					   
{$vmDetailHTML += "
  <tr>
<td rowspan=""2"">$vmhostnametext</td>
<td class="$connectionclass">$vmhostConnectionStatetext</td>
<td down"">$vmhostNumCputext</td>
<td down"">$vmhcpupercent</td>
<td down""></td>
</tr><tr><td down"">$vmhuptimetext</td><td down""></td>
<td class="$percentclass">$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"

  }	

}

I can’t get the class to change with my new script. Any thoughts?

2 Spice ups

This is the error i’m getting.

PS C:\ps_script\bldg> .\test.ps1
At C:\ps_script\bldg\Test.ps1:161 char:12
+ <td class="$connectionclass">$vmhostConnectionStatetext</td>
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '$connectionclass">$vmhostConnectionStatetext</td>
<td down"">$vmhostNumCputext</td>
<td down"">$vmhcpupercent</td>
<td down""></td>
</tr><tr><td down"">$vmhuptimetext</td><td down""></td>
<td class="$percentclass">$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"' in expression or statement.
At C:\ps_script\bldg\Test.ps1:161 char:28
+ <td class="$connectionclass">$vmhostConnectionStatetext</td>
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '">$vmhostConnectionStatetext</td>
<td down"">$vmhostNumCputext</td>
<td down"">$vmhcpupercent</td>
<td down""></td>
</tr><tr><td down"">$vmhuptimetext</td><td down""></td>
<td class="' in expression or statement.
At C:\ps_script\bldg\Test.ps1:166 char:12
+ <td class="$percentclass">$vmhmempercent</td>
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '$percentclass">$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"' in expression or statement.
At C:\ps_script\bldg\Test.ps1:166 char:25
+ <td class="$percentclass">$vmhmempercent</td>
+                         ~~~~~~~~~~~~~~~~~~~~~
Unexpected token '">$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan=""5"" lnbrk""></td>
</tr>`n"' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

PS C:\ps_script\bldg>

You could use the SWITCH statement. More details here on this statement: about Switch - PowerShell | Microsoft Learn

1 Spice up

thanks. i’ll review that and give it a try.

Also note, it looks like you have some double quotes off in a couple of places…

$connectionclass""

$connectionclass""

right?

It might be easier to use “here strings” for this:

    $vmDetailHTML += @"
<tr>
<td rowspan='2'>$vmhostnametext</td>
<td class='$connectionclass'>$vmhostConnectionStatetext</td>
<td down"">$vmhostNumCputext</td>
<td down"">$vmhcpupercent</td>
<td down""></td>
</tr><tr><td down"">$vmhuptimetext</td><td down""></td>
<td class='$percentclass'>$vmhmempercent</td>
<td down"">$vmhostMemTotaltext</td>
</tr>
<tr><td colspan="5" lnbrk""></td>
</tr>`n
"@

The @" must be the last thing on the line at the beginning and the "@ must be on a line by itself at the very left.

But here strings are very handy as you can use single or double quotes inside them and you don’t have to worry about escaping or double/tripple quoting.

Just wrap the value in single or double quotes as notmal.

2 Spice ups

This works great! Had to clean up the Class on the rest of the rows, but it works.

if ($vmhmempercent -lt 50)
{
$percentclass = 'low'
}
Elseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))
{
$percentclass = 'med'
}
elseif ($vmhmempercent -gt 74) 
{
$percentclass = 'high'
}

if ($vmhostConnectionStatetext -ne "Connected")
{
$connectionclass = 'down'
}
Elseif ($vmhostConnectionStatetext -eq "Connected")
{
$connectionclass = 'up'
}
					   
$vmDetailHTML += @"
<tr>
<td rowspan='2' class='up'>$vmhostnametext</td>
<td class='$connectionclass'>$vmhostConnectionStatetext</td>
<td class='up'>$vmhostNumCputext</td>
<td class='up'>$vmhcpupercent</td>
<td class='up'></td></tr>
<tr>
<td class='up'>$vmhuptimetext</td>
<td class='up'></td>
<td class='$percentclass'>$vmhmempercent</td>
<td class='up'>$vmhostMemTotaltext</td>
</tr>
<tr><td colspan="5" class='lnbrk'></td>
</tr>`n
"@  

Thank you so much. This change and removing some extra lines just cut my script from 574 to 510 lines. And I still get the results I want.

1 Spice up