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 <\/p>\n
For Each Section of Original Script:<\/p>\n
$vmServers = $vmServers | Sort\n\nForEach ($vmserver in $vmservers)\n\n{\tWrite-Host \"Testing $vmServer...\"\n$vmhost = get-vmhost -name $vmserver\".eldoradocourt.org\"\n#$vmg = $vmserver | get-vmguest\n\n$vmhostnametext=$vmserver\n$vmhostConnectionStatetext=$vmhost.ConnectionState\n$vmhostNumCputext=$vmhost.NumCpu\n$vmhostCpuUsagetext=$vmhost.CpuUsageMhz\n$vmhostCpuTotaltext=$vmhost.CpuTotalMhz\n$vmhostMemUsuagetext=$vmhost.MemoryUsageGB \n $vmhostMemUsuagetext = \"{0:N3}\" -f $vmhostMemUsuagetext \n$vmhostMemTotaltext=$vmhost.MemoryTotalMB\n $vmhostMemTotaltext = \"{0:N3}\" -f $vmhostMemTotaltext \n$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString(\"#.##\")}\n$vmhtotcpughz = (([int]$vmhost.cputotalmhz))\n$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))\n$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString(\"#.##\")}\n$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))\n$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))\n$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString(\"#.##\")}\n\nif ($vmhostConnectionStatetext -ne \"Connected\" )\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td down\"\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td down\"\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\n\nElseif ($vmhmempercent -lt 50)\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td low\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\t \n\nElseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td med\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\t \n\nelseif ($vmhmempercent -gt 74) \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td high\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n}\n}\n<\/code><\/pre>\n
Advertisement
Attempt to streamline:<\/p>\n
$vmServers = $vmServers | Sort\n\nForEach ($vmserver in $vmservers)\n\n{\tWrite-Host \"Testing $vmServer...\"\n$vmhost = get-vmhost -name $vmserver\".eldoradocourt.org\"\n#$vmg = $vmserver | get-vmguest\n\n$vmhostnametext=$vmserver\n$vmhostConnectionStatetext=$vmhost.ConnectionState\n$vmhostNumCputext=$vmhost.NumCpu\n$vmhostCpuUsagetext=$vmhost.CpuUsageMhz\n$vmhostCpuTotaltext=$vmhost.CpuTotalMhz\n$vmhostMemUsuagetext=$vmhost.MemoryUsageGB \n $vmhostMemUsuagetext = \"{0:N3}\" -f $vmhostMemUsuagetext \n$vmhostMemTotaltext=$vmhost.MemoryTotalMB\n $vmhostMemTotaltext = \"{0:N3}\" -f $vmhostMemTotaltext \n$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString(\"#.##\")}\n$vmhtotcpughz = (([int]$vmhost.cputotalmhz))\n$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))\n$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString(\"#.##\")}\n$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))\n$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))\n$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString(\"#.##\")}\n\nif ($vmhmempercent -lt 50)\n{\n$percentclass = \"low\"\n}\nElseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))\n{\n$percentclass = \"med\"\n}\nelseif ($vmhmempercent -gt 74) \n{\n$percentclass = \"high\"\n}\nif ($vmhostConnectionStatetext -ne \"Connected\")\n{\n$connectionclass = \"down\"\n}\nElseif ($vmhostConnectionStatetext -eq \"Connected\")\n{\n$connectionclass = \"up\"\n}\n\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td class=\"$connectionclass\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td class=\"$percentclass\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\n\n}\n<\/code><\/pre>\nI can’t get the class to change with my new script. Any thoughts?<\/p>","upvoteCount":2,"answerCount":7,"datePublished":"2018-02-28T17:53:03.000Z","author":{"@type":"Person","name":"anthony7445","url":"https://community.spiceworks.com/u/anthony7445"},"acceptedAnswer":{"@type":"Answer","text":"
It might be easier to use “here strings” for this:<\/p>\n
$vmDetailHTML += @\"\n<tr>\n<td rowspan='2'>$vmhostnametext</td>\n<td class='$connectionclass'>$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td class='$percentclass'>$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"5\" lnbrk\"\"></td>\n</tr>`n\n\"@\n\n<\/code><\/pre>\nThe @\" must be the last thing on the line at the beginning and the \"@ must be on a line by itself at the very left.<\/p>\n
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.<\/p>\n
Just wrap the value in single or double quotes as notmal.<\/p>","upvoteCount":2,"datePublished":"2018-02-28T19:09:13.000Z","url":"https://community.spiceworks.com/t/trying-to-streamline-a-script/637573/6","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},"suggestedAnswer":[{"@type":"Answer","text":"
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 <\/p>\n
For Each Section of Original Script:<\/p>\n
$vmServers = $vmServers | Sort\n\nForEach ($vmserver in $vmservers)\n\n{\tWrite-Host \"Testing $vmServer...\"\n$vmhost = get-vmhost -name $vmserver\".eldoradocourt.org\"\n#$vmg = $vmserver | get-vmguest\n\n$vmhostnametext=$vmserver\n$vmhostConnectionStatetext=$vmhost.ConnectionState\n$vmhostNumCputext=$vmhost.NumCpu\n$vmhostCpuUsagetext=$vmhost.CpuUsageMhz\n$vmhostCpuTotaltext=$vmhost.CpuTotalMhz\n$vmhostMemUsuagetext=$vmhost.MemoryUsageGB \n $vmhostMemUsuagetext = \"{0:N3}\" -f $vmhostMemUsuagetext \n$vmhostMemTotaltext=$vmhost.MemoryTotalMB\n $vmhostMemTotaltext = \"{0:N3}\" -f $vmhostMemTotaltext \n$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString(\"#.##\")}\n$vmhtotcpughz = (([int]$vmhost.cputotalmhz))\n$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))\n$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString(\"#.##\")}\n$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))\n$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))\n$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString(\"#.##\")}\n\nif ($vmhostConnectionStatetext -ne \"Connected\" )\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td down\"\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td down\"\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\n\nElseif ($vmhmempercent -lt 50)\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td low\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\t \n\nElseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td med\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\t \n\nelseif ($vmhmempercent -gt 74) \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td up\"\">$vmhostConnectionStatetext</td>\n<td up\"\">$vmhostNumCputext</td>\n<td up\"\">$vmhcpupercent</td>\n<td up\"\"></td>\n</tr><tr><td up\"\">$vmhuptimetext</td><td up\"\"></td>\n<td high\"\">$vmhmempercent</td>\n<td up\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n}\n}\n<\/code><\/pre>\nAttempt to streamline:<\/p>\n
$vmServers = $vmServers | Sort\n\nForEach ($vmserver in $vmservers)\n\n{\tWrite-Host \"Testing $vmServer...\"\n$vmhost = get-vmhost -name $vmserver\".eldoradocourt.org\"\n#$vmg = $vmserver | get-vmguest\n\n$vmhostnametext=$vmserver\n$vmhostConnectionStatetext=$vmhost.ConnectionState\n$vmhostNumCputext=$vmhost.NumCpu\n$vmhostCpuUsagetext=$vmhost.CpuUsageMhz\n$vmhostCpuTotaltext=$vmhost.CpuTotalMhz\n$vmhostMemUsuagetext=$vmhost.MemoryUsageGB \n $vmhostMemUsuagetext = \"{0:N3}\" -f $vmhostMemUsuagetext \n$vmhostMemTotaltext=$vmhost.MemoryTotalMB\n $vmhostMemTotaltext = \"{0:N3}\" -f $vmhostMemTotaltext \n$vmhuptimetext = (([int]$vmhost.ExtensionData.Summary.QuickStats.Uptime) /60/60/24)| % {$_.ToString(\"#.##\")}\n$vmhtotcpughz = (([int]$vmhost.cputotalmhz))\n$vmhusedcpughz = (([int]$vmhost.CpuUsageMhz))\n$vmhcpupercent = (($vmhusedcpughz/$vmhtotcpughz)*100) | % {$_.ToString(\"#.##\")}\n$vmhtotmemgb = (([int]$vmhost.MemoryTotalGB))\n$vmhusedmemgb = (([int]$vmhost.MemoryUsageGB))\n$vmhmempercent = (($vmhusedmemgb/$vmhtotmemgb)*100) | % {$_.ToString(\"#.##\")}\n\nif ($vmhmempercent -lt 50)\n{\n$percentclass = \"low\"\n}\nElseif (($vmhmempercent -gt 49 )-and ($vmhmempercent -lt 75))\n{\n$percentclass = \"med\"\n}\nelseif ($vmhmempercent -gt 74) \n{\n$percentclass = \"high\"\n}\nif ($vmhostConnectionStatetext -ne \"Connected\")\n{\n$connectionclass = \"down\"\n}\nElseif ($vmhostConnectionStatetext -eq \"Connected\")\n{\n$connectionclass = \"up\"\n}\n\n\t\t\t\t\t \n{$vmDetailHTML += \"\n <tr>\n<td rowspan=\"\"2\"\">$vmhostnametext</td>\n<td class=\"$connectionclass\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td class=\"$percentclass\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"\n\n }\t\n\n}\n<\/code><\/pre>\nI can’t get the class to change with my new script. Any thoughts?<\/p>","upvoteCount":2,"datePublished":"2018-02-28T17:53:03.000Z","url":"https://community.spiceworks.com/t/trying-to-streamline-a-script/637573/1","author":{"@type":"Person","name":"anthony7445","url":"https://community.spiceworks.com/u/anthony7445"}},{"@type":"Answer","text":"
This is the error i’m getting.<\/p>\n
PS C:\\ps_script\\bldg> .\\test.ps1\nAt C:\\ps_script\\bldg\\Test.ps1:161 char:12\n+ <td class=\"$connectionclass\">$vmhostConnectionStatetext</td>\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nUnexpected token '$connectionclass\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td class=\"$percentclass\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"' in expression or statement.\nAt C:\\ps_script\\bldg\\Test.ps1:161 char:28\n+ <td class=\"$connectionclass\">$vmhostConnectionStatetext</td>\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nUnexpected token '\">$vmhostConnectionStatetext</td>\n<td down\"\">$vmhostNumCputext</td>\n<td down\"\">$vmhcpupercent</td>\n<td down\"\"></td>\n</tr><tr><td down\"\">$vmhuptimetext</td><td down\"\"></td>\n<td class=\"' in expression or statement.\nAt C:\\ps_script\\bldg\\Test.ps1:166 char:12\n+ <td class=\"$percentclass\">$vmhmempercent</td>\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nUnexpected token '$percentclass\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"' in expression or statement.\nAt C:\\ps_script\\bldg\\Test.ps1:166 char:25\n+ <td class=\"$percentclass\">$vmhmempercent</td>\n+ ~~~~~~~~~~~~~~~~~~~~~\nUnexpected token '\">$vmhmempercent</td>\n<td down\"\">$vmhostMemTotaltext</td>\n</tr>\n<tr><td colspan=\"\"5\"\" lnbrk\"\"></td>\n</tr>`n\"' in expression or statement.\n + CategoryInfo : ParserError: (:) [], ParseException\n + FullyQualifiedErrorId : UnexpectedToken\n\nPS C:\\ps_script\\bldg>\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-02-28T17:54:17.000Z","url":"https://community.spiceworks.com/t/trying-to-streamline-a-script/637573/2","author":{"@type":"Person","name":"anthony7445","url":"https://community.spiceworks.com/u/anthony7445"}},{"@type":"Answer","text":"You could use the SWITCH statement. More details here on this statement: about Switch - PowerShell | Microsoft Learn<\/a><\/p>","upvoteCount":1,"datePublished":"2018-02-28T17:59:04.000Z","url":"https://community.spiceworks.com/t/trying-to-streamline-a-script/637573/3","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"