Remote Support: how to receive script output via email Objective: Have script results emailed to you. The example below uses free disk space, but the procedure used below is useful in many situations.
Here's an overview:
[1] Write your script.
[2] Install BLAT
[3] Task Schedule a batch file which calls your script
[1] Write your script
Here's a sample script written in VB. There is tons of info online about WSH so I won't go into a lot of detail. Just ensure the script works locally before trying it on remote PCs. Oh! And don't test it on a live network.
' FREESPACE.VBS
' Report Amount of free space on each disk for each server specified in Input File
' Usage: cscript freespace.vbs > freespace.txt
' Script works fine throws an error on exit...?
Const INPUT_FILE_NAME = "D:\Scripts\Computers.txt"
Const FOR_READING = 1
Const HARD_DISK = 3
Dim strFreeSpaceinMBs
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
Wscript.Echo "Free Disk Space Report"
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Wscript.Echo "------------------------------------------"
Wscript.Echo "Server: "& vbTab & strComputer
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
For Each objDisk in colDisks
strFreeSpaceinMBs = objDisk.FreeSpace
strFreeSpaceinMBs = strFreeSpaceinMBs /1048576 'convert to MB
strFreeSpaceinMBs = Round (strFreeSpaceinMBs, 2)
Wscript.Echo "Disk: "& vbTab & objDisk.DeviceID & " " & "Free Disk Space: "& vbTab & strFreeSpaceinMBs & " MB"
Next
Next
[2] Install BLAT
Download it and then install it from
blat.net.
Here's the install bit:
Blat -install smtphost.bar.com foo@bar.com
More... Here's how to send a test email:
blat myfile.txt -to username@yourdomain.com -s "Testing BLAT"
The point here is that Remote Support (username@yourdomain.com) will receive a test mail from foo@bar.com, proving BLAT is working OK. foo@bar.com can be an Administrator email box on the domain you want to monitor.
Troubleshooting BLAT: Add these to your command line: -superdebug -log blat.log
When Blat completes, check the log to see if your attachment was sent as
well. This is your first clue to what happened, if the attachment went, and if the problem is at your end or the far end. Worth noting is that Yahoo! Groups host a
BLAT mail list.
OK, that's a script written and BLAT installed. Time to put all the bits together.
[3] Schedule a batch file to call your script
The freespace.vbs script logs to freespace.txt, this file is then emailed to you by BLAT.
REM FREESPACE.CMD
cscript freespace.vbs > freespace.txt
cd blat250
cd full
blat D:\scripts\freespace.txt -to username@yourdomain.com -s "Daily Free Disk Space Report"
In the above example, BLAT.EXE is D:\scripts\blat250\full.
Now just call the above with Task Scheduler! Easy-peasy!
Finally archive the emails you receive: put the free disk space data into an Excel spreadsheet for example. Unfortunately the ever-lovable pie chart may not be an appropriate graphical representation of our data set. Then, when a customer asks you: "based on current data usage rates, when do you predict we should think of buying a new file server?", you can whip out some bar-charts and (with a cheeky grin) intone: "Funny you should ask "I have that information right here!". Note- The above script in [1] could be modified to log freespace to a .CSV file and the results then BLATTED, but that's for another day.
To summarise, here are our cast of characters:
FREESPACE.CMD - a batch file
FREESPACE.VBS - a WSH script
FREESPACE.TXT - a text file
BLAT.EXE - an executable
FREESPACE.CMD is Task Scheduled to call FREESPACE.VBS which creates FREESPACE.TXT which is then emailed by BLAT.EXE to username@yourdomain.com