Creating a basic vbscript to call a single command - command-line

I have installed program where I need to run a command line in order for it to do something. So I want to create a simple vbscript to carry out this command ion order for me to deploy the cmd to several machines using SCCM2012.
The program sits in the %ProgramFiles% directory and it is an 'exe' file with a cmd switch I am wanting to use, example
program.exe -testcmd
Hope this makes sense and can anyone assist please?
I have looked at the forums but I don't understand what all the command mean in the script.....(I am hopeless)

You can use the Run method of the WshShell object. Just make sure to put quotes around the path to your EXE. In VBScript, you need to escape embedded quotes (by doubling them) or you can use the Chr(34) statement.
strCmd = Chr(34) & "%ProgramFiles%\program.exe" & Chr(34) & " -testcmd"
CreateObject("WScript.Shell").Run strCmd

Related

Can i run a exe with cmd or powershell if i have spaces in username?

Ex:
cmd /C start C:\Users\Bob Builder\Desktop\New Folder\test.exe
I'm trying to use cmd to start a file but since there are spaces in the path, cmd is throwing an error after Bob.
Error:
"Windows cannot find C:\Users\Bob. Make sure you typed the name
correctly, then try again."
The system cannot find the file C:\Users\Bob.
Its simply failing to accept the spaces. It's driving me crazy because I'm spoiled with C# working out of the box. I don't know much about this, I have been spending way too much time trying to figure this out. Some help would be greatly appreciated.
In order for a path that contains spaces to be recognized as a single path (argument), it must be quoted.
In order for an executable to execute in the current console window, synchronously, with its streams connected to the calling shell, it must be invoked directly, not via start.
Direct invocation from cmd.exe (only "..." quoting supported):
"C:\Users\Bob Builder\Desktop\New Folder\test.exe"
From PowerShell:
& 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'
Note:
PowerShell also supports '...' strings (single-quoted), which are verbatim strings that are preferable to "..." (double-quoted) ones if you do not require expansion of variables (string interpolation) - see the conceptual about_Quoting_Rules help topic.
For syntactic reasons, PowerShell requires the use of &, the call operator to invoke commands that are quoted and/or contain variable references - see this answer for details.
By contrast, use start in cmd.exe / Start-Process in PowerShell (whose built-in alias is also start) to launch an executable in a new window (on Windows), asynchronously, with no (direct) ability to capture the launched executable's output:
From cmd.exe:
start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"
Note:
Specifying "title" - i.e. a self-chosen (console) window title - is required for syntactic reasons in this case: without it, the double-quoted path itself would be interpreted as the window title, and the - implied - executable to launch would be another cmd.exe instance.
Note that if you launch a GUI application this way, the title argument is irrelevant, because no new console window is created.
Conversely, if you launch a console application specified by double-quoted path and therefore must use a title argument, note that "" will result in the new window having no title.
From PowerShell (parameter -FilePath is positionally implied):
Start-Process 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'
Note:
Start-Process does not support specifying a window title, so you may want to call cmd.exe's internal start command for that (or other features not supported by Start-Process, such as specifying the process priority).
To work around quoting problems, invoke cmd.exe's start from PowerShell by passing the entire start command as a single string to cmd /c:
cmd /c 'start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"'
cmd /C start "C:\Users\Bob Builder\Desktop\New Folder\test.exe"
Quotes are your friend. Sometimes even double quotes are too!
Seems like cmd won't work for me. Powershell worked with this script:
$env:Path += ";C:\Users\Bob Builder\Desktop\New Folder\"
test.exe

PowerShell SCript to Execute CMD.exe with Arguments

