How can I pass and access command line arguments in VBscript?
Set args = Wscript.Arguments
For Each arg In args
Wscript.Echo arg
Next
From a command prompt, run the script like this:
CSCRIPT MyScript.vbs 1 2 A B "Arg with spaces"
Will give results like this:
1
2
A
B
Arg with spaces
If you need direct access:
WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...
Related
I am calling a cmd like:
perl ScriptGen.pl Target.c %PreProcessorPath%\preproc.exe Arg1 Arg2 > clean.c
And I get errors which mean Arg1 and Arg2 are not correctly passed to the pre processor.
I would like to call this perl script like this by having the target file, and preprocessor path itself as arguments to the perl script, and Arg1 Arg2 are arguments to the perl script which the preproc understands.
EDIT: There is a
my $cmd = shift #ARGV
at the begginning of the script to parse the arguments, and then the script works with that.
This is my Suggestion:
You can use the batchfile instead of using in a single line several parameters with applications;
Create one batchfile - scriptgen.bat
SETPATH=%PATH%;%PreProcessorPath%\preproc.exe
call preproc.exe %1 %2
rem %1 = Arg1 %2 = Arg1 %3 = Target.c
perl -w ScriptGen.pl %3 %1 %2 > clean.c
In MS-dos Prompt run:
scriptgen.bat Arg1 Arg2 Arg3
I have a script foo.cmd:
echo %1 %2
In PowerShell I run:
foo.cmd "a,b" "c"
Expected output: a,b c
Actual output: a b
Why?
The double quotes are removed after PowerShell parsed the command line and passes it to CMD for execution, so CMD actually sees a statement
foo.cmd a,b c
Since the comma is one of CMD's delimiter characters that statement is equivalent to
foo.cmd a b c
To avoid this behavior you need to ensure that the double quotes are preserved when passing the arguments to CMD. There are several ways to achieve this. For instance, you could put the double quoted arguments in single qoutes:
foo.cmd '"a,b"' "c"
and change the positional parameters in the batch script to %~1, %~2 so that CMD removes the double quotes from the arguments.
If you have PowerShell v3 or newer you can use the "magic parameter" --% to avoid the nested quotes:
foo.cmd --% "a,b" "c"
You still need %~1 and %~2 in the batch script, though.
What are the rules of string expansion in command mode? If on cmd.exe, I would write this:
c:\asdoc.exe -doc-sources+=src
I need to convert this to a string where the actual source path ("src") is computed so somewhere above this line, $sourcePath = "src" is executed. Now I need to transform that cmd.exe command to PowerShell command. I have tried the following but it doesn't work:
& c:\asdoc.exe -doc-sources+=$sourcePath # does NOT work
& c:\asdoc.exe -doc-sources+="$sourcePath" # does NOT work
& c:\asdoc.exe -doc-sources+=($sourcePath) # does NOT work
I used the EchoArgs utility and it gives me these results:
Arg 0 is <-doc-sources+=$sourcePath> # case 1
Arg 0 is <-doc-sources+=$sourcePath> # case 2
Arg 0 is <-doc-sources+=> # case 3
Arg 1 is <src path>
How do I make the string expand "correctly" in this example?
If i understand you properly you want everything expanded and as a single argument. Try to "collect" the argument with quotes.
PS > $sourcepath = "src"
PS > & EchoArgs.exe "-doc-sources+=$($sourcePath)"
Arg 0 is <-doc-sources+=src>
So try this:
& c:\asdoc.exe "-doc-sources+=$($sourcePath)"
The example below will also works AS LONG as you want to expand a variable an not a property inside a variable (ex $myobject.sourcepath)
& c:\asdoc.exe "-doc-sources+=$sourcePath"
I'm trying to use powershell to do a "get" from srcSafe using a label that contains spaces.
I've read what seems like numerous posts about how to pass params w/spaces to exe's but nothing I've tried works. My problem appears to be supplying the label correctly.
Following is the cmd line version ( which works ).
ss get $/sandbox/TestSSCmdLine/* -R -I-N -VL"label space"
My simplest powershell version is:
ss get '$/sandbox/TestSSCmdLine/*' -R -I-N '-VL\"label space\"'
When I run the powershell cmd I get no files and $lastexitcode is "100".
Echo args shows what I think should be correct.
Arg 0 is <get>
Arg 1 is <$/sandbox/TestSSCmdLine/*>
Arg 2 is <-R>
Arg 3 is <-I-N>
Arg 4 is <-VL"label space">
Powershell ISE shows the same.
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Raw argument string: get $/sandbox/TestSSCmdLine/* -R -I-N "-VL\"label space\""
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 0: get
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 1: $/sandbox/TestSSCmdLine/*
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 2: -R
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 3: -I-N
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 4: -VL"label space"
Just to confuse things start-process seems to work:
$cmd = "ss.exe"
$args = "get", '$/sandbox/TestSSCmdLine/*', "-R", "-I-N", '-VL"label space"'
$proc = start-process $cmd $args -Wait -NoNewWindow -PassThru -WorkingDir $pwd
$proc.ExitCode
An additional confusing item is the fact the echo args now shows the version parameter as:
Arg 4 is <-VLlabel space> -> note no spaces, also does not work from cmd line.
Thanx for any help!
John A.
In cmd, the quotes would have been used to ensure label space was passed as a part of the -VL argument. Since the Start-Process version works with a result argument of -VLlabel space, I would try calling ss with '-VLlabel space', without any embedded quotes (third option listed at the top of this answer).
I am trying to do the following - for each *.sql file in the current directory run
sqlplus username/password#connect_identifier_specified_in_argument #file_name
Here is what I have so far:
$scripts = dir *.sql
foreach($script in $scripts) {
Write-Host sqlplus username/password"#"$args "#"$script.Name
}
(I know Write-Host outputs it to the screen; I'm just trying to debug for now.)
However, there is something funky with how PowerShell treats the # character and when I run this I always get something like:
PS C:\code\scripts> C:\utils\run_sql_scripts_on.ps1 identifier
sqlplus username/password#identifier # ALERTS.sql
See that space after the "#"? What gives?
Escape the # with a backtick (`).
Write-Host sqlplus username/password`#$args `#$script.Name
PowerShell Community Extensions has a handy little utility (echoargs) for debugging this sort of problem:
5>echoargs username/password"#"$args "#"$script.Name
Arg 0 is <username/password#>
Arg 1 is <#>
Arg 2 is <test.txt>
Try escaping with a backtick:
6>echoargs "username/password`#$args" "`#$($script.Name)"
Arg 0 is <username/password#>
Arg 1 is <#test.txt>