Wednesday, July 13, 2022

Using Powershell to send an email alert when a file type is found

A while back I had the privilege of working with a brilliant dev; Andrew Nesbit in helping sort out some processes for the Horizon ILS and how it would work with the Metro Library Card System (  I helped him with some of the Horizon work with how the different library systems work together more specifically SirsiDynix Symphony, polaris and Horizon.  

One issue that came out that I never got around to fixing till now was the creation of the .FAIL file that is generated if the server has a problem or there is some sort of incompatibility between library systems, such as pin, city name, password length, etc.  There is also an issue with the service stopping without notice; which also creates a .FAIL file as shown below.

Metro FAIL file.  Dead Process

Metro FAIL file.  FAILED User

So I decided to finally write a powershell script to send me an alert when this happens.  So it looks in the directory where the Me Server saves it's logs; and I have powershell checking that directory as shown below.  The script uses powershell to check for a .FAIL file in the log directory and then sends a send-mail message.  I know that this is depreciated and I will have to update this sooner then later; but it will work for the purpose I need for now.

If ( (get-childitem $PATH TO DIRECTORY | select extension) -match ".fail"){
Send-MailMessage -From '$FROMEMAIL' -To '$MYEMAIL' -Subject '$Subject_I_WANT' -smtpserver '$SMTPSERVER'

For security I have setup a secure powershell execution policy on the machine and am using a bat file that will execute that powershell script with the proper permissions it requires.  I setup a bat file to do this.


Powershell.exe -executionpolicy remotesigned -File $PATHTOFILE

I then setup task manager to execute the script daily.

My next step is to have the system check for the two different fail logs with any attachments and send the email to different users as required.

Hyper-V VM Backup using Powershell (VM-Export)

One of the many things I've been working on is an automated script to do VM backups.  I've been doing a lot of work automating proce...