Announcement

Collapse
No announcement yet.

Converting custom batch file to vbscript

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

  • Converting custom batch file to vbscript

    Hello to all,
    This is my first post here ever, I am Alex, I work in a AD domain network environment. I am a Systems Administrator with admin rights across the network. I will be deploying this application to Windows XP SP2 computers. I am trying to remotely deploy an msi's application using a vbscript but so far i had no luck. This application is open-source, the makers of this application have come out with a batch file to deploy the application's msi but I think is a very limited way to roll this msi.

    My first part of this post is to ask if there is a way to convert this batch installation file into a vbscript. I have been using psexec commands to deploy the application but my installation process is a little bit complicated recently I was asked to instruct my other team mates how to do the installations, I want to simplify their work. The suggested installation is the following.
    ..(Moderator edit: added code tags around both scripts)
    Code:
    @echo on
    set ARGS=
    set ARGS=%ARGS% NEWPOOL="N"
    set ARGS=%ARGS% POOLNAME="NEW_POOL"
    set ARGS=%ARGS% RUNJOBS="Y"
    set ARGS=%ARGS% VACATEJOBS="Y"
    set ARGS=%ARGS% SUBMITJOBS="Y"
    set ARGS=%ARGS% [email protected]
    set ARGS=%ARGS% SMTPSERVER="mailserver.domain.com"
    set ARGS=%ARGS% HOSTALLOWREAD="*"
    set ARGS=%ARGS% HOSTALLOWWRITE="*.domain.com"
    set ARGS=%ARGS% HOSTALLOWADMINISTATOR="$(FULL_HOSTNAME)"
    set ARGS=%ARGS% INSTALLDIR="C:\Condor"
    set ARGS=%ARGS% INSTALLDIR_NTS="C:\Condor"
    set ARGS=%ARGS% POOLHOSTNAME="servername.domain.com"
    set ARGS=%ARGS% ACCOUNTINGDOMAIN="domain.com"
    set ARGS=%ARGS% JVMLOCATION="C:\Windows\system32\java.exe"
    set ARGS=%ARGS% USEVMUNIVERSE=N
    set ARGS=%ARGS% VMVERSION="server1.4"
    set ARGS=%ARGS% VMMEMORY="128"
    set ARGS=%ARGS% VMMAXNUMBER="$(NUM_CPUS)"
    set ARGS=%ARGS% VMNETWORKING=N
    set ARGS=%ARGS% STARTSERVICE="Y"
     
    msiexec /qb /l* c:\filename-install-log.txt /i %systemdrive%\\Installation\Condor-7.2.x-winnt50-x86.msi %ARGS%
    ---------------------------------------------------------------------------------------------
    The second part of this post is to show my intents of putting the installation process in a vbscript so far I created the below script, which I have used to push the application and it works but only locally and my untimate goal is to run the vbscript remotely. I don't know if I should break the post into two but I read in DanielP's recommendations not to duplicate efforts. I have tried to modify the vbscript to point to remote computers. The bold italic parts are my failed intent to perform remote installation. If you see any thing in this script that is wrong please feel free to correct it or suggest a better way to rewrite the script.

    The local installation vb script looks like this:
    Code:
    On Error Resume Next
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("WScript.Shell")
    Set objWSHShell = Wscript.CreateObject("Wscript.Shell")
    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set WshEnv = WshShell.Environment("SYSTEM") 
    Set objNetwork = CreateObject("Wscript.Network")
    'strComputer = *.*
     
    strComputer = InputBox("Enter the name of the computer:") 
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     
    Const EVENT_SUCCESS = 0
    const HKEY_LOCAL_MACHINE = &H80000002
    Const ADS_SECURE_AUTHENTICATION = 1
    Const OverwriteExisting = True
    Drive = objShell.ExpandEnvironmentStrings("%SYSTEMDRIVE%")
    BuildPath = "\\SHARENAME\Software_DP\"
    Domain = "DOMAIN"
     
    '*****************************************************************************************
    ' Creates a Condor Installation directory and copy all installation files
    '*****************************************************************************************
    objFSo.CreateFolder(Drive & "\Condor")
    objFSO.CreateFolder(Drive & "\Condor\Installation")
    objFSO.CopyFile BuildPath & "Condor\Condor_7_2_1\*.*", Drive & "\Condor\Installation", OverwriteExisting
    objFSO.CreateFolder(Drive & "\Condor\Installation\Registering_condor_credd_accounts")
    objFSO.CopyFile BuildPath & "Condor\Condor_7_2_1\Registering_condor_credd_accounts\*.*", Drive & "\Condor\Installation\Registering_condor_credd_accounts", OverwriteExisting
    objShell.LogEvent EVENT_SUCCESS, "Condor Installation directory created & all installation files were copied successfully"
    '*****************************************************************************************
    ' Calls the Condor version 7.2.1 Installation Batch file
    '*****************************************************************************************
    wscript.sleep 10000
    objShell.Run("%Comspec% /K %systemdrive%\Condor\Installation\PROD_Install_condor_batch_7_2_1.bat"), 1, True
    objShell.LogEvent EVENT_SUCCESS, "Condor Software is installed" 
     
    '*****************************************************************************************
    ' Copying other important configuration files to Condor Root Directory 
    '*****************************************************************************************
    'wscript.sleep 300000
    objFSO.CopyFile Drive & "Condor\Installation\condor_config.local.credd" , Drive & "\Condor\", OverwriteExisting
    objShell.LogEvent EVENT_SUCCESS, "Condor_config.local.credd file is in condor root directoy" 
    objFSO.CopyFile Drive & "Condor\Installation\Cycle_Agent\condor_config.local.history" , Drive & "\Condor\", OverwriteExisting
    objShell.LogEvent EVENT_SUCCESS, "Condor_config.local.history file is in condor root directoy" 
    objFSO.CopyFile Drive & "Condor\Installation\Cycle_Agent\cycle_agent.exe" , Drive & "\Condor\bin", OverwriteExisting
    objShell.LogEvent EVENT_SUCCESS, "Cycle_agent executable is copied to Condor's Bin directory"
    '*****************************************************************************************
    ' Adding Condor\Bin to the Environment Variables Path
    '*****************************************************************************************
    WshEnv("Path") = WshEnv("Path") & ";C:\Condor\bin"
    objShell.LogEvent EVENT_SUCCESS, "Condor\bin path had been added to Environment variables" 
     
    '*****************************************************************************************
    ' 
    '*****************************************************************************************
    Any help is much appreciated, if you need more information from me please don't hesitate to ask for it.

    Alex
    0
    Try to put the whole installation process in one gigantic VB script
    0%
    0
    Breakdown the installation process into several VB script
    0%
    0

    The poll is expired.


  • #2
    Re: Converting custom batch file to vbscript

    Thanks Moderator for adding those tags

    Comment


    • #3
      Re: Converting custom batch file to vbscript

      Here is the bat as VBScript. The idea is quite simple. Allocate an array for all the parameters. Then join the cells to get the parameter string. Create a shell object and use it to execute Msiexec.

      A fancier version would use two-dimensional array; one column for keys and another for the values, but being friday evening, I have no time for writing one.

      Code:
      option explicit
      
      dim arg, objShell, sysdrive, msiexec
      dim args(20)
      
      args(0) = "NEWPOOL=""N"""
      args(1) = "POOLNAME=""NEW_POOL"""
      args(2) = "RUNJOBS=""Y"""
      args(3) = "VACATEJOBS=""Y"""
      args(4) = "SUBMITJOBS=""Y"""
      args(5) = "[email protected]"
      args(6) = "SMTPSERVER=""mailserver.domain.com"""
      args(7) = "HOSTALLOWREAD=""*"""
      args(8) = "HOSTALLOWWRITE=""*.domain.com"""
      args(9) = "HOSTALLOWADMINISTATOR=""$(FULL_HOSTNAME)"""
      args(10) = "INSTALLDIR=""C:\Condor"""
      args(11) = "INSTALLDIR_NTS=""C:\Condor"""
      args(12) = "POOLHOSTNAME=""servername.domain.com"""
      args(13) = "ACCOUNTINGDOMAIN=""domain.com"""
      args(14) = "JVMLOCATION=""C:\Windows\system32\java.exe"""
      args(15) = "USEVMUNIVERSE=N"
      args(16) = "VMVERSION=""server1.4"""
      args(17) = "VMMEMORY=""128"""
      args(18) = "VMMAXNUMBER=""$(NUM_CPUS)"""
      args(19) = "VMNETWORKING=N"
      args(20) = "STARTSERVICE=""Y"""
      
      arg = join(args, " ")
      
      Set objShell = CreateObject("WScript.Shell")
      sysdrive = objShell.ExpandEnvironmentStrings("%systemdrive%")
      
      msiexec = "msiexec /qb /l* c:\filename-install-log.txt /i " 
      msiexec = msiexec & sysdrive &
      msiexec = msiexec  & "\\Installation\Condor-7.2.x-winnt50-x86.msi "
      
      objShell.Run( msiexec & arg )
      As for the latter script, what exactly is your problem? You aren't actually using the WMI object for anything.

      -vP

      Comment


      • #4
        Re: Converting custom batch file to vbscript

        VonPryz,
        Thank you very much for taking your time to reply and answering my plead of help. I will definitely try your suggestion tomorrow when I go back to work. Concerning the latter script, and your comment
        "You aren't actually using the WMI object for anything"
        that is what I don't know how to do? The script runs fine locally when I run it in any machine but what I was trying to accomplish is to run it against a remote computer located within my secured perimeter environment. Unfortunately I failed trying to do that, that is why I added the following lines of code:
        Code:
         
        strComputer = InputBox("Enter the name of the computer:") 
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        But it doesn't do anything but run the script on my computer instead of the remote one. How can I accomplish to accommodate the current vbscript to run on remote computers?
        Again, thank you very much for your input and your time,
        Sincerely,
        Alex
        Last edited by Alexito; 28th April 2009, 01:37.

        Comment


        • #5
          Re: Converting custom batch file to vbscript

          Originally posted by Alexito View Post
          How can I accomplish to accommodate the current vbscript to run on remote computers?
          What you need is two scripts: a deployment script and a deployed script. The latter is the one that actually does all the install stuff like copying files and whatnot. This is pretty much the script you already have created. The former is used to put the deployed script onto the remote computer and to call it. Take a look on this Technet article, it'll discuss remote scripts in more a detail.

          The deployed script should be copied on a remote machine. A Scripting.FileSystemObject could be used for that part.

          If you study this Scripting Guy article, you are likely to find a way for executing a VBScript on a remote machine too. Consider this:

          Code:
          Set objWMIService = GetObject _
              ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
          errReturn = objWMIService.Create _
              ("cmd.exe /c md c:\newfolder", Null, Null, intProcessID)
          I wonder what would happen if one replaces the "cmd.exe /..." part with a "cscript deploymentscript.vbs" statement.

          -vP

          Comment


          • #6
            Re: Converting custom batch file to vbscript

            You can also use a tool such as psexec to run the script remotely.

            Code:
            psexec.exe \\strComputer -u domain\username -p password -e cscript.exe //B C:\Install.vbs
            Last edited by ekrengel; 28th April 2009, 21:51.

            Comment


            • #7
              Re: Converting custom batch file to vbscript

              You could also create a scheduled job on the remote machine...I was working on something similar a little while ago. It will:

              - ask for a computer name
              - ask for your password (which will be hidden with an IE window password function)
              - setup the scheduled task with the msiexec commands (I just pasted in vonPryz's part here)
              - check if the job has ran, if not it will wait 10 second and try again. when the job is gone off the remote machine you will be notified with a msgbox, signifying that the install has started

              Code:
              dim args(20)
              
              Set fso = CreateObject("Scripting.FileSystemObject")
              Set wshShell = CreateObject("WScript.Shell")
              
              strComputer = Inputbox("Enter Computername:","MsiExec Install")
              	If strComputer = "" Then WScript.quit
              On Error GoTo 0
              
              strJobfile = "\\"& strComputer & "\admin$\tasks\Install.job"
              
              ' if the job already exists, delete it
              If fso.FileExists(strJobFile) Then fso.DeleteFile strJobFile, True
              
              If (Err.Number <> 0) Then 
              	MsgBox "Failed to delete old job " & Err.Description, vbcritical + vbokonly,strComputer
              	WScript.Quit
              End If
              
              args(0) = "NEWPOOL=""N"""
              args(1) = "POOLNAME=""NEW_POOL"""
              args(2) = "RUNJOBS=""Y"""
              args(3) = "VACATEJOBS=""Y"""
              args(4) = "SUBMITJOBS=""Y"""
              args(5) = "[email protected]"
              args(6) = "SMTPSERVER=""mailserver.domain.com"""
              args(7) = "HOSTALLOWREAD=""*"""
              args(8) = "HOSTALLOWWRITE=""*.domain.com"""
              args(9) = "HOSTALLOWADMINISTATOR=""$(FULL_HOSTNAME)"""
              args(10) = "INSTALLDIR=""C:\Condor"""
              args(11) = "INSTALLDIR_NTS=""C:\Condor"""
              args(12) = "POOLHOSTNAME=""servername.domain.com"""
              args(13) = "ACCOUNTINGDOMAIN=""domain.com"""
              args(14) = "JVMLOCATION=""C:\Windows\system32\java.exe"""
              args(15) = "USEVMUNIVERSE=N"
              args(16) = "VMVERSION=""server1.4"""
              args(17) = "VMMEMORY=""128"""
              args(18) = "VMMAXNUMBER=""$(NUM_CPUS)"""
              args(19) = "VMNETWORKING=N"
              args(20) = "STARTSERVICE=""Y"""
              
              arg = join(args, " ")
              
              msiexec = "%SystemRoot%\system32\msiexec.exe /qb /l* c:\filename-install-log.txt /i %systemdrive% "
              msiexec = msiexec & "\\Installation\Condor-7.2.x-winnt50-x86.msi"
              
              	RemoteDateTime()
              
              ' Add minutes
              strNewTime = DateAdd("n", 1, time)
              
              ' setup the scheduled task on the remote machine
              strUserName  = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%\%USERNAME%")
              strpassword = GetPassword("Please enter your password to create the scheduled task:")
              strExe = "\\" & strComputer & msiexec & arg
              strNewTime = cstr(FormatDateTime(strNewTime, vbShortTime))& ":00"
              strCommand = "SCHTASKS /s " & strComputer & " /Create /SC once /TN Install /TR " & chr(34) & strExe & chr(34) &_
              	" /ST "& strNewTime & " /RU " & chr(34) & strUserName & chr(34) & " /RP " & chr(34) & strpassword & chr(34)
              
              ' run the scheduled task
              retval = WshShell.Run(strCommand, 0, True)
              
                If retval = 0 Then
              	MsgBox "The script was successfully created to start at " & strNewTime
                Else
              	strMessage ="There were problems creating script task. " & Err.Description
              	MsgBox strMessage, vbCritical + vbOKOnly, "Fatal Error"
                      WScript.Quit
                End If
              
              ' check if task has completed, the sleep 10 seconds and try again
              Do While fso.FileExists(strJobFile)
              	WScript.Sleep 10000
              Loop
              
              MsgBox "Install has started!"
              WScript.Quit(0)
              
              
              Sub RemoteDateTime()
              	On Error Resume Next
              	Dim objWMI, colItems, objItem
              	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
              		If (Err.Number <> 0) Then
              			MsgBox "WMI Failure " & err.Description, vbcritical + vbokonly,strComputer
              		Exit Sub
              	End If 
              	Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_Localtime",,48)
              	For Each objItem In colItems
              		strRmtDate = objItem.Year & "-" & padNum(objItem.Month) & "-" & padnum(objItem.Day)
              		strRmtTime = objItem.Hour & ":" & padNum(objItem.Minute) & ":" & padnum(objItem.Second)
              	Next
              End Sub
              
              Function GetPassword(myPrompt)
                  	Dim objIE
                  	Set objIE = CreateObject( "InternetExplorer.Application" )
                  	objIE.Navigate "about:blank"
                  	objIE.Document.Title = "Password"
                  	objIE.ToolBar        = False
                  	objIE.Resizable      = False
                  	objIE.StatusBar      = False
                  	objIE.Width          = 300
                  	objIE.Height         = 180
              
                  	With objIE.Document.ParentWindow.Screen
                      	objIE.Left = (.AvailWidth  - objIE.Width ) \ 2
                      	objIE.Top  = (.Availheight - objIE.Height) \ 2
                  	End With
                
                  	objIE.Document.Body.InnerHTML = "<DIV align='center'><P>" & myPrompt _
                                                & "</P>" & vbCrLf _
                                                & "<P><INPUT TYPE='password' SIZE= '20'" _
                                                & "ID='Password'></P>" & vbCrLf _
                                                & "<P><INPUT TYPE='hidden' ID='OK'" _
                                                & "NAME='OK' VALUE='0'>" _
                                                & "<INPUT TYPE='submit' VALUE='OK'" _
                                                & "OnClick='VBScript:OK.Value=1'></P></DIV>"
                  	objIE.Visible = True
              
                  	Do While objIE.Document.All.OK.Value = 0
                      	WScript.Sleep 200
                  	Loop
              
                  	GetPassword = objIE.Document.All.Password.Value
              
                  	objIE.Quit
                  	Set objIE = Nothing
              End Function
              Last edited by ekrengel; 28th April 2009, 21:59. Reason: Path of msiexec.exe

              Comment


              • #8
                Re: Converting custom batch file to vbscript

                Thank you all for your collaboration VonPryz and Ekrengel,
                I was able to run VonPryz's code local on a computer without any issue, I just adjusted to my environment and it worked like a gem but Ekrengel's code I could not make it run. Every time I run the script I got the following error message
                Fatal Error:
                There were problems creating script task. Type mismatch...
                I know my password is correct but I did try it with a different admin account and had the same result.
                Any ideas? What could I be doing wrong?
                Thanks in advance for your help,
                Alex

                Comment

                Working...
                X