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>