One of our users has an occasional requirement to manually run a scheduled task on a server. I wanted him to do this without logging on via RDP. Scheduled tasks can be run remotely using the SVHTASKS program (on Windows XP and Server 2003) so I started writing a VB Script to call this app. The tricky part was capturing the output of the command and displaying that back to the user, so he would know if it was a success or failure. It turns out this is actually quite difficult to do, so I hacked up various other scripts I found on the web until I eventually got this to work. The working version is below.
strServer =
"your_server_name"
strTask =
"task_name"
Continue = MsgBox ("Scheduled job " & strTask & " will run on " & strServer & ". Are you sure?",vbYesNo + VBQuestion, "Run scheduled task")
If Continue = vbYes then
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
sExe = "SCHTASKS /Run /S " & strServer & " /TN " & strTask
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
oShell.Run "%comspec% /c " & sExe & " >" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
' capture output and inject into a variable
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
MsgBox sResults , vbInformation , "Result"
Else
wscript.quit
End If
I've been asked by a group of users if they can have a daily report of everything the fax server has sent. Since the fax software we're using doesn't have this feature, I thought I'd write some vb script to pull all of lines containing yesterday's date from the log file (called sendlog.txt) and email them to a public folder. This took an hour or so to create and looks like this:
Const ForReading =
1
Dim fso, TempFile, yesterday, strSubject
yesterday = Date - 1
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = yesterday
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists("C:\temp\output.txt")) Then
objFSO.DeleteFile "C:\temp\output.txt"
End If
Set objFile = objFSO.OpenTextFile("C:\Program Files\GFI\FaxMaker\sendlog.txt", ForReading)
Set TempFile = objFso.CreateTextFile("C:\Temp\output.txt", True)
Set wshShell = WScript.CreateObject ("WSCript.shell")
TempFile.Write "Fax Server sendlog for " & yesterday & vbCRLF & "----------------------------------" & vbCRLF & vbCRLF
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
TempFile.Write strSearchString & vbCRLF
Next
End If
Loop
objFile.Close
TempFile.Close
strSubject = "FaxMaker_Sendlog_" & yesterday
wshShell.run "C:\monitoring\blat262\full\blat.exe" & " " & "C:\temp\output.txt" & " -to " & "myaddress@mydomain.com" & " -subject " & strSubject
Partial credit goes to the MS Scripting Guy who had already published part of this for me. Also you'll notice I called blat to email the file, which is considerably less complex than sending it in the vbs (or cheating, as some might call it) ... using vbs to send the message would have doubled the amount of code!
To accomplish the same task using a bash script, you could do it like this:
echo "Fax server sendlog for `date +%d/%m/%y`" > /tmp/tempfile.txt
cat /var/log/sendlog.txt | grep `date +%d/%m/%y` >> /tmp/tempfile.txt
mail -s "Fax Server log for `date +%d/%m/%y`" myaddress@mydomain.com < /tmp/tempfile.txt
Much simpler I think - only 3 lines of code!! People say Linux is harder than Windows but I think this proves that is not always the case. Manipulating files and data in Linux can take considerably less effort if you know which tools to use and how to use them.
Recent Comments