SO I have surfed this site and the web and I feel as though I am missing something simple.
I find related questions but none that combine a scriptblock and remote calling of a 3rd party app (not a simply windows function or app)
I have the following string that I can copy into a command window and run without issue
"C:\Program Files (x86)\Vizient\Vizient Secure Channel v2.1\VizientSC.exe" UID=me#musc.edu PWD=XXXXXXXXX HCOID=123456 PRODTYPE=PRO-UHCSECURECHANNEL-CDB PACKAGETYPE=OTH FOLDERPATH="\\da\db5\MyFiles\Viz\20180413"
To simplify this, lets just assume I want to run this same String every time BUT with a REMOTE call.
I have written this many different ways but to no avail using
Invoke-Command -ComputerName "edwsql" -ScriptBlock { .........
I simply want to run the designated string using cmd.exe on a remote machine.
The EXE being run in the string is a 3rd party software that I do not want to install all all possible locations. Much simpler to run remote form the box it is already installed and is secure.
Can someone point me in the right direction???? Pls???? I'm new to PowerShell. I am trying to phase out some old PERL as the folks who can support that on the client site are few and far between these days.
You don't need to try so hard. PowerShell can run commands. If the command you want to run contains spaces, enclose in " (as you have done) and invoke it with the & (call or invocation) operator. This is all you need to do:
& "C:\Program Files (x86)\Vizient\Vizient Secure Channel v2.1\VizientSC.exe" UID=me#musc.edu PWD=XXXXXXXXX HCOID=123456 PRODTYPE=PRO-UHCSECURECHANNEL-CDB PACKAGETYPE=OTH FOLDERPATH="\\da\db5\MyFiles\Viz\20180413"
If a parameter on the executable's command line contains any characters that PowerShell interprets in a special way, you will need to quote it.

Trying to start a program in VBScript

I had been able to start this program previously with the following code:
dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strCmd = "cmd.exe /c start /D C:\Jts C:\Windows\system32\javaw.exe -cp jts.jar;total.2012.jar -Dsun.java2d.noddraw=true -Dswing.boldMetal=false -Dsun.locale.formatasdefault=true -Xmx768M -XX:MaxPermSize=128M jclient/LoginFrame C:\Jts"
WshShell.Run(strCmd)
Now, however, thanks to a wonderful java update, my java.exe file is located here:
C:\Program Files (x86)\Java\jre1.8.0_31\bin\javaw.exe
I am having trouble replacing the strCmd variable above so that my VBScript doesn't error out. I know it has to do with the spacing in Program Files (x86) and i have tried to implement this answer: How to use spaces in CMD?
but it doesn't seem to be working. Please help and explain what these spaces do please.
EDIT:
I just figured it out. God, I hate spaces. Apparently this worked, I would like to know if this is the best solution or not:
strCmd = "cmd.exe /c start /D C:\Jts C:\""Program Files (x86)""\Java\jre1.8.0_31\bin\javaw.exe -cp jts.jar;total.2012.jar -Dsun.java2d.noddraw=true -Dswing.boldMetal=false -Dsun.locale.formatasdefault=true -Xmx768M -XX:MaxPermSize=128M jclient/LoginFrame C:\Jts"
The first set of parameters in quotes for the Start command is assumed to be the windows title.
Your command does not work, it may appear to but that's because it's so wrong Windows can't tell how wrong it and treats it as a partial path..
JavaW would be listed under app paths. You are specifing your folder as a window title, leaving the program name isolated (but Windows knows how to find GUI programs just by name).
dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c start """" /D C:\Jts ""C:\Windows\system32\javaw.exe"" -cp jts.jar;total.2012.jar -Dsun.java2d.noddraw=true -Dswing.boldMetal=false -Dsun.locale.formatasdefault=true -Xmx768M -XX:MaxPermSize=128M jclient/LoginFrame C:\Jts"
Fixed needless obfuscation and pointless variable generation.
Removed brackets from Run (you aren't testing the return value).
Put in a set of blank quotes for Windows Title ("""")
Quoted JavaW path (doesn't need it for System32). Remember if quotes
are used anywhere for Start the first set MUST be windowtitle.

Is there a way to have a readable SINGLE cmd/powershell script?

In olden days, I remember a trick we used to use to run Perl scripts within Windows cmd.exe as a simple invocation of a cmd file rather than having to run perl.exe with the script name, something like:
#rem = '
#perl -x -S %0 %*
#goto :eof
#rem ';
<insert Perl script here>
This used the rather neat trick of exploiting differences in how cmd.exe and perl.exe would handle the input script. Windows' cmd.exe would read the first three lines as three separate commands which would:
have a comment with no echo;
run perl with the same input file and arguments, without echo; and
goto the end of the file (exit) with no echo.
On the other hand, perl.exe would treat the first four lines as an assignment statement, then go on to execute the Perl script proper.
Now it's often a pain to run Powershell scripts from the cmd.exe command line since you have to use something like:
powershell -file go.ps1
I'm wondering if there's a way to encode both batch and powershell commands into a single cmd file, similar to the Perl trick, in such a way that it starts running under cmd.exe but switches to Powershell quickly after that.
If that were possible, you could run your script go.cmd simply by entering:
go
at the command line, rather than some convoluted invocation of powershell.
I know that you can ship separate cmd and ps1 files but that's in fact what I'm trying to get away from. I'm looking for a single file solution if possible.
I also know that you can base-64 encode your script or execute it as a string, provided you replace all newlines with semicolons. But that means the Powershell stuff is no longer easily editable or readable in the resulting file.
Fortunately or otherwise, Powershell knows exactly what to do with .cmd files: use CMD to run them.
#set $a=%0 && powershell -encodedcommand ZwBjACAAKAAoAGQAaQByACAAZQBuAHYAOgBgACQAYQApAC4AdgBhAGwAdQBlACAALQByAGUAcABsAGEAYwBlACAAJwAiACcALAAnACcAKQAgAHwAIABzAGUAbABlAGMAdAAgAC0AcwBrAGkAcAAgADIAIAB8ACAAcABvAHcAZQByAHMAaABlAGwAbAAgAC0A
#exit /b
"Hello from Powershell! You know it's me because cmd would never know that 2 + 2 = $(2 + 2)!"
The encoded command is
gc ((dir env:`$a).value -replace '\"','') | select -skip 2 | powershell -
Which fetches the contents of the script invoking itself, skips the stuff intended for CMD and, yes, runs Powershell on the rest.
Since we're piping commands through stdin and not having them in a proper script file there may be weirdness for more complicated scripts, doubly so if you start nesting these hybrids. I wouldn't trust this in production, but hey, it's something.

WScript.Echo throwing object required error in vbscript

I'm trying to display messages as my vbscript program runs. It runs off a command prompt in xp, for example: cscript.exe test.vbs. I don't want to use msgBox while this is running as I just want it to post the scripts progress, but I don't want any user interaction.
I tried using Wscript.echo "Some text", but I'm getting compile errors when I step through the program using Words built-in vbeditor.
I found this code and it runs fine in another file:
Option Explicit
Dim strComputer
strComputer = "LocalHost"
WScript.Echo "Computer: " _
& strComputer
WScript.Quit
I then tried using Dim and set to setup a Wscript variable, but that didn't work either.
Any ideas as to what I'm doing wrong? I did verify Wscript is running on this machine.
Thanks,
James
Word uses VBA (Visual Basic for Applications), not VBScript. Although both languages belong to the Visual Basic family, they have differences. One of them is that the WScript object isn't available in VBA - that's why you're getting errors when debugging your script in Word.
Having said that, your code is valid and runs perfectly fine with both cscript and wscript.