Announcement

Collapse
No announcement yet.

Running an invisible script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running an invisible script

    I have created a script to zip up files with the help of this forum, and it is working great.
    I would like it to run in the background, and append all the actions to a log for later analysis.

    Here is the script, which starts up with a batch file that provides the variables:

    Code:
    '***********************************************************************
    ' AFTER THE 2ND DAY OF EACH MONTH, AND BEFORE THE LAST DAY OF THE MONTH, 
    ' FILES IN THE VARIOUS FOLDERS (AS SPECIFIED BY "source_folder") NEED TO 
    ' BE ZIPPED TO AN ARCHIVE FOLDER (AS SPECIFIED BY "destination_folder")
    ' THIS IS RUN AS A SCHEDULED TASK ON AKLAP915 THAT STARTS 
    ' "BRIEFINGBACKUP.BAT", AND PROVIDES THE FOLDER AND FILENAME VARIABLES.
    '
    ' THE BACKUP INCLUDES FILES MODIFIED:
    ' - FROM THE LAST DAY OF TWO MONTHS AGO,
    ' - ALL FILES FROM PREVIOUS MONTH,
    ' - ALL FILES UP TO AND INCLUDING THE FIRST DAY OF THE CURRENT MONTH.
    '
    ' SO TWO CONDITIONS SHOULD BE MET:
    '    File.DateLastModified >= last_Day_Pre-Prev_Month
    '          *AND*
    '    File.DateLastModified =< first_Day_Current_Month
    '
    ' IN THE ARCHIVE FOLDER A SUBFOLDER NEEDS TO BE CREATED FOR THE BACKUP, 
    ' AND THE NAME MUST BE REFERRING TO THE PREVIOUS MONTH.
    '***********************************************************************
    '***********************************************************************
    ' CREATE VARIABLES
    '***********************************************************************
    Dim source_folder, destination_folder, archive_file, filelist, tt
    d = Date : pre = DateSerial(year(d),month(d),0)
    
    fstDayCurrentMonth = DateSerial(year(d),month(d),1)
    lstDayPrePrevMonth = DateSerial(year(pre),month(pre),0)
    '**********************************************************************
    ' THESE ARE THE CONSTANT VALUES
    '**********************************************************************
       source_parent      = "D:\Briefing_Backups"
       destination_parent = "D:\Briefing_Backups\Archive"
       filelist           = "D:\Briefing_Backups\filelist.lst"
    '**********************************************************************
    ' GET THE ARGUMENTS OBJECT
    '**********************************************************************
       Set ArgObj = WScript.Arguments
       var1 = ArgObj(0) : var2 = ArgObj(1)
    '- # -                               - # -
    dStamp = Monthname(Month(pre)) & "_" & Year(pre)
    source_folder = source_parent & "\" & var1
    destination_folder = destination_parent & "\" & dStamp
    archive_file = var2 & "_" & dStamp & ".zip"
    tt=0
    BackupFiles lstDayPrePrevMonth, fstDayCurrentMonth
    wsh.echo "Total days in backup: " & _
         1+DateDiff("d", lstDayPrePrevMonth, fstDayCurrentMonth) _
         & vbNewLine & "Number of Files backed up:", iCnt
    wscript.quit
    
    Sub BackupFiles (d_A,d_B)
       Dim FSO, oSourceFolder, oFile, iCnt
       Dim dLM, dA, dB, runcmd, appWinZip
       
       appWinZip = "D:\Winzip\Winzip32.exe"
       dA = Year(d_A)*1e4 + Month(d_A)*1e2 + Day(d_A)
       dB = Year(d_B)*1e4 + Month(d_B)*1e2 + Day(d_B)
       iCnt = 0
       Set FSO = CreateObject("Scripting.FileSystemObject")
       If FSO.FolderExists(destination_folder) = False _
         Then FSO.CreateFolder(destination_folder)
       Set oSourceFolder = FSO.GetFolder(source_folder)
    
       ' Create the list of files to be zipped
       With FSO.CreateTextFile(filelist,true)
         For Each oFile in oSourceFolder.Files
           dLM = oFile.DateLastModified
           dLM = Year(dLM)*1e4 + Month(dLM)*1e2 + Day(dLM)
           If (dLM >= dA) AND (dLM =< dB) Then
               .writeline source_folder & "\" & oFile.name
               iCnt = iCnt + 1
           End If
         Next  
         .close
       End With
      If iCnt > 0 Then 
       ' populate archive according the filelist
       runcmd = "cmd /c title Backup to ZIP & COLOR 72 &"
       runcmd = runcmd & " """ & appWinZip & """ -a """ _
               & destination_folder & "\" & archive_file _
               & """ @""" & filelist & """"
       CreateObject("WScript.Shell").Run runcmd, 1, True
       tt=tt + iCnt
       rem FSO.deletefile filelist
      End If
    End Sub

  • #2
    Re: Running an invisible script

    Create a service account, that is, an user account that has appropriate rights. Create a scheduled task to run your script and set the account you created to run the task.

    -vP

    Comment


    • #3
      Re: Running an invisible script

      I have set it up that way, but the problem is we have an RDP session to the server that uses that account, so the users see the script running.

      Comment


      • #4
        Re: Running an invisible script

        I've tried using another account for running the script, and it is working fine.

        Thank you for the assistance.

        Comment


        • #5
          Re: Running an invisible script

          I still need help with creating a logfile though.

          Comment


          • #6
            Re: Running an invisible script

            Not sure, are you looking for something like cmdow?
            Marcel
            Technical Consultant
            Netherlands
            http://www.phetios.com
            http://blog.nessus.nl

            MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
            "No matter how secure, there is always the human factor."

            "Enjoy life today, tomorrow may never come."
            "If you're going through hell, keep going. ~Winston Churchill"

            Comment


            • #7
              Re: Running an invisible script

              Originally posted by krokkie View Post
              I still need help with creating a logfile though.
              Take a look at Scripting.FileSystemObject. An example.

              -vP

              Comment


              • #8
                Re: Running an invisible script

                Cheers, I will give it a try.

                Comment


                • #9
                  Re: Running an invisible script

                  1) see green colored script part,
                  Originally posted by krokkie View Post
                  I have created a script to zip up files with the help of this forum, and it is working great.

                  1) I would like it to run in the background, and
                  2) append all the actions to a log for later analysis.

                  Here is the script, which starts up with a batch file that provides the variables:

                  Code:
                  '***********************************************************************
                  ' AFTER THE 2ND DAY OF EACH MONTH, AND BEFORE THE LAST DAY OF THE MONTH, 
                  ' FILES IN THE VARIOUS FOLDERS (AS SPECIFIED BY "source_folder") NEED TO 
                  ' BE ZIPPED TO AN ARCHIVE FOLDER (AS SPECIFIED BY "destination_folder")
                  ' THIS IS RUN AS A SCHEDULED TASK ON AKLAP915 THAT STARTS 
                  ' "BRIEFINGBACKUP.BAT", AND PROVIDES THE FOLDER AND FILENAME VARIABLES.
                  '
                  ' THE BACKUP INCLUDES FILES MODIFIED:
                  ' - FROM THE LAST DAY OF TWO MONTHS AGO,
                  ' - ALL FILES FROM PREVIOUS MONTH,
                  ' - ALL FILES UP TO AND INCLUDING THE FIRST DAY OF THE CURRENT MONTH.
                  '
                  ' SO TWO CONDITIONS SHOULD BE MET:
                  '    File.DateLastModified >= last_Day_Pre-Prev_Month
                  '          *AND*
                  '    File.DateLastModified =< first_Day_Current_Month
                  '
                  ' IN THE ARCHIVE FOLDER A SUBFOLDER NEEDS TO BE CREATED FOR THE BACKUP, 
                  ' AND THE NAME MUST BE REFERRING TO THE PREVIOUS MONTH.
                  '***********************************************************************
                  '***********************************************************************
                  ' CREATE VARIABLES
                  '***********************************************************************
                  Dim source_folder, destination_folder, archive_file, filelist, tt
                  d = Date : pre = DateSerial(year(d),month(d),0)
                  
                  fstDayCurrentMonth = DateSerial(year(d),month(d),1)
                  lstDayPrePrevMonth = DateSerial(year(pre),month(pre),0)
                  '**********************************************************************
                  ' THESE ARE THE CONSTANT VALUES
                  '**********************************************************************
                     source_parent      = "D:\Briefing_Backups"
                     destination_parent = "D:\Briefing_Backups\Archive"
                     filelist           = "D:\Briefing_Backups\filelist.lst"
                  '**********************************************************************
                  ' GET THE ARGUMENTS OBJECT
                  '**********************************************************************
                     Set ArgObj = WScript.Arguments
                     var1 = ArgObj(0) : var2 = ArgObj(1)
                  '- # -                               - # -
                  dStamp = Monthname(Month(pre)) & "_" & Year(pre)
                  source_folder = source_parent & "\" & var1
                  destination_folder = destination_parent & "\" & dStamp
                  archive_file = var2 & "_" & dStamp & ".zip"
                  tt=0
                  BackupFiles lstDayPrePrevMonth, fstDayCurrentMonth
                  wsh.echo "Total days in backup: " & _
                       1+DateDiff("d", lstDayPrePrevMonth, fstDayCurrentMonth) _
                       & vbNewLine & "Number of Files backed up:", iCnt
                  wscript.quit
                  
                  Sub BackupFiles (d_A,d_B)
                     Dim FSO, oSourceFolder, oFile, iCnt
                     Dim dLM, dA, dB, runcmd, appWinZip
                     
                     appWinZip = "D:\Winzip\Winzip32.exe"
                     dA = Year(d_A)*1e4 + Month(d_A)*1e2 + Day(d_A)
                     dB = Year(d_B)*1e4 + Month(d_B)*1e2 + Day(d_B)
                     iCnt = 0
                     Set FSO = CreateObject("Scripting.FileSystemObject")
                     If FSO.FolderExists(destination_folder) = False _
                       Then FSO.CreateFolder(destination_folder)
                     Set oSourceFolder = FSO.GetFolder(source_folder)
                  
                     ' Create the list of files to be zipped
                     With FSO.CreateTextFile(filelist,true)
                       For Each oFile in oSourceFolder.Files
                         dLM = oFile.DateLastModified
                         dLM = Year(dLM)*1e4 + Month(dLM)*1e2 + Day(dLM)
                         If (dLM >= dA) AND (dLM =< dB) Then
                             .writeline source_folder & "\" & oFile.name
                             iCnt = iCnt + 1
                         End If
                       Next  
                       .close
                     End With
                    If iCnt > 0 Then 
                  
                     ' populate archive according the filelist
                     runcmd = "cmd /c title Backup to ZIP & COLOR 72 &"
                     runcmd = runcmd & " """ & appWinZip & """ -a """ _
                             & destination_folder & "\" & archive_file _
                             & """ @""" & filelist & """"
                     CreateObject("WScript.Shell").Run runcmd, 1, True
                     tt=tt + iCnt
                     rem FSO.deletefile filelist
                    End If
                  End Sub
                  "redirect display information returned by WinZip to a file (append to the file) in the user's temp folder" - sample,
                  Code:
                  ' CONSTANT VALUE
                  logfile = "%temp%.\TEST.txt"  ' (made it 'evergrowing' in this sample)
                  
                     ' populate archive according the filelist
                     runcmd = "cmd /c title Backup to ZIP & COLOR 72 &"
                     runcmd = runcmd & " (echo.___&echo.* %date% %time% &"
                     runcmd = runcmd & " """ & appWinZip & """ -a """ _
                             & destination_folder & "\" & archive_file _
                             & """ @""" & filelist & """"
                     runcmd = runcmd & " )>>""" & logfile & """ 2>&1"
                     CreateObject("WScript.Shell").Run runcmd, 0, True
                  additionally (optional),
                  I've changed WSH.RUN 2nd parameter (= intWindowStyle) from '1' to '0' this will make runcmd run hidden.
                  ___


                  2)
                  Originally posted by Dumber View Post
                  Not sure, are you looking for something like cmdow?
                  Yes - to run the scheduled wrapper script (the batch file that is launching this vbscript) hidden, you can use tools like RunHiddenConsole.exe, CMDOW.exe or Cwnd.exe.
                  Or,,, (?) don't use a batch to launch the script, just schedule: wscript.exe "pathto\script.vbs" "var1" "var2"


                  \Rems
                  Last edited by Rems; 19th September 2010, 14:16.

                  This posting is provided "AS IS" with no warranties, and confers no rights.

                  __________________

                  ** Remember to give credit where credit's due **
                  and leave Reputation Points for meaningful posts

                  Comment


                  • #10
                    Re: Running an invisible script

                    Cheers for that. I am busy testing it and will let you know how I get on.

                    Comment


                    • #11
                      Re: Running an invisible script

                      I have added the code, but only get the following output for the log:

                      ___
                      * Tue 21/09/2010 3:47:26.37
                      ___
                      * Tue 21/09/2010 3:48:23.47
                      ___
                      * Tue 21/09/2010 21:03:14.46
                      ___
                      * Tue 21/09/2010 21:04:49.21
                      ___
                      * Tue 21/09/2010 21:15:03.01
                      ___
                      * Tue 21/09/2010 21:15:08.71
                      ___
                      * Tue 21/09/2010 21:15:43.19
                      ___
                      * Tue 21/09/2010 21:15:54.35
                      ___
                      * Tue 21/09/2010 21:15:55.71
                      ___
                      * Tue 21/09/2010 21:15:57.49
                      ___
                      * Tue 21/09/2010 21:16:07.71
                      ___
                      * Tue 21/09/2010 21:16:27.91

                      Comment


                      • #12
                        Re: Running an invisible script

                        I don't know the Winzip32 program, what should be in that log?
                        What does Winzip32 normally show onscreen when it runs?


                        \Rems

                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        __________________

                        ** Remember to give credit where credit's due **
                        and leave Reputation Points for meaningful posts

                        Comment


                        • #13
                          Re: Running an invisible script

                          It displays things like filename, how many files are valid.

                          I will try to get you a screenshot tomorrow.

                          Comment


                          • #14
                            Re: Running an invisible script

                            Here are the screenshots of the batch file and Winzip.
                            Last edited by krokkie; 18th March 2014, 21:53.

                            Comment


                            • #15
                              Re: Running an invisible script

                              4 boxes are visible,
                              1st - the black DOS-box was for launching the vbscript (note, it might not be really nesseary to leave it open while archiving or, even to use this batch at all for starting the vbscript (??) )

                              2nd box - is the command line launched from within the vbscript. This box is empty because the output is being redirected to a text file (>log). You could make this box hidden with the '0 ' parameter.

                              3rd-4th - I can see a WinZip GUI screen is started. Wel you cannot redirect what is shown on the GUI to txt file.
                              In that case, to write log there should be,
                              - a special command line swith to export log (I don't think there is one).
                              Or
                              - There should be a global preference option in the application to enable logging.

                              Command line solution:
                              You should use the WinZip Command Line Support Add-on, it is a free add-on for users of WinZip with a valid license.

                              article: How to create a log file when using WinZip Command Line Support Add-on


                              \Rems
                              Last edited by Rems; 23rd September 2010, 14:23.

                              This posting is provided "AS IS" with no warranties, and confers no rights.

                              __________________

                              ** Remember to give credit where credit's due **
                              and leave Reputation Points for meaningful posts

                              Comment

                              Working...
                              X