![]() |
|
|
| Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site | ||
|
|
Mailbox Commands - Executes CMD commands on servers via Email Download scripts Ever wanted to manage your network via the email system ?? Ever been stuck in an Airport Terminal trying to diagnose a problem over the phone and muttered "my kingdom for a cmd session". Then this might be the script you've been looking for. This script uses a WSS on_save event sink to allow you to send cmd commands as the text of an email message to a specific mailbox and have those commands executed by the mail server. For example you could logon to your hotmail account send a mail to your exchange server mailbox with the subject Mailbox CMD and a body of "ping myremoteserver" to check if your remote server was up. Your email server would carry out this command and return results to you in an email. This allows you to do basic troubleshooting on your network no matter where you are as long as you can get to a replyable email address. (This could even include your WAP email phone if you're lucky enough to have one) When you consider how many functions you can accomplish via the command line with the NT resource kit utilities it opens up a world of possibilities. The down side of this script is security, if someone external to your network worked out what you where doing they could then reek havoc . So if you are going to consider using this script you should customise which subject line makes this event fire and consider it as a break in emergencies type utility. Using this in conjunction with OWA is more secure as the mail traffic won't be going over any public networks. You may want to also consider porting this to visual basic code see my other article for more details How it works I've used a two step approach to firing this event that adds a layer of abstraction for the Exchange event sinks. The event sink code looks as follows, this uses a Async On_Save event sink <SCRIPT LANGUAGE="VBScript"> This piece of code spawns a process that starts the main script and passes the URL of the email that caused the event to fire. The main script then opens up the email via CDO and checks for "MailBox CMD" in the subject line, then reads in the textbody of the email, it then executes each of the lines of the textbody as a separate cmd process that reports the result of execution to the result file. Once all commands have been executed the script then creates a mail that responds to the primary email and reads in the results file as the text body of this email. To cope with extended ASCII characters that most email clients can deal with but the NT command line can't interpret two mid functions are used to change "" which are in extended ASCII in emails back to the standard 127 ASCII. You may find you have problems with other extended ASCII characters and may need to implement more Mid parsing lines in this case. Also be carefully that if your entering commands in your email requests with server names eg \\myserver that these name may get automatically reformatted by the mail client to a file URL. This causes problems because when the server goes to execute the command it will try to execute the reformatted hyperlink which in most cases looks like "file:\\myserver". Outlook Express is notorious for doing this to remove the auto-formatting highlight the auto created hyperlink and right click then select remove hyperlink. Notes: With the mails you send to trigger this event the Subject is case sensitive so for example in the code below you must have the case for the subject as "Mailbox CMD" to fire the event. Main Script Dim WshShell, nfile, fso, msgobj, msgobj1
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
if I = 0 then
inbstr = objArgs(I)
else
inbstr = inbstr & " " & objArgs(I)
end if
Next
set WshShell = CreateObject("WScript.Shell")
Set msgobj = CreateObject("CDO.Message")
set msgobj1 = CreateObject("CDO.Message")
Set fso = CreateObject("Scripting.FileSystemObject")
if (fso.FileExists("c:\temp\results.txt")) Then
fso.deletefile("c:\temp\results.txt")
End If
slen = 1
msgobj.DataSource.Open inbstr
if msgobj.subject = "Mailbox CMD" then
Do until stratend = 1
if instr(slen,msgobj.textbody,vbCrLf) then
elen = instr(slen,msgobj.textbody,vbCrLf)
elen1 = elen - slen
comex = mid(msgobj.textbody,slen,elen1)
slen = elen + 1
on error resume next
comex = replace(comex,chr(147),chr(34))
comex = replace(comex,chr(148),chr(34))
comex = "cmd.exe /C " & comex & " >> c:\temp\results.txt"
strrun = WshShell.run (comex,1,TRUE)
else
stratend = 1
end if
loop
With msgobj1
.From = "newb2@ad1"
.Subject = "Command Results"
.To = msgobj.from
End With
set nfile = fso.opentextfile("c:\temp\results.txt",1,true)
Do until nfile.AtEndOfStream = True
inpline = nfile.readline
msgobj1.textbody = msgobj1.textbody & inpline & vbCrLf
loop
nfile close
msgobj1.send
end if
set msgobj = nothing
set msgobj1 = nothing
set fso = nothing
set objArgs = nothing
set wshshell = nothing
Registering this script For details on installing and registering this script see my previous article Using VBS Event Sink scripts with the Web Storage System. Or have a look in the ESDK search for regevent.vbs. Download scripts |
|
|
|
Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Pro Exchange. OutlookExchange.Com and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
© Copyright Pro Exchange, Inc., 2006