Tag Archives: VBScript

Function to check if a file exists in VBScript

It’s very easy to check if a file exists in VBScript, but to make this common task even easier it’s best to use a quick function to do the job. I’ve written two functions, one using the FilesystemObject and another using WMI, both of which return a Boolean.

VBScript Function to check if a file exists on the local computer

Function FileExists(FilePath)
  Set fso = CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(FilePath) Then
    FileExists=CBool(1)
  Else
    FileExists=CBool(0)
  End If
End Function

To use the function just do something like the following:

If FileExists("c:\testfolder\testfile.txt") Then
  WScript.Echo "Does Exist"
Else
  WScript.Echo "Does not exist"
End If

This will work perfectly if you are only looking for files on your local machine. But what if you want to check if a file exists on a remote machine?

VBScript Function to check if a file exists on a remote machine

Function FileExists(FilePath, Computer)
  Set objWMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
  FilePath = Replace(FilePath, "\", "\\")
  Set Files = objWMIService.ExecQuery ("Select * From CIM_Datafile Where Name = '" & FilePath & "'")
  
  If Files.Count > 0 Then
      FileExists=CBool(1)
  Else
      FileExists=CBool(0)
  End If
End Function

Use the function like this:

If FileExists("c:\testfolder\testfile.txt", "testcomputer") Then
  WScript.Echo "Does Exist"
Else
  WScript.Echo "Does not exist"
End If

 

Setting up email alerts from Dell Openmanage (OMSA)

Dell Openmanage Server Administrator is a really useful tool for configuring and monitoring server hardware but it lacks built in E-Mail alerting or notifications.

The software is capable of calling external programs or scripts when a server alert is triggered so I have written a simple script that provides E-Mail alert functionality.

There are a couple of scripts out there that do similar, but I wanted something self contained and capable of using SMTP Authentication with an external relay.

The other problem with OMSA is that you need to go into each individual alert to configure the settings which can be very time consuming, I wanted a way to add them all automatically as I am going to install the script on a number of servers.

Once you have downloaded and extracted the zip file you will need to edit the variables at the top of the script to make them appropriate for your environment:

The variables at the top define the e-mail settings and are hopefully self explanatory. If a remote SMTP server is not specified the local machine will be used to send the E-Mail.

Below these you can to decide what events you want to be notified of, just comment out any that you aren’t interested in, e.g:

AlertConfig(0)="powersupply|Power supply failure"
AlertConfig(1)="powersupplywarn|Power supply warning"
'AlertConfig(2)="tempwarn|Temperature warning"
'AlertConfig(3)="tempfail|Temperature failure"

I have included every alert available in OMSA 6.2 on a PE T710, some of these alerts won’t be available on older versions or machines.

After you have configured the script, I suggest you test that the E-Mail options by running:

cscript dellalert.vbs testemail

You should receive a test e-mail, if not something isn’t working properly.

When you have successfully tested e-mail, configure OMSA to send alerts by running:

cscript dellalert.vbs setup

You will then see the script go through and setup all the appropriate alerts as per your settings. This process can take a couple of minutes as the “omconfig” command takes a while. This step will not work if you do not have OMSA installed.

Once this is done you should be able to see that the script has been added into OMSA:

Now you need to generate some real alerts to test the script properly. How you do this will depend on the capabilities of your server, I was able to disconnect the redundant power supply and have an alert generated almost immediately. I think opening the case will also generate an event, but I am not sure as I didn’t test. Hopefully you should get something like this in your Inbox:

Update: There is a new version of this script which is written entirely in Powershell and will work with Windows Server Core etc.  Please visit :

https://www.tachytelic.net/2011/09/dell-openmanage-server-administrator-e-mail-alerts-updated-powershell-script/

Download both versions by following the PayPal link.

GBP 2.95 / Download

 Get these scripts for FREE!

 If you are an Office 365 user you can get this script for free by assigning me as your Office 365 Partner of Record.  If you would prefer to get the script for free please follow the instructions on this page:

https://www.tachytelic.net/setting-tachytelic-net-office-365-partner-record/

Once you have made the change to your 365 subscription please send an email through this contact form and I will send you the scripts free of charge: