<img alt="" src="https://secure.inventive52intuitive.com/789747.png" style="display:none;">
QuickPost: Throwing your errors into the Notification Center

QuickPost: Throwing your errors into the Notification Center

Posted by HTG

On a lot of the projects I work on, support staff are keen to run a few “pre-flight checks” as part of the logon process. Ensuring that machines are named, homed and running correctly is often something they like to do to avoid any potential issues during the user’s session – sometimes going so far as to log the user out or even shut the machine down if an errant value is detected.

Of course, communicating these errors to the user is key – you at least want them to know why they’re being forcibly logged off, or perhaps to get them to contact the helpdesk to get the issue(s) resolved. For a long time, I’ve just used VBScript or PowerShell to flip up a pop-up window and have done with it. However, incessant pop-ups at logon can get annoying – especially when you’ve already got a few things popping up anyway (Skype, intranet page, etc.). Is there a way we can just dump them nicely into the Notification Center down in what used to be known as the “system tray”? After all, that’s where everything that “notifies” is supposed to go these days…

Well, fortunately we can, with a few simple lines of PowerShell.

In this example, I was checking for low disk space. So first I had to actually ascertain whether or not the machine was in an “errant” state. Quick and dirty – I just did this by querying the disk space and then setting an environment variable to “Critical” if it was under a certain level. This could probably be done much more efficiently, but hey, it works, and I’m on a bit of a short timeframe today, so I’m not going to develop it any further just yet 🙂

# Set free space threshold (1GB in this example)

$threshold = 1

# Get free space of C: drive

$freespacebytes = [System.IO.DriveInfo]::GetDrives() | Where-Object {$_.Name -eq ‘C:\’} | Select-Object -expandproperty AvailableFreeSpace

# Convert free space figure to GB

$freespaceGB = $freespacebytes /= 1070000000

# Set environment variable to CRITICAL or NON_CRITICAL

if($freespaceGB -lt $threshold)
{
[Environment]::SetEnvironmentVariable(“CDriveSpace”, “Critical”, “User”)
}
else
{
[Environment]::SetEnvironmentVariable(“CDriveSpace”, “Non-Critical”, “User”)
}

Now, once that variable is set, we just need to take some action to spit out an error that appears in the Notification Center. There are a couple of ways to do this. The first actually requires you to provide a 16×16 icon somewhere in order for the command to work – the second has no dependency, so it’s up to you which one you use.

Also, timing is an issue here – the shell must be fully initialized and visible to the user in order for the PowerShell to work correctly. If you’re an AppSense Environment Manager user, then the “Post-logon trigger” is ideal for this situation. If you’re not, and you’re using something like a logon script, it may or may not work as intended. In this situation I think a Scheduled Task would probably be a better bet, or configure a Logon Script Delay via Group Policy (2012 R2 and up).

Method 1 (lines may wrap)

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon

$objNotifyIcon.Icon = “C:\windows\system32\icon.ico”
$objNotifyIcon.BalloonTipIcon = “Error”
$objNotifyIcon.BalloonTipText = “C: drive disk space is less than defined threshold of 1GB. Please free up disk space or contact support.”
$objNotifyIcon.BalloonTipTitle = “Machine check failure”
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)

Some notes…the first line beginning with [void] is necessary to avoid an interactive progress message as it loads the required assemblies.

Icon refers to the actual icon you see visible in the Notification Area in the bottom right of the screen.

BalloonTipIcon is the icon you will see when the actual error pops up in the right-hand corner (this can be set to Error, Info or Warning, with the appropriate icon for each)

BalloonTipTitle is pretty self-explanatory….

…as is BalloonTipText.

Finally, the number (10000 in this example) is the amount of time in milliseconds that the error will display. The system does have maximum and minimum values for this, however, so you may find your values overridden if you configure them extremely high or low.

Method 2 (lines may wrap)

function Show-BalloonTip
{

[CmdletBinding(SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory=$true)]
$Text,

[Parameter(Mandatory=$true)]
$Title,

[ValidateSet(‘None’, ‘Info’, ‘Warning’, ‘Error’)]
$Icon = ‘Error’,

$Timeout = 10000
)

Add-Type -AssemblyName System.Windows.Forms

if ($script:balloon -eq $null)
{
$script:balloon = New-Object System.Windows.Forms.NotifyIcon
}

$path                    = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloon.Icon            = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon  = $Icon
$balloon.BalloonTipText  = $Text
$balloon.BalloonTipTitle = $Title
$balloon.Visible         = $true

$balloon.ShowBalloonTip($Timeout)
}

Show-BalloonTip -Text ‘There is not enough free space on the C: drive. Please either free up space or contact support.’ -Title ‘Machine check fail!’ -Icon Error -Timeout 1000

In this example a function is used and then called with the Show-BalloonTip cmdlet. Not there is no particular icon used this time – the Icon parameter only refers to Error, Warning or Info as mentioned earlier.

Both of these examples work equally well – example output from each is shown below. The only difference is in the icon displayed in the notification area (highlighted in red) – Method 1 allows you to customize this whereas Method 2 puts a “blank” Windows icon there.

Method 1 output
Method 2 output

So there you have it – a straightforward way to dump your pop-ups and customized errors/warnings/information into the Notification Center. Hope this is useful!

Contact

Want to partner with us?

Get in touch to learn more about our services or arrange a free 30-minute consultation with one of our Secure Cloud Experts.

Get in touch
HTG - Contact CTA