We help IT Professionals succeed at work.

VBScript:  Running a Dos Command with Confirmation and Logging

superfly18
superfly18 asked
on
427 Views
Last Modified: 2010-04-07
Hi Experts:  I am in need of some VBScript help again.  I would like to create a script that would do the following:

Ask the user if they would like to run a particular Dos command (e.g. run a BAT file).  

For example:  Would you really like to run c:\xxx\xxx? (Y/N):
Please confirm by entering your email address:
---the dos command is run, and then the users email address, their windows login (taken from the system), and the time the user ran the application are logged to a separate text file which would have the date and time the script was run (e.g. c:\log\scriptname-[datetimerun (in the format of 01012001)].txt

Is this possible??

Please let me know,
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
VinnyD...that is Awesome!!!!

Author

Commented:
VinnyD:  How would I add time HHMMSS to the filename?

Thanks in advance

Commented:
Give this a try and let me know if it is what you are looking for. It will add AM or PM onto the end of the filename. If you would rather have it in 24 hour format let me know.For example would you rather have 1:00:00 pm listed as 010000PM or 130000. If you want it as 130000 I will try to modify the example to do that.


Dim fso,wshell,fil,User,rsp,email,net,arrDate,mTime,arrTime
Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("Wscript.Network")
User = net.UserName
Set net = Nothing
rsp = MsgBox("Would you like to run myfile.bat?",vbYesNoCancel,"Run Bat file?")
if rsp = vbYes then
     email = InputBox("Enter e-mail address")
     Set wshell = CreateObject("Wscript.Shell")
     wshell.Run "C:\MyFile.bat"
     arrDate = Split(Cstr(Now),"/")
     if Len(arrDate(0)) = 1 Then
          arrDate(0) = "0" & arrDate(0)
     end if
     if Len(arrDate(1)) = 1 Then
          arrDate(1) = "0" & arrDate(1)
     end if

     mTime = Mid(arrDate(2),Instr(arrDate(2)," ") + 1)
     arrTime = Split(mTime,":")
     
     if Len(arrTime(0)) = 1 Then
          arrTime(0) = "0" & arrTime(0)
     end if

     arrDate(2) = Left(arrDate(2),Instr(arrdate(2)," "))

     Set fil = fso.OpenTextFile("C:\log\" & arrDate(0) & arrDate(1) & arrDate(2) & "-" & _
            arrTime(0) & arrTime(1) & Left(arrTime(2),Instr(arrTime(2)," ") -1) & _
            Mid(arrTime(2),Instr(arrTime(2)," ") + 1) & ".txt",2,True)
     fil.WriteLine User & "," & Now    
     fil.Close
end if

set wshell = Nothing
Set fso = Nothing

Author

Commented:
Actually, that was exactly what I was looking for.  Many thanks!!! again,
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.