Vbscript start vbs with arguments and define multiple arguments - vbscript

I need to start a vbs script by an argument and related to the passed argument see a pop up.
Example :
Dim Arg, var1, var2
Set Arg = WScript.Arguments
'Parameter1, begin with index0
var1 = Arg(0)
if (instr(WScript.Arguments.Name,"Printer")> 0 then
wscript.echo "Printer type..."
end if
if (instr(WScript.Arguments.Name,"help")> 0 then
wscript.echo "help..."
end if
Thanks in advance
'Clear the objects at the end of your script.
set Arg = Nothing

call you script like so
myscript.vbs /help
and access args like so
'setup the named argument collection
set argNamedCollection = WScript.Arguments.Named
'get the arguments passed in
argHelp = argNamedCollection.Item("help")
argPrinter = argNamedCollection.Item("printer")
'or check directly
'check for help arguments
if argNamedCollection.Exists("help") then
'do somthing
end if

Related

VBScript: How to change variable value by calling another script file?

This is how I'm passing arguments between files these days.
1.vbs:
Dim MyVar
'1) Assigning some value to MyVar.
MyVar = "foo"
'2) Passing MyVar to 2.vbs.
CreateObject("WScript.Shell").Run _
Chr(34) & "C:" & "\2.vbs" & _
Chr(34) & " " & Chr(34) & MyVar & Chr(34), _
1, True
'4) End point.
MsgBox MyVar
2.vbs:
If _
WScript.Arguments.Count > 0 _
Then
Call MySub( _
WScript.Arguments(0))
End If
Sub MySub(MyVar)
'3) Doing some work with MyVar.
MyVar = "bar"
End Sub
So, if all code was in 1 single file, and
if I had to use Call MySub instead of CreateObject("WScript.Shell").Run
— i'd successfully changed MyVar from "foo" to "bar",
and got the latter on MsgBox.
Yet, sometimes I really want to work with MyVar in another file,
and be able to get it back (to the already running first file) with changes.
I just don't know how to do that properly.
Option 1:
keep the files as they are, and add an echo at the end of your sub:
2.vbs:
'(...)
'3) Doing some work with MyVar.
MyVar = "bar"
Wscript.Echo(MyVar)
'(...)
1.vbs
Read the output by changing the method ".Run" to ".Exec"
'(...)
'2) Passing MyVar to 2.vbs.
set oShell = CreateObject("Wscript.Shell")
set Exe = oShell.Exec("cmd /c 2.vbs """&MyVar&""" ")
MyVar = Exe.StdOut.ReadAll
'(...)
'4) End point.
MsgBox MyVar
Option 2:
Using the "2.vbs" file as a function library and changing the sub to function:
1.vbs
'Put the import statements at the top of 1.vbs code for easier readability.
Import "2.vbs"
'(...)
MyVar = MyFunction(MyVar)
2.vbs
Function MyFunction(MyVar)
'3) Doing some work with MyVar.
MyFunction = "bar"
end Function

How can I read all arguments with the VBS-SAPI-VOICE?

I want to create a program, that automaticly reads all
start-arguments. This is what I end with after some hours
of researching and debugging but it still not working.
(I'm not very experienced in VBScript.)
The Error Message is something like "Instruction expected" or "statement expected"
I dont know whats the right translation.(I use the German Version. The original Errormessage is: "Anweisung erwartet")
Private Sub Say()
Set VOICE = createobject("sapi.spvoice")
Set Args = WScript.Arguments
Count = 0
While(count > Args.Count)
VOICE.speak(WScript.Arguments(count))
count = count + 1
End While
End Sub
Say()
Have someone an idea?
I hope you understood what I mean. (My English is awful)
Refer to this you can write something like that :
Set VOICE = createobject("sapi.spvoice")
' Store the arguments in a variable:
Set objArgs = Wscript.Arguments
If objArgs.Count = 0 Then
Wscript.Echo "Missing parameters"
VOICE.Speak "Missing parameters"
End If
' Display all command-line arguments
For Each strArg in objArgs
WScript.Echo strArg
VOICE.Speak strArg
Next

Test if VBScript named argument exists

I want to test if a specific named argument has been provided before validating that argument, so I can provide meaningful error codes for missing and invalid conditions.
I have this now
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named
If colArgs.Item("Script") Then
If not objFSO.FileExists(colArgs.Item("Script")) Then
intReturn = 1805
End If
Else
intReturn = 1639
End If
If Not intReturn Then
msgBox colArgs.Item("Script"), 0, "Script"
Else
msgBox intReturn, 0, "Error"
End If
And my expectation would be that if I don't provide an argument called Script at all, I would get the Error msgBox with the 1639 value. Instead I get the good msgBox, with a blank for Script.
I also tried
If Not colArgs.Item("Script") = "" Then
EDIT: Per #Tomalak, I now have this
Option Explicit
Dim objShell, objFSO, colArgs, intReturn
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named
If Not IsEmpty(colArgs.Item("Script")) Then
If Not objFSO.FileExists(colArgs.Item("Script")) Then
intReturn = 1805
End If
Else
intReturn = 1639
End If
If IsEmpty(intReturn) Then
msgBox colArgs.Item("Script"), 0, "Script"
Else
msgBox intReturn, 0, "Error"
End If
And for what it is worth, I am calling the VBScript from PowerShell like this
$script = "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\Helper Target.ps1"
$arguments ="`"\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\PxHelper.vbs`" //nologo /script:`"$script`" /wait:1"
Start-Process -filePath:Wscript.exe -argumentList:$arguments
And now I get the error condition even when the script IS provided. Grrr, Mondays.
If a named argument is not given on the command line
WScript.Arguments.Named.Exists("argname") will return False
WScript.Arguments.Named("argname") will return an empty value
If a named argument is given but not assigned a value on the command line (/argname)
WScript.Arguments.Named.Exists("argname") will return True
WScript.Arguments.Named("argname") will return an empty value
If a named argument is given with an empty string on the command line (/argname:)
WScript.Arguments.Named.Exists("argname") will return True
WScript.Arguments.Named("argname") will return an empty string
If a named argument is given with a value on the command line (/argname:value)
WScript.Arguments.Named.Exists("argname") will return True
WScript.Arguments.Named("argname") will return a string with that value
Empty values are different from empty strings: They are uninitialized, whereas the empty string is a is regular string of zero length.
You can check for empty values with the IsEmpty() function.
If Not WScript.Arguments.Named.Exists("foo") Then
' show message / end script / use default
End If
If IsEmpty(WScript.Arguments.Named("foo")) Then
' show message / end script / use default
End If

Writing an output of shellExecute command with arguments into a log file in VBScript

I've tried a lot of things to accomplish this, but haven't succeeded yet in the way I'd like to.
I've got an hta app, that gathers parameters from checkboxes and then runs a cmd file passing those parameters there.
I want to create a log file of that process without creating any new wrapping files if a log checkbox is checked.
My logic is to run a script with arguments by another with redirecting parameter as an argument. And I can't get the syntax right (or is it even possible inside the same file). My simplified code:
Sub RunCmd
dim shell
dim shellWithLog
dim command
dim ARGS
set shell = createobject("Shell.Application")
set shellWithLog = createobject("wscript.shell")
command = "gui.cmd"
if (checkbox1.checked) then
ARGS = ARGS + " Do_This"
end if
if (checkbox2.checked) then
ARGS = ARGS + " Do_That"
end if
if (logFile.checked) then
shellWithLog.run (shell.shellExecute command, ARGS), " &>" + "publishLog.log", 1
else
shell.shellExecute command, , "runas", 1
end if
End Sub
This doesn't work, obviously, but at least shows what I'm trying to achieve.
To concatenate strings in VBScript, the & operator is used. The + operator is for numerical addition only.
Sub RunCmd
dim shell, ARGS
set shell = createobject("Shell.Application")
ARGS = ""
if checkbox1.checked then ARGS = ARGS & " Do_This"
if checkbox2.checked then ARGS = ARGS & " Do_That"
if logFile.checked then ARGS = ARGS & ARGS " > publishLog.log"
' ShellExecute(sFile, [vArgs], [vDirectory], [vOperation], [vShow])
' https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
shell.shellExecute "gui.cmd", ARGS, , "runas", 1
End Sub

VBScript how to join WScript.Arguments?

I am trying to join the arguments to a string to be passed to another script. The following:
WScript.Echo(Join(WScript.Arguments))
gives me an error:
Error: Wrong number of arguments or invalid property assignment
Code: 800A01C2
What is wrong with that syntax?
WshArgument objects are not arrays, so you can't use Join() on them. What you can do is something like this:
ReDim arr(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
arr(i) = WScript.Arguments(i)
Next
WScript.Echo Join(arr)
Another solution can be done with ArrayList object from the system:
Set oAL = CreateObject("System.Collections.ArrayList")
For Each oItem In Wscript.Arguments: oAL.Add oItem: Next
WScript.Echo Join(oAL.ToArray, " ")
ReDim arr(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
arr(i) = """"+WScript.Arguments(i)+""""
Next
WScript.Echo Join(arr)
this will add quotes for each argument,
you can then remove it in the batch file with %~1 and so on.
Here is the function that I use. It will return all the arguments whether they contain quotes or not in a string that you can pass to another script.
Function GetArguments()
Dim Args, Arg
If WSH.Arguments.Count > 0 Then
For Each Arg In WSH.Arguments
Args = Args & """" & Arg & """ "
Next
Args = " """"" & Trim(Args) & """"""
End If
GetArguments = Args
End Function

Resources