A More Advanced Configuration - HiltonGiesenow/PoShMon GitHub Wiki

Building on the example in the Getting Started guide, you might be starting with a simple one-line PoShMon command like the following:

PS> Invoke-OSMonitoring -PoShMonConfiguration (New-PoShMonConfiguration { OperatingSystem -CPULoadThresholdPercent 5 }) -Verbose

At the moment this is fine, because there's not much configuration we're providing. However, as we start to add settings, like a list of servers to monitor, what services and event log settings we want to include, various notification settings (e.g. email, PushBullet and so on) then having this all on one line is going to get messy. Let's look at splitting it out into something more manageable.

Table of Contents

Splitting Out the PoShMon Configuration

First, let's split out the creating of the Configuration object, as follows:

$poShMonConfiguration = New-PoShMonConfiguration { OperatingSystem -CPULoadThresholdPercent 5 }

Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration -Verbose

Now the script looks a little cleaner, but we can also split out the New-PoShMonConfiguration command on to multiple lines, because it will likely grow as we add settings. Let's do the following:

$poShMonConfiguration = New-PoShMonConfiguration {
            OperatingSystem -CPULoadThresholdPercent 5
        }

Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration -Verbose

This looks a bit neater, but let's change the settings to something more realistic. We'll take out the CPU threshold setting, and add in a list of the Event Log levels to scan for as well as a Windows Service we'd like to make sure is in a Running state on the servers.

$poShMonConfiguration = New-PoShMonConfiguration {
            OperatingSystem -EventLogCodes 'Error','Warning' -WindowsServices 'BITS'
        }

Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration -Verbose

Creating a More Readable Multi-Line Configuration

The above sample is hopefully not too hard to follow, but it can get a little messier once we have a bunch of configuration sections included, each with their own settings. Take the following detailed configuration for a SharePoint farm as an example:

$poShMonConfiguration = New-PoShMonConfiguration {
                General -EnvironmentName 'SharePoint' -MinutesToScanHistory 15 -PrimaryServerName 'SPAPPSVR01' -ConfigurationName SpFarmPosh -TestsToSkip "SPDatabaseHealth","SPUPSSyncHealth"
                OperatingSystem -EventLogCodes 'Error','Warning' -WindowsServices 'BITS'
                WebSite -WebsiteDetails @{ 
                                        "http://intranet" = "Read our terms"
                                        "http://extranet.company.com" = "Read our terms"
                                     }
                Notifications -When OnlyOnFailure {
                    Email -ToAddress "[email protected]" -FromAddress "[email protected]" -SmtpServer "EXCHANGE.COMPANY.COM"
                }
                
            }

Now, this is definitely better than a one-liner, but you might still find it a little too compressed. Luckily, we can use PowerShell's multi-line character, the '`' character, to split some of these settings out even further, as follows:

$poShMonConfiguration = New-PoShMonConfiguration {
            General `
                -EnvironmentName 'SharePoint' `
                -MinutesToScanHistory 15 `
                -PrimaryServerName 'SPAPPSVR01' `
                -ConfigurationName SpFarmPosh `
                -TestsToSkip "SPDatabaseHealth","SPUPSSyncHealth"
            OperatingSystem `
                -EventLogCodes 'Error','Warning' `
                -WindowsServices 'BITS'
            Notifications {
                Email `
                    -ToAddress "[email protected]" `
                    -FromAddress "[email protected]" `
                    -SmtpServer "smtp.company.com"
            }
        }

Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration -Verbose

Now, this is (imho) a lot more readable. It's a little 'high' in that there's a longer scroll bar, but the ISE or Visual Studio Code, for instance, will let me collapse that down (using the '-' sign in the margin) to just the following:

$poShMonConfiguration = New-PoShMonConfiguration {...}

Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration -Verbose

HOWEVER, there's one really important thing you need to be careful of now - the line continuation ('`' character). If you miss one, it will misconfigure the monitoring! Depending on what you misconfigure, it might tell you easily, but it also might not so use with caution.

Using an External Configuration File

Of course, the smallest configuration code you can have is to externalise the configuration altogether, for instance into a separate file. The easiest way to do this is to create your configuration as before, and then simply save it out to a separate file. The following command will do this:

$poShMonConfiguration | Export-Clixml C:\Temp\PoShMonSampleConfig.xml

(where C:\Temp\PoShMonSampleConfig.xml is of course the file your choosing to store your configuration in)

You only need to do this once, and thereafter you can simply read this configuration whenever you want to use it to call PoShMon. The first step is to read the contents of the file into a variable, and then go ahead and use it, as follows:

$$poShMonConfiguration2 = Import-Clixml C:\Temp\PoShMonConfig.xml
Invoke-OSMonitoring -PoShMonConfiguration $poShMonConfiguration2 -Verbose