Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


looking for help in CreateObject("Wscript.Shell")

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,35999
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> looking for help in CreateObject("Wscript.Shell")
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 looking for help in CreateObject("Wscript.Shell&qu... - 7/6/2006 4:24:10 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Hello!
I am using the code below as part of my backup script to send email notification using the blat utility, i did not received any errors, and i did not received also email on my mailbox:

      
But when i typed manually the above string on the command line as follows, i will successfully received the email:

      
Thanks in advance for any help.
 
 
Post #: 1
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 4:34:45 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Change this line:
shellobj.Run("%COMSPEC% /C blat " & backupLog & " -t " & email_recipients & " -f " & email_from & " -server " & smtp_server & " -s " & email_subject0)


to be this:
strCMD = "%COMSPEC% /C blat " & backupLog & " -t " & email_recipients & " -f " & email_from & " -server " & smtp_server & " -s " & email_subject0
WScript.Echo strCMD
shellobj.Run strCMD


Then type whatever it displays exactly as is at the command line a see if it works. I suspect that you need some "s somewhere in there.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to desert_storm28)
 
 
Post #: 2
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 4:48:19 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Thank you!
A message box pops-up with the  following message:
%COMSPEC% /C blat D:\backup\backup.log -t myemail@mydomain.com -f admin@mydomain.com -server smtp.mydomain.com -s SUCCESS: TEST backup on SERVER1 was successful

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 5:03:12 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
So if you type that exact string into a command prompt does it do what you expect it to?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to desert_storm28)
 
 
Post #: 4
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 5:25:46 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Yes, I will received the message on my mailbox if I type the string exactly in the command prompt, and this blat utility is working fine in batch file like this:

      
So, I included the above batch file in my vbscript like this and it is working fine:

      
But I want the batch script to be translated to vbscript so that I only have one script.

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 5:30:43 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Change the /C to a /K so that you can see what is being run.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to desert_storm28)
 
 
Post #: 6
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 5:48:55 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
I see now the problem by changing to /K, because after the processing of the email, i have the following line:
filesys.DeleteFile "d:\backup\backup.log"
and it seems that it did'nt wait for the email to process first before executing the delete, is there anyway to finish the process of the email first before deleting the file? below is my entrie script for you to see:

      

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 6:04:12 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Look at the documentation for the .Run method. Especially the second and third parameters. Setting the third parameter to True will make script execution wait until the process that .Run starts completes it's operation.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to desert_storm28)
 
 
Post #: 8
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 6:10:08 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Thank you so much, it's working now without any errors by adding the True parameter.

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 7:03:29 PM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Hello ebgreen!

I also added a  paging notification on my script, and am trying to put an option to turn ON or OFF the email nad pager notification, I put a variable like this (0=ON/1=OFF):


      
but if I changed the variable into 1, i will again recevied the email and pager alert, in this case I will received the failed alert.

< Message edited by desert_storm28 -- 7/6/2006 9:59:33 PM >

(in reply to desert_storm28)
 
 
Post #: 10
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 10:32:08 PM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
VBScript does not enforce variable typing so it does not complain about something that other languages would complain about. You declare your variable as a stirng but you compare it as a decimal. Try doing it one way or the other. Actually to make thing a little more clear I would use True/False instead of 1/0.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to desert_storm28)
 
 
Post #: 11
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/6/2006 10:43:17 PM   
  ehvbs

 

Posts: 2114
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi desert_storm28,

plan before code:
=================

Conditions:

    (1) Alerts : (c) You want emails
                 (d) You don't want emails

    (2) The could be 0, 1, 2, ... Error, let's reduce this to
        Errors : (a) There is at least one Error
                 (b) There are no Errors att all

Actions:

    (1) Send email       (a) SUCCESS
                         (b) FAIL
    (2) Don't send email (c) ./.

