vbscript command from commandLine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

vbscript command from commandLine

#1 Post by Ed Dyreen » 13 Oct 2015 19:32

Is it possible to run this vbscript command from the commandLine, if no vbscript file exists ?

Code: Select all

cscript.exe //E:vbscript wscript.sleep( 3000 )
Windows Script Host is looking for a file, is there a way to have it not look for a file and run this vbscript command instead ?

Code: Select all

Microsoft (R) Windows Script Host versie 5.7
Copyright (C) Microsoft Corporation 1996-2001. Alle rechten voorbehouden.

Invoerfout: Kan scriptbestand C:\profSys\ADMIN\wscript.sleep( niet vinden.
Or is this just impossible because of the way vbscript works by design ?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: vbscript command from commandLine

#2 Post by Squashman » 13 Oct 2015 19:46

Yes. It needs to be in a script file. But, from what I have seen you can run Powershell commands like that. I assume because it is a shell like cmd.exe.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: vbscript command from commandLine

#3 Post by Aacini » 13 Oct 2015 21:01

Yes, the file is needed. However, a simple trick may be used:

Code: Select all

echo wscript.sleep(3000) > tmp.vbs & cscript //nologo tmp.vbs & del tmp.vbs

Antonio

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: vbscript command from commandLine

#4 Post by npocmaka_ » 14 Oct 2015 02:15

with mshta:

Code: Select all

mshta "vbscript:Execute("createobject(""scripting.filesystemobject"").GetStandardStream(1).write(""hello""):close")"|more


Though its verbose and ugly , you need 'more' or 'findstr "^"' at the end (if you want to print something in the console), you have flashing mshta (with jscript this can be fixed with 'code' - is there something similar here) , no access to WScript object (and will need some time to come out with sleep function) and you should be pretty careful with the quotes.

With mshta one-liners javascript is a little bit more convenient because both single and double quotes can define a string. But with javascript there's no passing by reference and it is needed by some functions in some functions provided by MS.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: vbscript command from commandLine

#5 Post by npocmaka_ » 14 Oct 2015 04:12

But on the other hand this:

Code: Select all

echo WScript.Echo("#")>con && cscript /E:vbscript con


prints(not the usual error about missing file) :

CScript Error: Execution of the Windows Script Host failed. (Not enough storage is available to complete this operation. )


I assumed that this is because of the missing EOF in the con device.I tried this with the same result :
(not sure if the first line will produce EOF)

Code: Select all

>for /f %a in ('cls') do set "EOF=%a"
>echo WScript.Echo("#")%EOF%|cscript /E:vbscript con

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: vbscript command from commandLine

#6 Post by Squashman » 14 Oct 2015 08:21

Would be neat if you could somehow read the clipboard memory as a file but again that would require some third party program to do that.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: vbscript command from commandLine

#7 Post by foxidrive » 15 Oct 2015 02:21

Squashman wrote:Would be neat if you could somehow read the clipboard memory as a file but again that would require some third party program to do that.


You mean binary files like images etc, right?

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: vbscript command from commandLine

#8 Post by npocmaka_ » 15 Oct 2015 03:23

Squashman wrote:Would be neat if you could somehow read the clipboard memory as a file but again that would require some third party program to do that.


Internet.Explorer application and mshta (which is basically also internet explorer) have clipboardData.getData('Text') method (example taken from einstein1969):

Code: Select all

for /f "usebackq tokens=1,* delims=[]" %i in (`mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text')));"^|find /v /n ""`) do @set "c[%i]=%j" 


Though I think its too much.


Here's one pretty simple VBExecutor.bat :

Code: Select all

@echo off
for %%# in (-h /h /help -help) do (
   if "%~1" equ "%%~#" (
      (echo()
      echo Executes vbscript statements
      (echo()
      echo Usage:
      echo  %~nx0 statement [statement]
      (echo()
      echo double quotes in ststements must be replaced with single quotes
      echo in ordred to avoid collision with command line arguments
      (echo()
      echo example:
      echo   call %~nx0 "Wscript.Echo('example')"  "WScript.Sleep(3000)"
      exit /b 0
   )   
)
cscript /noLogo "%~f0?.WSF"  //job:execute %*
exit /b %ErrorLevel%

   <job id="execute">
      <script language="VBScript">
      For i=0 to WScript.Arguments.Count-1
         Execute(Replace(WScript.Arguments.Item(i),"'","""")):
      Next
      </script>
  </job>


Which will iterate through the arguments and will execute them. For strings you'll need to use single quote instead of double quote to avoid the mess with VBScript strings and arguments enclosing with double quotes.It is not bulletproof but for one liners is ok I think:


Code: Select all

call VBExecutor "Wscript.Echo('ZzZ')"  "WScript.Sleep(3000)"



Probably I should option (with named arguments) that will switch between Eval,Execute,ExecuteGlobal ?

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: vbscript command from commandLine

#9 Post by npocmaka_ » 15 Oct 2015 05:04

Now with options to Eval,Execute and ExecuteGlobal though I'm not sure about the differences (the last two allows you to define a function) and even not sure if in this case there will be difference between Execute and ExecuteGlobal:

Code: Select all

@echo off
set job=Execute
for %%# in (-h /h /help -help) do (
   if "%~1" equ "%%~#" (
      (echo()
      echo Executes vbscript statements
      (echo()
      echo Usage:
      echo  %~nx0 [function] statement [statement]
      (echo()
      echo double quotes in ststements must be replaced with single quotes
      echo in ordred to avoid collision with command line arguments
      (echo()
      echo default function is /Execute
      echo other options are /Eval and /ExecuteGlobal
      (echo()
      echo examples:
      echo   call %~nx0 "Wscript.Echo('example')"  "WScript.Sleep(3000)"
      echo   call %~nx0 /Execute "Wscript.Echo('example')"  "WScript.Sleep(3000)"
      echo   call %~nx0 /ExecuteGlobal "Function SumTwo(a,b):SumTwo=a+b:End Function"  "WScript.Echo(SumTwo(2,2))"
      echo   call %~nx0 /Eval  "WScript.Echo(2+2)"
      exit /b 0
   )   
)

cscript /noLogo "%~f0?.WSF"  //job:execute  %*
exit /b %ErrorLevel%

   <job id="Execute">
      <script language="VBScript">
      funct="/execute"
      start=0:
      Function startsWith (str , prefix )
         startsWith = CBool (Left(str, Len(prefix)) = prefix )
      End Function
      
      check=LCase(WScript.Arguments.Item(0)):
      
      If startsWith(check,"/") then
         start=1:
         select case check
            case "/execute"
            case "/executeglobal"
               funct="/executeglobal"
            case "/eval"
               funct="/eval"
            case else
               Wscript.Echo("Invalid evaluation function " & check ):
               Wscript.Quit(1):
         end select
      end if
      
      For i=start to WScript.Arguments.Count-1
         select case funct
            case "/execute"
               Execute(Replace(WScript.Arguments.Item(i),"'","""")):
            case "/executeglobal"
               ExecuteGlobal(Replace(WScript.Arguments.Item(i),"'","""")):
            case "/eval"
               Eval(Replace(WScript.Arguments.Item(i),"'",""""))
         end select
         
      Next
      </script>
  </job>



examples:

Code: Select all

VBExecutor.bat /Execute "Function SumTwo(a,b):SumTwo=a+b:End Function"  "WScript.Echo(SumTwo(2,2))"
VBExecutor  /Eval  "WScript.Echo(2+2)"
VBExecutor.bat  "WScript.Sleep(700)"
VBExecutor.bat  /ExecuteGlobal "WScript.Echo('Example')"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: vbscript command from commandLine

#10 Post by Ed Dyreen » 16 Oct 2015 12:52

Some elaboration;

I've ran into a compatibility issue where autoIT no longer supports pre winXP SP2 OSes. So I now have 2 libraries, one that compiles a program for a pre SP2 OS and one that compiles it for post SP1 OSes.

However parrallelisation functions in the older version of autoIT tend to sometimes fail so I had to write my own. It works by starting a cmd process that locks a file and then starts executing ping.exe until the process is terminated.

However on a rare occasion ping.exe will fail and windows will show a messageBox telling that the process ping.exe failed to start. To work around this I opted to use vbScript's sleep command. The only problem with this is that there does not exist a vbscript or batch file - the commands are feeded into the child cmd process started by autoIT -.

Code: Select all

local $cmd = ""
$cmd &= "@echo off &title mutex: '" &$process &"' &setlocal EnableDelayedExpansion"
$cmd &= '    &>"' &$fullPathfile &'.LOCK" ('
$cmd &= '       (echo.' &@AutoItPID &')'
$cmd &= '       &call;>"' &$fullPathfile &'.ACTIVE"'
$cmd &= '       &for /L %i in () do ping;2>nul>&2 &if not exist "' &$fullPathfile &'.ACTIVE" exit 0'
$cmd &= '    )'
$cmd &= '&endlocal &exit 1'

; create the lock
$PID = Run ( @ComSpec &' /C ' &$cmd, '', @SW_HIDE )

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: vbscript command from commandLine

#11 Post by npocmaka_ » 16 Oct 2015 13:47

Ed Dyreen wrote:Some elaboration;

I've ran into a compatibility issue where autoIT no longer supports pre winXP SP2 OSes. So I now have 2 libraries, one that compiles a program for a pre SP2 OS and one that compiles it for post SP1 OSes.

However parrallelisation functions in the older version of autoIT tend to sometimes fail so I had to write my own. It works by starting a cmd process that locks a file and then starts executing ping.exe until the process is terminated.

However on a rare occasion ping.exe will fail and windows will show a messageBox telling that the process ping.exe failed to start. To work around this I opted to use vbScript's sleep command. The only problem with this is that there does not exist a vbscript or batch file - the commands are feeded into the child cmd process started by autoIT -.

Code: Select all

local $cmd = ""
$cmd &= "@echo off &title mutex: '" &$process &"' &setlocal EnableDelayedExpansion"
$cmd &= '    &>"' &$fullPathfile &'.LOCK" ('
$cmd &= '       (echo.' &@AutoItPID &')'
$cmd &= '       &call;>"' &$fullPathfile &'.ACTIVE"'
$cmd &= '       &for /L %i in () do ping;2>nul>&2 &if not exist "' &$fullPathfile &'.ACTIVE" exit 0'
$cmd &= '    )'
$cmd &= '&endlocal &exit 1'

; create the lock
$PID = Run ( @ComSpec &' /C ' &$cmd, '', @SW_HIDE )


What about using CHOICE or W32TM for sleep?

Code: Select all

w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:N  >nul 2>&1


Code: Select all

CHOICE /T N /C 0 /D 0 >nul

(Though I'm not sure if CHOICE was available in windows XP)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: vbscript command from commandLine

#12 Post by Ed Dyreen » 18 Oct 2015 07:21

No, choice is not part of a default XP installation.

Thanks for all these ideas guys :)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: vbscript command from commandLine

#13 Post by penpen » 18 Oct 2015 10:28

Ed Dyreen wrote:No, choice is not part of a default XP installation.
I assume you have the home edition.
Edit: The following Information is wrong - i've tricked myself, see my next post:
Choice is part of XP pro, and also part of XP x64 prof.
I have no information about the other editions (Mediacenter, ULCPC, Professional Blade PC, ...).


penpen

Edit: Added the (red) edit note.
Last edited by penpen on 25 Oct 2015 07:12, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: vbscript command from commandLine

#14 Post by Ed Dyreen » 18 Oct 2015 10:30

penpen wrote:
Ed Dyreen wrote:No, choice is not part of a default XP installation.
I assume you have the home edition.
Choice is part of XP pro, and also part of XP x64 prof.

Code: Select all

C:\profSys\ADMIN>systeminfo

Naam van besturingssysteem:         Microsoft Windows XP Professional
Versie van besturingssysteem:       5.1.2600 Service Pack 3, build 2600
Fabrikant van besturingssysteem:    Microsoft Corporation

Code: Select all

C:\profSys\ADMIN>set
ComSpec=C:\WINDOWS\system32\cmd.exe
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
windir=C:\WINDOWS

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\profSys\ADMIN>choice /?
choice wordt niet herkend als een interne
of externe opdracht, programma of batchbestand.

C:\profSys\ADMIN>

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: vbscript command from commandLine

#15 Post by penpen » 18 Oct 2015 18:50

Strange, my WinXP says:

Code: Select all

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Z:\>for %A in (%PATHEXT%) do @for %a in ("choice%~A") do @echo(%~dpnx$PATH:a

C:\Windows\System32\choice.exe





But i've installed fom a self-created iso; i will install it from my original sb version to check if i've added it manually (although i couldn't remember to add anything other than Service packs and Updates).

Ed is right:
I've installed win xp (home and prof) from my original CDs and "choice.exe" is not part of the installation.
So i Ed is right with the next post, too.


penpen

Edit: Added the last paragraph.
Last edited by penpen on 25 Oct 2015 07:20, edited 1 time in total.

Post Reply