What do we get more than anything, notifications. I'm a fan of turning them off (when I can), but if you wanted to show notifications through Powershell, here's a way how to do that.
We can now set all of the properties we need for our notification.
This is what our notification looks like.
A notification in Windows 10 |
Adding Windows Forms
We are going to leverage the NotifyIcon class built into Windows Forms in order to display our notification in Powershell. This is possible through the Add-Type cmdlet available in Powershell.
With the Add-Type cmdlet, we can add the Windows.Forms assembly directly into our Powershell instance and use types like we would in code. This can be done with the following code.
Add-Type -AssemblyName System.Windows.FormsOnce this assembly is loaded into memory, we can begin to set the properties of the NotifyIcon and display it from Powershell.
Setting the properties
Before we set the properties on our notification, we need to create a NotifyIcon first. This can be done with the New-Object cmdlet.
$notification = New-Object System.Windows.Forms.NotifyIconWe are storing a reference of the object in a variable so that we can set properties on our NotifyIcon.
We can now set all of the properties we need for our notification.
$path = (Get-Process -id $pid).Path $notification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) $notification.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Error $notification.BalloonTipText = "A Powershell error, oh no!!" $notification.BalloonTipTitle = "ERROR" $notification.Visible = $true $notification.ShowBalloonTip(3000)The Icon grabs the image for the Powershell application. The BalloonTipIcon is the icon associated with the notification (using any valid value from the ToolTipIcon enumeration). The text and title are pretty self-explanatory. We also ensure the NotifyIcon is visible and is visible for 3 seconds (3000 miliseconds).
This is what our notification looks like.
Our Powershell error |
Small notes
I wanted to call out two pieces of the above script which justify a little more explanation. First is the code that is returning the icon of the notification. You will notice above that we are calling Get-Process to pull the path of the current process. The $pid is an automatic variable that automatically is the value of the current process of the executing Powershell session.
When we use the path of the current executing Powershell session (which for me happens to be "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"), we are using it to call a static method on the System.Drawing.Icon class. In Powershell, when we encounter square brackets around a variable, Powershell knows that this variable is a .NET class and interprets as such. The two colons after the brackets indicate we are calling a static method, ExtractAssociatedIcon.
Full script
The full script is below, with additional comments. Enjoy!
# Add the Windows.Forms assembly Add-Type -AssemblyName System.Windows.Forms # Create an object to hold a reference to a NotifyIcon $notification = New-Object System.Windows.Forms.NotifyIcon # Set the icon of the notification $path = (Get-Process -id $pid).Path $notification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) # Set the balloon icon $notification.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Error # Set the text & title $notification.BalloonTipText = "A Powershell error, oh no!!" $notification.BalloonTipTitle = "ERROR" # Ensure this notification is visible $notification.Visible = $true # Show the notification for 3 (3000 miliseconds) seconds $notification.ShowBalloonTip(3000)
ReplyDeletei couldn't believe that i would ever be re-unite with my ex-lover, i was so traumatize staying all alone with no body to stay by me and to be with me, but i was so lucky one certain day to meet this powerful spell caster Dr Akhere,after telling him about my situation he did everything humanly possible to see that my lover come back to me,indeed after casting the spell my ex-lover came back to me less than 48 hours,my ex-lover came back begging me that he will never leave me again,3 months later we got engaged and married,if you are having this same situation just contact Dr Akhere on his email: AKHERETEMPLE@gmail.com thanks very much sir for restoring my ex-lover back to me,his email: AKHERETEMPLE@gmail.com or call/whatsapp:+2349057261346
hindi ako makapaniwala na kailanman ay muling makiisa ako sa aking kasintahan, labis akong na-trauma sa pananatiling nag-iisa na walang katawan na manatili sa akin at makakasama ko, ngunit napakasuwerte ako sa isang tiyak na araw upang matugunan ito malakas na spell caster na si Dr Akhere, matapos sabihin sa kanya ang tungkol sa aking sitwasyon ginawa niya ang lahat ng makataong posible upang makita na ang aking kasintahan ay bumalik sa akin, sa katunayan matapos na ihagis ang spell ang aking dating kasintahan ay bumalik sa akin ng mas mababa sa 48 oras, dumating ang dating kasintahan ko. bumalik sa pagmamakaawa sa akin na hindi na niya ako pababayaan, 3 buwan mamaya kami ay nakipag-ugnay at nag-asawa, kung nagkakaroon ka ng parehong sitwasyong ito makipag-ugnay lamang kay Dr Akhere sa kanyang email: AKHERETEMPLE@gmail.com maraming salamat sa sir sa pagpapanumbalik ng aking dating kasintahan bumalik sa akin, ang kanyang email: AKHERETEMPLE@gmail.com o tumawag / whatsapp: +2349057261346
AM SANDRA FROM CANADA, THANKS TO DR ONIHA WHO HELP ME BRING MY HUSBAND BACK, MY HUSBAND LEFT ME WITH THREE KIDS, FOR ANOTHER YOUNG GIRL, FOR OVER TWO YEARS, I TRIED ALL I COULD TO SETTLED OUR DIFFRENCES, BUT IT YIELDED NO RESULT, I WAS THE ONE TAKING CARE OF THE CHILDREN ALONE, UNTIL ONE DAY, I CAME IN CONTACT WITH SOME ARTICLES ONLINE, CONTAINING HOW DR ONIHA HAS HELP SO MANY LOVERS AND FAMILY REUNION AND REUNIT AGAIN, AND I DECIDED TO CONTACT HIM, AND HE CAST HIS SPELL ON MY HUSBAND, WITHIN FIVE DAYS, MY HUSBAND RAN BACK HOME, AND WAS BEGGING ME AND THE KIDS FOR FORGIVENESS, IN CASE YOU ARE PASSING THROUGH SIMILAR PROBLEMS, AND YOU WANTS TO CONTACT DR ONIHA, YOU CAN REACH HIM VIA HIS CONTACT NUMBER, ON CALL OR WHATSAP +2347089275769 OR EMAIL DRONIHASPELL@YAHOO.COM
ReplyDeleteI was diagnosed as HEPATITIS B carrier in 2013 with fibrosis of the
ReplyDeleteliver already present. I started on antiviral medications which
reduced the viral load initially. After a couple of years the virus
became resistant. I started on HEPATITIS B Herbal treatment from
ULTIMATE LIFE CLINIC (www.ultimatelifeclinic.com) in March, 2020. Their
treatment totally reversed the virus. I did another blood test after
the 6 months long treatment and tested negative to the virus. Amazing
treatment! This treatment is a breakthrough for all HBV carriers.