vbscript error on run command with long argument - vbscript

I need execute a script on vbs
I have no problem run the program.exe with this script
objShell.Run("%SystemDrive%\temp\program.exe")
However the program can support the arguement for silent installation
I run the following command manually on cmd is
%SystemDrive%\temp\program.exe /s /v"MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG="CFG_GRP" ICG="ICG_GRP" REBOOT=Force /qn""
so I put this command on the script but get exception error ')'
objShell.Run("%SystemDrive%\temp\program.exe /s /v"MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG="CFG_GRP" ICG="ICG_GRP" REBOOT=Force /qn""")
any idea?

Try this
Command = "cmd /c %SystemDrive%\temp\program.exe /s /v""MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG=""CFG_GRP"" ICG=""ICG_GRP"" REBOOT=Force /qn"""""
objShell.Run Command
or
objShell.Run "cmd /c %SystemDrive%\temp\program.exe /s /v""MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG=""CFG_GRP"" ICG=""ICG_GRP"" REBOOT=Force /qn"""""
Either way will work the issue is the incorrectly formed string.
Because quotes " denote the start and end of a string when used inside a string they have to be escaped by doubling them "", otherwise VBScript will think it's the end of the string and throw a syntax error.

Related

Execute cmd.exe command from Powershell and pass parameters

I'm creating PS script to remove a bunch of applications from lots of devices.
The theory with the code below being it passes the path of an application to cmd.exe along with the "RD" (remove directory) command and two switches.
However all i get when running this is..
"'rd C:\Program Files\Mozilla Firefox /s /q' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the
path is correct and try again.
Any suggestions please as to how accomplish my goal here
if ($app_path) {
write-host "Now Removing $appname ,install directory ($app_path)"
$command = "rd $app_path /s /q"
& cmd.exe /c $command
}
The arguments need to be separate strings.
& cmd /c rd $app_path /s /q
or
$command = 'rd',$app_path,'/s','/q'
& cmd /c $command
or
cmd /c rd $app_path /s /q

cmd /c and the & symbol in file path

I ran into a problem, where I have a batch file located in a directory that has the and symbol in it.
When I try to run the cmd /c on the batch file the and symbol messes up the cmd program even though the path is double quoted.
C:\>cmd /c "C:\This & That\batch.bat"
'C:\This' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
I'm not sure what to do. I tried escaping the & , using ^& but that doesn't seem to work as I'm all-ready in double quotes.
If I run the same command line from a directory that does not have an & symbol it works fine.
C:\>cmd /c "C:\This and That\batch.bat"
C:\>echo "Hi There"
"Hi There"
Thanks for any help.
so this would be the best way.
You also need to escape the & with ^ and run double double quotes.
cmd /c ""C:\This ^& That\batch.bat""
or you can set a location variable and use && to run the set and cmd /c after each other.
set "location=C:\This ^& That" && cmd /c ""%location%\batch.bat""

Echo Code into a File using VBScript

I am writing a VBScript which I want to use to echo the code of another VBScript into an output file.
However, I am unable to write some of the characters to the output file using this method.
If I use the command line method:
cmd.exe /c "#echo "hello"">output.vbs
This works and the string: "hello" is written to the output file.
However, when I do the same using a VBScript, it does not work.
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
So, is there a way I can echo it into another file retaining the double quotes?
Thanks.
Your quoting is wrong.
Change this:
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
into this:
objShell.Run "%comspec% /c #echo ""hello"">output.vbs"

How to run a batch command in Windows as a different user without typing the password?

I am trying to run a simple windows batch command as a one-liner on a windows command prompt, without having to specify the password. The user is named 'alex', so I am trying:
>cmd /C echo PASSWD | runas /profile /user:alex "cmd /c dir"
but I get the following error:
Enter the password for alex:
Attempting to start cmd /c dir as user "ALEX-NEU\alex" ..
RUNAS ERROR: Unable to run - cmd /c dir
1326: Logon failure: unknown user name or bad password.
where ALEX_NEU is the machine name. The user name and the provided password are correct - so why do I get this error? How to do it correctly?
The only way to supply the password to runas at runtime is with a script, and not terribly gracefully either. It's ugly, it's hackish, and it's asking for security troubles. Having said that, I've done something similar in the past with WScript.Shell's .run and .SendKeys methods like this:
#if (#a==#b) #end /* multiline JScript comment
:: runas.bat
:: runas with automated password entry
#echo off
setlocal enabledelayedexpansion
set "USER=username"
set "PASS=password"
set "CMD=cmd /c dir && pause"
:: delayed expansion prevents special characters from being evaluated
cscript /nologo /e:JScript "%~f0" !USER! !PASS! !CMD!
goto :EOF
:: end batch portion / begin JScript portion */
for (var i=0, args=[]; i<WSH.Arguments.length; i++)
args.push(WSH.Arguments(i).replace(/"/g, '^"')); // escape all quotes
var user = args[0],
pass = args[1],
cmd = ' "' + args.slice(2).join(' ') + '"',
oShell = new ActiveXObject("Wscript.Shell");
oShell.run('runas /noprofile /netonly /user:' + user + cmd);
WSH.Sleep(500);
oShell.SendKeys(pass + '{ENTER}');
This is a hack, only a proof of concept, and is largely untested. I have no idea whether it'll handle "quoted arguments" in the !CMD! variable. I daresay if you want to use it for practical applications, you'll have a great deal of revising and debugging in your future.
There are a couple of other embellishments to this method you might consider.

Windows start command not able to execute batch file

I am executing start command from command prompt.
Command calls batch file named D:\My script.cmd and passes argument "Argument one". Here is command that I am tring to execute.
C:\Users\ABCUser>start "D:\My script.cmd" "Argument one"
but getting error mesasge as The system cannot find the file Argument one.
I don't understand why command is searching for file. Contents of file My script.cmd.
#echo off
cls
echo "Hello"
echo %1
Am I missing something or command syntax is wrong ? This command is not even working for file name without spaces.
It's the well known bug of start/cmd.exe handling a cmd and also an argument with quotes.
The cause is, that start uses cmd.exe /k to start the new task.
The help of cmd /k and cmd /c explains, that in this case the first and last quote are removed.
And additionally you used the start command wrong.
This should work, as the call works like a dummy to supress the quoting problem
start "Title" call "D:\My script.cmd" "Argument one"
You can use cmd.exe /c "D:\My script.cmd" arg1 arg2
If there is a problem you can switch the /c with /k which will leave the cmd open for you to examin the errors..
gl,
Refael

Resources