Search code examples
vbscriptsilent

Need help in this visual basic script: launching a program in silent mode


I'm trying to launch a program in silent mode, to install a certain application. The command line for launching the installation in silent mode is as follows:

setup.exe /s /v" /q"

I tried using the following: strCmd="C:\setup.exe"" /s /v"""" /q"""

But obviously this doesn't work. Can anyone help me on writing the right syntax. I know there's something wrong with the quotes as an escape character. I have wasted a lot of time trying to figure that out. Any help is highly appreciated.


Solution

  • If you want to run the command using different credentials, you can use the RunAs command. Things can get tricky when embedding your command within the RunAs command, however. These steps may help:

    1. Determine the proper command line syntax. Test this directly at the command prompt to ensure it's correct.

      c:\setup.exe /s /v"/qn"
      
    2. Embed your command within the RunAs command. Your entire command needs to be surrounded with quotes. In addition, any quotes within your command need to be escaped with \. Again, test this directly at the command prompt to ensure it's correct.

      runas.exe /user:DOMAIN\USER "c:\setup.exe /s /v\"/qn\""
      
    3. Convert the entire command string to VBScript. Wherever you see a quote, double it, or replace it with Chr(34). Here is how it would look when the quotes are doubled:

      runas.exe /user:DOMAIN\USER ""c:\setup.exe /s /v\""/qn\""""
      
    4. Assign it to a VBScript variable. We just need to put additional quotes around the entire command:

      strCommand = "runas.exe /user:DOMAIN\USER ""c:\setup.exe /s /v\""/qn\"""""
      
    5. Now you're ready to go. You can run this directly with Shell.Run:

      With CreateObject("WScript.Shell")
          .Run strCommand
      End With
      

    Note that you will still need to use SendKeys when RunAs prompts you for the password, as it provides no way to pass it on the command line.

    An alternative method is to use SCHTASKS to schedule a one-time task to be run immediately. With SCHTASKS, you can pass the complete credentials on the command line.