Something neat I did was working with command line parameters in VBScript. You can access the command line arguments array through
Wscript.Arguments(i)
where i goes from 0 upwards. So, if you’ve got one argument option you could handle it like this
If Wscript.Arguments.Count = 0 Then
‘ Do Proceed as Normal
Else
If Wscript.Arguments(0) = “/something” Then
RunFunctionA
End If
End If
If you want to have multiple parameters (the script I did had /nolog & /reset), then you’ll want to process all the arguments before you proceed. So, I went through the array and turned on flags so I could act afterwards. If you don’t do this, you might end running something you don’t want or in a way you don’t want. You could process your arguments like this:
If Wscript.Arguments.Count = 0 Then
‘ Do Proceed as Normal
Else
For i = 0 to Wscript.Arguments.Count – 1
If Wscript.Arguments(i) = “/reset” Then
resetflag=1 ’Turn on the reset flag
ElseIf Wscript.Arguments(i) = “/nolog” Then
loggingflag = 0 ‘Turn off the logging flag
End If
Next
End If
If loggingFlag =0 Then
‘Don’t log
Else
LogMyStuff ‘ Call the function you want
End If
‘==========================================================================
‘If reset command line parameter is detected, run the reset function only, then quit
‘==========================================================================
If resetFlag Then
Reset
WScript.Quit
End If
Hi… I notice you were getting the exact same error I am getting when building a certain model of laptop using BDD 2007
—–
Unable to get WinNT ADSI provider: (-2147221020)
ERROR – ZTITatoo state restore task should be running in the full OS, aborting
ZTIERROR – Non-zero return code by ZTITatoo, rc = 1 (this one twice)
—–
Can you tell me how you resolved this? It only happens on 1 particular model of Toshiba that we use (Portege R200) and its got me stumped!
Many Thanks
If it’s only happening on one model, my guess is that it would be to do with drivers. If you’ve got any drivers for this model, I’d try removing them all, then adding them back in gradually. From what I’ve seen, Vista should install ok without additional drivers.
If it’s not a recent model you might have trouble finding suitable Vista drivers, I did for our Dells. If it’s an older model and doesn’t work with or without standard drivers you might need to say “Sorry, we can’t support Vista on this model”.
Ben Hunter’s blog linked from here also has a good entry about logs. The logs are surprisingly useful, so you might be able to get some more info from them.
Hi,
How do you call another .vbs/VBScript from within a .vbs script?
Thanks
Lewis
You need to do a couple of things:
First, create a shell object:
Set objShell = CreateObject(“WScript.Shell”)
Next, create a command:
cmd = “cscript \\path\to\my\other.vbs”
Then, execute the command
return = objShell.Run(cmd,0,True)
Thanks!
BTW, one further newbie question – the path has a folder name that has spaces, e.g.
E:\Program Files\Examples\
how do I code the path so will work?
You need to put quotes around the string something like this:
Quotes = Chr(34)
command = Quotes & “command with spaces” & Quotes
What this does is declare Quotes as a variable using the ascii value of the quotes character. Then you put quotes before and after the string. This also works for strings without spaces.
I have to pass command through Inputbox.
ex:
Path1=inputbox(“Enter command”)
This parameter how can a run thorugh
wscipt
Just wondering
How would I for example be able to create a command line argument that can be changed from input in the cmd window?
If for example I want to create a new user, what would the code look like to allow me to enter a desired username in cmd as opposed to having a preset username in the script already?
It could use some of the code in the post. You could call your script by running something like:
cscript “CreateNewUser.vbs” “Frederick New”
and then within your script anytime you use
WScript.Arguments(0) you will get “Frederick New”
Hope that helps.
That is helping me on the right track
What I intend to do is create a script that will let me specify the input into the commannd line. Essentially, what I want to do is to run the script and specify a username and password in the command line and for the script to obey the arguments in the command line (IE: I type in the command line that I want username to be Roland and password as lemon38 for example and for the script to set those actions when creating the object/user)
In my vbs i need receive parameters and send these parameters to the batch file.
my code:
Set WshShell = CreateObject(“WScript.Shell”)
Set ArgObj = WScript.Arguments
Dim ArgObj, var1, var2
var1 = ArgObj(1)
var2 = ArgObj(2)
WshShell.Run chr(34) & “InstallHide.bat ” & var1 & ” ” & var2
Set WshShell = Nothing
This is not working. When i execute i have an error “subscript out of range”.
What i’m doing wrong ? Someone can help me ?
It’s probably on the line var2 = ArgObj(2)
In VBScript, the arguments start from 0, so you’d probably want
var1 = ArgObj(0)
var2 = ArgObj(1)
You’d probably also need to check your quoting around InstallHide.bat
I put like you said and return an error. Invalid character.
This VBS is called from a .bat file. So this bat faile receive thar arguments, process them and then call this vbs. The line in the .bat file to call the vbs is:
cscript InstallHide.vbs “%1″”%2″”%3″”%4″”%5″”%6″”%7″”%8″”%9″
The vbs only receive the parameters and send them to the InstallHide.bat.
Can you help me ?
Check that the quotes are straight rather than angled quotes perhaps?
I have been using the Command Line since it was created!
I am now experimenting & learning to use VBScript.
This thread has got my attention & I came up with the following:
HOPE IT HELPS!!!!!
BATCH FILE TO RUN EVERYTHING:
ECHO %1 %2 %3 %4 %5 %6 %7 %8 %9
CSCRIPT ARGUMENTS.VBS “%1″ “%2″ “%3″ “%4″ “%5″ “%6″ “%7″ “%8″ “%9″
VBScript:
‘ Set WshShell = CreateObject(“WScript.Shell”)
Set ArgObj = WScript.Arguments
Dim ArgObj, var1, var2, var3, var4, var5, var6, var7, var8, var9
var1 = ArgObj(0)
var2 = ArgObj(1)
var3 = ArgObj(2)
var4 = ArgObj(3)
var5 = ArgObj(4)
var6 = ArgObj(5)
var7 = ArgObj(6)
var8 = ArgObj(7)
var9 = ArgObj(8)
response = var0&var1&var2&var3&var4&var5&var6&var7&var8&var9
MsgBox(“Arguments: “&response)
Set WshShell = Nothing
Not sure why I had to comment out the 1st line to get it to work!
RESULTING OUTPUT IN COMMAND WINDOW:
C:\scripts\TESTS>ARGUMENTS A1 A2 A3 A4 A5 A6 A7 A8 A9
C:\scripts\TESTS>ECHO A1 A2 A3 A4 A5 A6 A7 A8 A9
A1 A2 A3 A4 A5 A6 A7 A8 A9
C:\scripts\TESTS>CSCRIPT ARGUMENTS.VBS “A1″ “A2″ “A3″ “A4″ “A5″ “A6″ “A7″ “A8″ “A9″
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\scripts\TESTS>
The script shows a MsgBox with all the arguments!
[...] it turns out, over 63% of visitors have been to one of just 2 articles (Sweet VBScript – command line parameters and VBScript Current Directory or Folder) I’m puzzled. These are brilliantly useful [...]
is there a way to call in multiple scripts : i.e. if script one isnt there use script two.
Of course, you might like something like
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objShell = CreateObject(“WScript.Shell”)
If objFSO.FileExists(“c:\script1.vbs”) Then
return = objShell.Run(“cscript c:\script1.vbs”,0,True)
ElseIf objFSO.FileExists(“c:\script2.vbs”)
return = objShell.Run(“cscript c:\script1.vbs”,0,True)
End If