What to do When (check all possible combinations of conditions
==============================================================

    Errors  Yes \___  Send FAIL
    Alerts  Yes /

    Errors  Yes \___  Send FAIL  (I think, you may decide otherwise)
    Alerts  No  /

    Errors  No  \___  Send SUCCESS
    Alerts  Yes /

    Errors  No  \___  Don't Send
    Alerts  No  /

Variables (initialized at start to reasonable values):
======================================================

Dim nErrors   : nErrors   = 0             ' Integer to count Errors; initially 0
Dim bErrors   : bErrors   = (0 < nErrors) ' Bool to reduce 0,1,2,..Errors to No Or Some
Dim bDoAlert  : bDoAlert  = True          ' or False, as you like it
Dim sDecision : sDecision = ""            ' what to do in clear text (don't know yet)
                                          ' but possible values are
                                          '   "Send FAIL"
                                          '   "Send SUCCESS"
                                          '   "Don't Send"

Determine conditions:
=====================

   If <CommandLineArgSilent> Then bDoAlert = False
   ...
   If <SomethingIsWrong> Then nErrors = nErrors + 1
   ...
   bErrors = (0 < nErrors)

Implement Condition => Decision mapping systematically (may be inefficient):
============================================================================

                                    ' Errors    Alerts
   If bErrors Then
      If bDoAlert Then
         sDecision = "Send FAIL"    '  True      True
      Else
         sDecision = "Send FAIL"    '  True      False
      End If
   Else
      If bDoAlert Then
         sDecision = "Send SUCCESS"  ' False      True
      Else
         sDecision = "Don't Send"    ' False      False
      End If
   End If

Implement Action (again, not short or fast but easy to see/check):
==================================================================

   Select Case sDecision
      Case "Send FAIL"
        WScript.Echo bErrors, bDoAlert, sDecision, "Failure"
      Case "Send SUCCESS"
        WScript.Echo bErrors, bDoAlert, sDecision, "Success"
      Case "Don't Send"
        WScript.Echo bErrors, bDoAlert, sDecision, "you can't see this, because I didn't send!"
      Case Else
        WScript.Echo "***** Fatal Programmer's Error *******"
   End Select

Implement test code to gain confidence:
======================================

Dim nErrors, bErrors, bDoAlert, sDecision

For Each nErrors In Array( 0, 5 )
    For Each bDoAlert in Array( True, False )
        bErrors = (0 < nErrors)
        WScript.Echo "-------- " & nErrors & " " & bDoAlert & " --------"
        If bErrors Then
           If bDoAlert Then
              sDecision = "Send FAIL"    '  True      True
           Else
              sDecision = "Send FAIL"    '  True      False
           End If
        Else
           If bDoAlert Then
              sDecision = "Send SUCCESS"  ' False      True
           Else
              sDecision = "Don't Send"    ' False      False
           End If
        End If
        Select Case sDecision
           Case "Send FAIL"
             WScript.Echo bErrors & " " & bDoAlert & " " & sDecision & " Failure"
           Case "Send SUCCESS"
             WScript.Echo bErrors & " " & bDoAlert & " " & sDecision & " Success"
           Case "Don't Send"
             WScript.Echo bErrors & " " & bDoAlert & " " & sDecision & " you can't see this, because I didn't send!"
           Case Else
             WScript.Echo "***** Fatal Programmer's Error *******"
        End Select
    Next
Next

It looks kind of ugly even when viewed using a monospaced font, but I think
you'll get the drift and I hope you'll find that this method will get
you thru more complicated cases painlessly.

Good luck!

P.S. As ebgreen suggested: use numbers to count, but booleans to store binary decisions.

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/7/2006 6:43:37 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
many thanks ehvbs! i will try to study and implement your code... also thanks to ebgreen for his suggestion.

< Message edited by desert_storm28 -- 7/7/2006 7:43:37 AM >

(in reply to ehvbs)
 
 
Post #: 13
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/9/2006 6:10:26 PM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Hello ehvbs, Im having hard time integrating your code to my script.

(in reply to desert_storm28)
 
 
Post #: 14
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/9/2006 11:37:55 PM   
  ehvbs

 

Posts: 2114
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi desert_storm28,

my code wasn't meant to be integrated into a script but to show a procedure
how to develop a script with non trivial interaction between conditions and
actions dependend on those conditions.

