In this post we’ll make a script for a FTP / SFTP monitor that can monitor the FTP/SFTP status, by doing the following operations:
-Log in
-Upload a file
-Download a file
-Delete the file

Since Powershell doesn’t have any built-in ftp support I was looking for some alternatives, and since I use WinSCP normally for ftp/sftp I found that they also support Powershell scripting, so why not take advantage of this? This guide was written with great help from WinSCP’s own page: https://winscp.net/eng/docs/library_powershell

Get the SSH fingerprint (This part is only nessary for the SFTP solution). Since we need the SSH fingerprint for logging into the SFTP we can obtain this by connecting to the SFTP from the normal WinSCP interface and doing the following steps:

-Download WINSCP: https://winscp.net/eng/download.php

-Make a SFTP session to the server you wish to monitor and connect to this session

-When the session is open, navigate to: Session->Server/Protocol information

-SSH fingerprintet is written under Server host key fingerprint

-Copy the fingerprint and paste it in the below script where there’s a lot of xx:xx:xx:

 

Obtaining the WinSCP .NET Assembly

Now we need to download WinSCP in a Powershell friendly edition, called the WinSCP .NET Assembly, get it by following this link: https://winscp.net/eng/docs/library_install

The files you need to succesfully run the script from a SCOM server is the .exe file and the .dll file.

 

Preparing the script for SCOM

Since we want to make the script work for SCOM, we need a way to communicate back to SCOM, this can be done by creating a propertybag. But don’t worry, we can also just for test communicate to the command-line, or if you want the final script just to write the result to the commandline, or send an exitcode.

The lines we need to implement a propertybag is the following lines:

Now we want to write either a succes or a failure to the propertybag, this is optained in either finally or the catch of the exception, first the succes:

And the failure:

Ok, so what is happening here? We just set the same parameters in SCOM to some specific string. We set the State to Healthy or Error, and write the exception message to the Description field.

Notice that we can just uncomment the lines if we want them written to the console/commandline on the following lines:

And if we also want exit-codes:

 

Setting the parameters

We also want to define which files to upload and which to delete, we set them in the start by the following lines:

Also I want to make a new file everytime, so I can just login and see when the file was created, and not depending on the test-file always being available, this is done by this line:

 

Please notice that we’re downloading everything in the remote folder, so please make a seperate folder for the test files, so you don’t download everything!

 

Changes if we want to monitor FTP

There’s only two things we need to change, the following two lines:

And we want to change them into:

We just change the connection type to FTP instead of SFTP, and we just uncomment the SSH fingerprint.

 

Below is the full script. Now you just need to set it up as a monitor in SCOM.