SBS 2011 Standard: Add a PSConfig Email Alert

sbsstdA client called me today to say ‘we cant get on the internet’, I asked what happened when they loaded up the browser, and they got a 503 Service Unavailable.

Ok i said, type in another address, which they couldn’t do of course because the familiar Companyweb page had not loaded and their link to Google was not available. People are still fuzzy it seems on what the address bar does.

companyw

Anyway, some updates had been applied to this server over the weekend, including some SharePoint updates, which had not been installed correctly, that is to say, PSConfig had not been run.

I could see straight away what the issue was, so set about solving it rather than asking the Tech if he had run PSConfig – colour me impatient on a Monday.

It got me thinking though, although Update Rollup 1, will show an alert on the Desktop when you login, to say PSConfig is required to complete your SharePoint update, what if you don’t log back in after a batch of updates.

Or worse, what if you are not running UR1 yet?

We can apply a little fix to both of these possibilities, that should alert the remote admin, that he has not finished his Updating duties just yet.

Firstly, let’s write a quick PowerShell script to query the server, and if needed, Alert us to the need to run PSConfig.

Ill say at this point i wrote a full script to do the whole PSConfig process, however due to the nature of what PSConfig does i think it is better to run this manually as required. The below script will just alert you as to wether a PSConfig is, or is not required.

If we open up PowerShellISE then copy and paste the below…

$a = Get-Date
#Email Settings
$strfrom = “administrator@server.co.uk”
$strto = “rob@myaddressco.uk”
$strsmtp = “smtp.server.com”
$strsubj = “PSConfig Required”
$strbody = “PSConfig is Required on SBSSERVER $a”
$strsubjn = “PSConfig Scheduled Task”
$strbodyn = “PSConfig Not Required on SBSSERVER $a”

#Import Sharepoint Powershell Module
add-pssnapin Microsoft.SharePoint.PowerShell

#Is PSConfig Command Required

if ((get-spserver $env:computername).NeedsUpgrade -eq$True)

{
#Send Email Notification PSConfig Required
Send-MailMessage -to $strto -from $strfrom -subject $strsubj -body $strbody -smtpserver $strsmtp
}

#if PSConfig not required
else
{
Send-MailMessage -to $strto -from $strfrom -subject $strsubjn -body $strbodyn -smtpserver $strsmtp
}

You will need to save the file with a .ps1 extension and then edit those Variables to suit your environment, but what does all this code mean?

First of all we are setting some variables for our email message, this will make it a lot easier to add your own settings at each site you want to set this up.

$a = Get-Date

This allows us to get the date and then enter it into our email message.

$strto = “youremail@address.com’

This sets a string as a variable, in this case the string (str) ‘to’ is the address you want to send the alert to.

Im hoping this will be quite easy to follow. We set the email settings here as variables, then further down in the script we can substitute the actual settings for the variables, so you don’t have to follow the script through and change each occurrence of a setting, you just change the variable at the top.

Send-MailMessage -to $strto -from $strfrom -subject $strsubj -body $strbody -smtpserver $strsmtp

Variables shown above in blue.

Our first line adds the Sharepoint PowerShell tools to this session.

add-pssnapin Microsoft.SharePoint.PowerShell

Moving on, we start with an IF statement.

if ((get-spserver $env:computername).NeedsUpgrade -eq$True)

If the above command returns a value of True then we do the action inside the { } otherwise do the action under Else.

Again, hopefully that is straightforward.

You may recognise this command from other blog posts or forums, as it is the way to find out if a PSConfig is required.

So, assuming the result is True, and a PSConfig is required, we then go to the action inside { } which is the code we have looked at with our variables to send out an Email alert.

If the result is false, we still send an email alert, just with different variables.

else
{
Send-MailMessage -to $strto -from $strfrom -subject $strsubjn -body $strbodyn -smtpserver $strsmtp
}

PSISE

In my example above, i have commented my lines of code using a #, which is useful if someone else has to look at this, or if you have a memory like mine.

So that was a very brief explanation of how our script will work. Hopefully you can see what is it trying to do!

How do we execute it?

We can create a simple Scheduled task to execute our script on Event, or at times of the day.

I have set mine to run at 30 minutes after a System Startup, and at 1pm every week day.

The syntax for the scheduled task action should be:

powershell.exe -command <path to ps1 file>

task1

task

In order to allow the email to send, we need to edit the configuration of Exchange. You may need to add a new SMTP Receive Connector, and configure that to allow Anonymous Relay. Before you totally freak out about anonymous relay, we wont be leaving this open to abuse, we are just going to allow the server to send out emails, alternatively you can use an external SMTP Server, like an ISP’s smart host or similar.

  • More info on anonymous relay here
  • Test your server for anonymous (open) relay here

From the Exchange Shell we can add a new Receive Connector with the relevant settings, it is important you add a new connector, rather than edit the SBS Default Internet Receive Connector.

You may already have an additional Receive Connector for a Scanner/Copier you can use that if you wish.

New-ReceiveConnector -Name “Email Alert Connector” -Usage Custom -PermissionGroups AnonymousUsers –Bindings serverIP:25 -RemoteIpRanges serverIP

(where serverIP is your servers IP address)

shell1

Your connector should be set to only allow connections from certain IP Addresses, in this case the Servers own internal IP (and any additional devices that need to use it) and it should be set with the Anonymous permission.

We then need to run this command in the Shell to add anonymous relay permission.

Get-ReceiveConnector “Email Alert Connector” | Add-ADPermission -User “NT AUTHORITY\ANONYMOUS LOGON” -ExtendedRights “Ms-Exch-SMTP-Accept-Any-Recipient”

shell2

Now if we execute our scheduled task, we should receive an email telling us if PSConfig is required or not.

alert

Hopefully you should not ever forget to run that PSConfig now!

If you are running UR1 for SBS 2011 Standard, then an event is logged in the Application log when a PSConfig is required. You can right click this event and tell it to add a scheduled task based on that event. You can then configure that event to send out an email alert.

I have not covered that here because my method above will work whether UR1 is running or not.

Special thanks to Shane Howey, PowerShell MVP who helped me learn about PowerShell variables!

If you don’t want to worry about creating a PowerShell script, you can download my one from the TechNet Gallery and then just edit the Email settings.

About Robert Pearman
Robert Pearman is a UK based Small Business Server enthusiast. He has been working within the SMB IT Industry for what feels like forever. Robert likes Piña colada and taking walks in the rain, on occasion he also enjoys writing about Small Business Technology like Windows Server Essentials or more recently writing PowerShell Scripts. If you're in trouble, and you can find him, maybe you can ask him a question.

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: