Some methods to run your script

Author Message
TomRiddle
  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
Some methods to run your script - Friday, July 25, 2008 11:15 PM
0

Anyone can copy and paste some code from the web and run a script, right? You just double click on it right? No wrong, here are a dozen ways to run that little snippet. Some methods described may be done the same by different techniques. Feel free to post more methods tips or corrections, this tutorial is by no means an exhaustive list as I have not mentioned script components, scripts compiled into exe or that you can even run a script from some editors.



1. VBS Launched via wscript engine
Method
: Create a txt file containing the script, rename extension to .VBS, double click on example.vbs

Example Code
:
 wscript.echo "Hello World"
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
: Download the latest version of Windows Scripting Host if you are running on an old version of windows



2. VBS Launched via cscript engine
Method
: Create a txt file containing the script, rename extension to .VBS, launch script by typing “cscript example.vbs” from command prompt.
Example Code:
 wscript.echo "Hello World"
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
: commonly batch files are used to launch vbs files using the cscript engine, useful in seeing output in console window, output can be piped to a file by adding “>output.txt” at the end of the launch command.



3. VBS Launched via another script
Method
: Create a txt file containing the script below and give it a .vbs extention, Create another vbs file that you wish to run, save in the root of c:\, call it example.vbs and it will be run by the below code.

Example Code for runme.vbs
:
 Set WshShell = CreateObject("WScript.Shell")
 strScriptPath = "C:\example.vbs"
 WshShell.Run "cscript.exe " & Chr(34) & strScriptPath & Chr(34), 0, vbTrue
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
: This script can run any executable. Full syntax is available in the WSH documentation.



4. VBS Launched by Drag and Dropping a file onto it
Method
: Create a txt file with VBS extension and include code that is in the example, Drag and drop a file onto script and file name will be echoed

Example
:
 Set objArgs = WScript.Arguments 
 For I = 0 to objArgs.Count - 1 
 WScript.Echo objArgs(I) 
 Next
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
: If you can access the name of the file dropped on your script then you can further manipulate the file via VBScript



5. VBS Launched from Windows Explorer right click file “Send To”
Method
: Create a txt file with VBS extension and include code that is in the example below. Place the VBS file here: C:\Documents and Settings\%UserName%\SendTo

Example
:
 Set objArgs = WScript.Arguments 
 For I = 0 to objArgs.Count - 1 
 WScript.Echo objArgs(I) 
 Next
 


Requirements
: W2000 or above

Notes
: If this script can echo the name of the file sent to it then you can further manipulate that file via VBScript for example, zip it up and save it in a special folder.



6. VBS Launched from Windows Explorer right click Folder <Your Name>
Method
: Create a txt file with the following name runfolder.vbs and include code that is in the example below.
Place the VBS file here: C:\windows\runfolder.vbs
Create registry key so that computer recognises runfolder.vbs:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\<YourName>\Command

Example Code for setting registry key to launch runfolder.vbs
:
 Const HKEY_LOCAL_MACHINE = &H80000002
 strComputer = "."
 
 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
 strComputer & "\root\default:StdRegProv")
 
 strKeyPath = "SOFTWARE\Classes\Folder\shell\Run My VBS\Command"
 strValueName = null
 strValue = "wscript C:\windows\run.vbs %L"
 oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
 oReg.SetStringValue
 HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 


Example Code for runfolder.vbs
:
 Set objArgs = WScript.Arguments 
 For I = 0 to objArgs.Count - 1 
 strArgs= strArgs &" "& objArgs(I) 
 Next
 wscript.echo trim(strArgs)
 


Requirements
: W2000 or above

Notes
: If this script can echo the name of the folder you clicked on then you can further manipulate that folder via VBScript for example: Scan or copy the folder contents.



7. VBS Launched via Scheduled task
Method
: Create scheduled task to launch your script

Example Code
:
 Wscript.echo “Hello World”
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
:



8. VBS Launched via WMI remote execute
Method
: Create a simple.vbs script you would like to run on a remote computer, create another script using the below code to copy it to remote machine and then execute it from that machine.

Example Code
:
 Const OverwriteExisting = TRUE
 strComputer=inputbox("Enter Remote Computer", "Remote Execute")
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 objFSO.CopyFile "simple.vbs", _
 "\\"&strComputer&"\C$\windows\", OverWriteExisting
 Set objWMIService = GetObject _
 ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
 Error = objWMIService.Create _
 ("wscript c:\windows\simple.vbs", null, null, _
 intProcessID)
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
: There are other utilities that can remote execute scripts but the utility must be installed on the remote machine first.



9. VBS Launched via Windows StartUp Folder
Method
: Create a simple.vbs script you would like to run at windows startup. Copy script to the following folder C:\Documents and Settings\%UserName%\Start Menu\Programs\Startup

Example Code
:
 Wscript.echo “Hello World”
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
:



10. VBS Launched via registry run key
Method
: Create a simple.vbs script you would like to run on remote computer at windows startup, copy it to the c drive and then reference it from the following registry key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] Create new string value pointing to your script.
See other keys in this area for examples

Example Code
:
 Wscript.echo “Hello World”
 


Requirements
: Computer with Windows Scripting host installed, txt editor. IE3+
http://support.microsoft.com/kb/188135

Notes
:



11. HTA
Method
: Create a txt file containing the script, rename extension to .HTA, double click on it.

Example Code
:
http://www.microsoft.com/technet/scriptcenter/topics/htas/tutorial1.mspx

Requirements
: Windows 2000or later and Internet Explorer 5.0 or later.

Notes
: The fist thing you should do is download
Helpomatic which is a HTA itself. This script provides working “copy and paste examples” of most things including a basic HTA which you can build on.
HTA has some VBScript syntax differences to VBS files



12. ASP
Method
: Create a txt file containing the script, rename extension to .ASP place on web server and open via web browser.

Example Code
:
 <html>
 <body>
 <%
 response.write("Hello World!")
 %>
 </body>
 </html> 
 


Requirements
: Microsoft web server (comes with XP or Windows Server), txt editor

Notes
: ASP has some VBScript syntax differences to VBS and HTA files
Code between these tags <% code %> is executed server side. I.e. if you view the source of the above example from your browser you would only see
 <html>
 <body>
 Hello World
 </body>
 </html> 
 


Notes
: ASP.net and above have higher server requirements but you get the gist. ASP renders HTML so just about any web browser these days will do.





13. Active Setup
Method
: Create a txt file containing the script, rename extension to .VBS, copy script to folder on your C drive, keys in the registry will launch the script once for each person that logs onto that machine.


Example Code
: http://www.visualbasicscript.com/fb.aspx?m=67461

Requirements
: Windows 2000 or later and Internet Explorer 5.0 or later.

Notes
:
Active setup is a process that runs automatically when a user logs in. It is commonly used to make changes in users profiles, i.e. add HKCU registry entries.

The following Active Setup Registry keys

HKLM\Software\Microsoft\Active Setup\Installed Components\%APPNAME%
and
HKCU\Software\Microsoft\Active Setup\Installed Components\%APPNAME%

are compared, and if the HKCU registry entries don't exist, or the version number of HKCU is less than HKLM, then the script specified in the "StubPath" key is executed for the current user.
<message edited by TomRiddle on Friday, December 05, 2008 11:33 PM>

Infer Sententia
  • Total Posts : 8
  • Scores: 0
  • Reward points : 0
  • Joined: 10/11/2008
RE: Some methods to run your script - Monday, October 20, 2008 7:49 PM
0
Hi, i was wondering, is there a program or something that can compile a .VBS script into an EXE file?

mbouchard
  • Total Posts : 2110
  • Scores: 29
  • Reward points : 0
  • Joined: 5/15/2003
  • Location: USA
RE: Some methods to run your script - Wednesday, October 22, 2008 12:01 AM
0
Yes there are, do a quick search on the forum and you will find some suggestions.
Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
RE: Some methods to run your script - Wednesday, October 22, 2008 2:18 AM
0
The biggest issue to keep in mind when compiling or hiding a VBS in an exe is the fact that the WScript object and other things may no longer be directly available. Be sure to test your code  and be paranoid about any of these compiler's.
To iterate is human, to recurse divine. -- L. Peter Deutsch

ebgreen
  • Total Posts : 8227
  • Scores: 98
  • Reward points : 0
  • Joined: 7/12/2005
RE: Some methods to run your script - Wednesday, October 22, 2008 3:12 AM
0
Even better is to determine why you need to hide the code inside an exe then make that reason go away.
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

Fredledingue
  • Total Posts : 572
  • Scores: 2
  • Reward points : 0
  • Joined: 5/9/2005
  • Location: Europe
RE: Some methods to run your script - Thursday, March 19, 2009 12:00 PM
0
Hi,
Just to say that "Sent To" and "Context Menu" methods works on w98 (with a limitation in the number of files to be passed at 128).

For the right click context menu I'm using Fast Explorer which has no limitation and I have written an hta to especialy to include your vbs  in such context menus (and manipulate simple menus too).

Context Menu Customizer Hta 0.5 Beta

Customize the right-click menu in Explorer

-Add, remove, edit items from the context menu for a filetype or a file extention, drives and folders.
-Add, remove, edit Fast Explorer items in the context menu (if the component {included} is installed).
-Modify basic settings for the file type (icon, double-click/open program, caption).
-Add vbs scripts to the context menu
<message edited by Fredledingue on Friday, April 10, 2009 12:50 PM>
Fred

TomRiddle
  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
RE: Some methods to run your script - Friday, March 20, 2009 1:20 AM
0
Good one Fredledingue.
All the cool apps have right click menus.

Fredledingue
  • Total Posts : 572
  • Scores: 2
  • Reward points : 0
  • Joined: 5/9/2005
  • Location: Europe
RE: Some methods to run your script - Friday, April 10, 2009 12:53 PM
0
I noticed with horror that the context customizer I had on my website was a very old one not even working on xp.
I have updated it now. But apparently nobody has tried it yet because I didn't hear any complain...
Fred