Help with a legacy script

Hello all,

Some years ago the company hired a contractor to write a PS script to check for servers that were awaiting reboot via WSUS GPOs and give them a kick, after which it then logged this and sent an email.

Here I am today trying to understand how it works because we have a new GPO which we want to cover but since adding it the script runs but we don’t get the export or email, i’m not sure how best to proceed as the script does contain info like mailservers and the like.

Any help appreciated.

Foz

F022Y,
Welcome to the forum. :wave:t4:

This is a peer to peer forum where we help each other with self written code. We do not review or refactor complete scripts or solutions, sorry.

I’d recommend to start to learn the fundamentals of PowerShell first. This will save you from a lot of wasted time and frustrations.

In general: when you have sensitive information embedded in the code you need to post you should obfuscate those information.

Hello Olaf,

Sorry should have said that i’m familiar with PS but clearly the guy who wrote this has done some things i’m not familiar with as i have amended it previously with no issue but not this section.

I only ask about sensitive info as previously i’ve known sites ask for the whole script and this beast is 242 lines.

The section i’m interested is as follows:-

Prepare the email to send to Infrastructure team with links to logs

$SMTPSvr = $SMTPServer
$From = $SendFrom
$To = $SendTo
$Subject = ("WSUS 2016 Auto Update Reboot Status - " + $todaysDate)

$Body = ("<html><head></head><body bgcolor=White><p>"+$FontSmall+"Can't read this? <a href='http://webadcu/WSUS/$HTMLFileName'>View in browser</a></font></p><p align=Left><img src='Corporate image' width='79' height='37'></p><p>"+$FontNormal+"Please find below the report for WSUS Auto-Rebooting the following servers:</font></p><p>"+$FontNormal+"Total Computers in GPO's:<br>" + $GPO + ": <font color=#000c52><b>" + $TotalGPO + "</b></font><br>" + $GPO2 + ": <font color=#000c52><b>" + $TotalGPO2 + "</b></font><br>" + $GPO3 + ": <font color=#000c52><b>" + $TotalGPO3 + "</b></font><br>" + $GPO4 + ": <font color=#000c52><b>" + $TotalGPO4 + "</b></font><br>" + $GPO5 + ": <font color=#000c52><b>" + $TotalGPO5 + "</b></font><br>" + $GPO6 + ": <font color=#000c52><b>" + $TotalGPO6 + "</b></font><br><br>" "Total Computers in all Auto Update GPO's: <font color=#000c52><b>" + $TotalComputers + "</b></font><br>Total Computers Rebooted: <b>" + $TotalReboots + "</b><br>Total Computers Not Rebooted: <b>" + $TotalNoReboots + "</b><br>Total Computers WMI Errors: <b>" + $TotalWMIErrors + "</b><br>Total Computers Non-contactable: <b>" + $TotalNonContact + "</b></font></p>")
    $Body += "<table width='100%' cellspacing=0 cellpadding=0 border=0 height=20><tr><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='100%' height='1'><img src='Corporate image' width='100%' height='1' border=0></td></tr>"
    $Body += "<tr><td width='100%' height='20' colspan='4'>"+$FontHeading+"<b>Computers Rebooted</b></font></td></tr><tr><td width='200' height='20'>"+$FontNormal+"<b>Computer Name</b></font></td><td width='200' height='20'>"+$FontNormal+"<b>Status</b></font></td><td width='200' height='20'>"+$FontNormal+"<b>Time Rebooted</b></font></td><td width='100%' height='20'><img src='Corporate image' width='100%' height='20' border=0></td></tr>"
    $Body += "<tr bgcolor='#CCCCCC'><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='100%' height='1'><img src='Corporate image' width='100%' height='1' border=0></td></tr>"
    $Body += $RebootArray
    $Body += "<tr><td width='200' height='20'><img src='Corporate image' width='200' height='20' border=0></td><td width='200' height='20'><img src='Corporate image' width='200' height='20' border=0></td><td width='100%' height='20' colspan='2'><img src='Corporate image' width='100%' height='20' border=0></td></tr>"
    $Body += "<tr><td width='100%' height='20' colspan='4'>"+$FontHeading+"<b>Computers Not Rebooted</b></font></td></tr><tr><td width='200' height='20'>"+$FontNormal+"<b>Computer Name</b></td><td width='200' height='20'>"+$FontNormal+"<b>Status</b></td><td width='100%' height='20' colspan='2'><img src='Corporate image' width='100%' height='20' border=0></td></tr>"
    $Body += "<tr bgcolor='#CCCCCC'><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='200' height='1'><img src='Corporate image' width='200' height='1' border=0></td><td width='100%' height='1' colspan='2'><img src='Corporate image' width='100%' height='1' border=0></td></tr>"
    $Body += $NoRebootArray
    $Body += "</table><p><font style='font-family: Calibri,Tahoma,Verdana; font-size: 12; color: #333333'>Please check the full log at <a href='file://Script Server/WSUS/$FileName'>\\Script Server\WSUS\$FileName</a>.<br><br>This is an automated email. Do not reply to this email.</font></p></body></html>"

#### Now send the email using Send-MailMessage  
Send-MailMessage –From $From –To $To –Subject $Subject –Body $Body -BodyAsHtml -SmtpServer $SMTPSvr -Port 25

# Create the html file to view in browser
$Body | Out-File ($ExportFilePath + $HTMLFileName) -Encoding ascii

The only bit i’ve added is the $GPO6 and $TotalGPO6 across the script

Hmmm … I assume you do not use VSCode, do you? :wink:


Either you remove the consecutive double quotes or you add a plus between them.

Regardless of that - I urgently recommend for you to read about here strings. It could make your code way easier to read.

Next you may read about creating HTML reports with PowerShell.

That could make the HTML part way easier to write and to maintain if needed.