The steps are:

  (1)  compile a complete list of the conditions you want to take into consideration

  (2)  compile a complete list of the actions you want (your script) to take

  (3)  For all possible combinations of coditions: identiy the action to take for
         this combination of conditions (do it systematically; don't optimize in
         this early stage)

  (4)  Dim/Initialize variables for the conditions (think about suitable variable
         types and default values)

  (5)  Think about/write code to set these variables during the execution of
         your script

  (6)   write boilerplate code (nested if/else statements) to map combinations
          of conditions to action identifiers

  (7)  write boilerplate code (select case statement) to map action identifiers
         to (calls of) actions (subroutines)

  (8)  write a simple test script to test your code (replacing actions with
         "WScript.Echo <that's what I'd do>" lines

If you are willing to do steps (1) to (3), we could work on (4) together and decide
how to proceed.

(in reply to desert_storm28)
 
 
Post #: 15
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/10/2006 6:41:34 PM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Hello ehvbs!

Thank you for all the teaching, after having a hard time, I came up with these code, I think these code fit my requirements... what do you think?

      

< Message edited by desert_storm28 -- 7/10/2006 7:04:32 PM >

(in reply to ehvbs)
 
 
Post #: 16
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/10/2006 9:26:58 PM   
  ehvbs

 

Posts: 2114
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi desert_storm28,

what do I think? Well, I think you should plan before you storm off to code.
What are your requirements exactly? Could you state the purpose of your script
in one sentence?

You are entitled to do your work the way you think best. I really have no
problems with that. This is no threat "I won't help you, if we don't do
it my way". Probably you get results faster if we tinker with the code
you posted. But why did you ignore my advice to keep the action dispatch
(select case) separated from the determining of the conditions (errors=0)?
Do you think it futile or difficult to come up with the lists I asked for
in my last posting? Even if they are not necessary for you, they would
help me to understand your problem better.

If you don't feel confident with my approach using lists, how would you
transform

    '0 - no ALERTS
    '1 - email success/fail alerts only.
    '2 - pager success/fail alerts only.
    '3 - email and pager, both success/fail alerts.
    
to a usage message for your script?   

And one nitpicking detail: If you use the hungarian type prefix, use it correctly:
"sAlert = 3" would qualify as fraud in my book. There is an article at

  http://www.joelonsoftware.com/articles/Wrong.html

which made me think about my coding style.

 

(in reply to desert_storm28)
 
 
Post #: 17
 
 RE: looking for help in CreateObject("Wscript.Shel... - 7/11/2006 1:13:56 AM   
  desert_storm28

 

Posts: 13
Score: 0
Joined: 7/6/2006
Status: offline
Sir, actually, am not a programmer, but I respect your coding standards, i am currently an OJT in a small firm as systems administrator, and I am only learning VBS in the past few days, and they assigned me a task to put together all their backup script into one VBS script, because some of their backup script was written in batch file. Right now, they had separate VBS scripts and batch file to backup the System State, Exchange Information Store, SQL Servers databases, and all important files in the server. And they'll be using the script I'm currently doing in all their servers as standard backup script in the future.

They are using the built-in ntbackup in Windows, the 7-zip compression utility to compress the ".bkf" file, the blat utility to send an email alert, and their paging terminal to send a pager alert.

So, my task is to put all together these scripts into one, because of pressure, i made a hurry to finish off the script without any programming standards, and the other code of this script was taken off from other websites, and my task is to complete the following in one script only:
1) an option to backup the System State or not.
2) an option to backup only the System State.
3) an option to backup the System State together with the ".bks" selection file.
4) an option to send email and pager success/fail alert to administrator after the backup process, and they told me to put an option also to turn off/on these feature, because they do not want to un-comment the code if they do not want an alert.
5) an option for local and remote house keeping.
6) an option to copy the backup to remote media1 if current day is even, and to remote media2 if the current day is odd.

Actually, the script is almost done, and what i will do now is to improve the coding standards. Please find below the entire script: