Saturday, May 18, 2013

VBS Tricks (.vbs Script)

Introduction

These are some of my favourite .vbs scripts in existence. VisualBasic Scripts (.vbs files) can be written in notepad , just like batch files, but they have significant control over everything in a PC. As much control as a complete PC program - but without any fuss, and they can be run on PC's you have no administrator rights on. The only trade off i've found is that for every vbs script you run, there will be a copy of wscript.exe running in the 'Processes' tab of Task Manager. That's a pain if you're trying to be incognito.

What you'll need:
 - A PC with Windows XP/Vista/7 - that's it! this'll already have notepad on it - and that's all you'll need.

To run the script specified, do this:
  1. Copy the code given into a notepad file, then save the file as anything.vbs (Make sure you have '*.* All files' selected, otherwise the resulting file will be anything.vbs.txt).
  2. Double-Click the file you just made or,
  3. Create a batch file with the line 'start anything.vbs' in it, then run the batch file.

Terminate a running process/program

Change 'notepad.exe' in the line that says strProcessToKill = "notepad.exe"  to the name of the process you want to terminate. Tis will terminate all occurences of that .exe which are running at the time. Like a quick version of Task Manager - handy for software developers. To find the .exe program name of the process you want to terminate:
  1. Press Ctrl+Alt+Del, 
  2. Click Task Manager,
  3. In Task Manager, Click the tab called 'Processes'.
  4. Find the name of your process in the 'Description' column,
  5. Find its equivalent in the 'Image Name' column (This will end with '.exe').
'Terminate all processes involving the name <strProcessToKill>
Option Explicit
Dim strComputer,strProcessToKill, objWMIService, colProcess, objProcess
strComputer = "."
strProcessToKill = "notepad.exe"
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" _
   & strComputer _
   & "\root\cimv2")
Set colProcess =
objWMIService.ExecQuery _
   ("Select * from Win32_Process Where
Name = '" & strProcessToKill & "'")
For Each objProcess in
colProcess
   msgbox "... terminating " &
objProcess.Name
   objProcess.Terminate()
Next

PC Speaking text

Displays a message box on the screen, where the user has to type in some text to be spoken. Only works on Windows PC's (They all contain Sound API which is used for this).

    Dim msg, sapi
    msg=InputBox("Enter your text for conversion:","Text-To-Speech Converter")
    Set sapi=CreateObject("sapi.spvoice")
    sapi.Speak msg

Run a Batch (.bat) file, with no command prompt window

This runs the batch file called 'testfile.bat', from the same folder as where this code is run.

    CreateObject("Wscript.Shell").Run "testfile.bat", 0, False

Run a Batch (.bat) file, with no command prompt window - drag n drop

This runs a batchfile, when you drag the batch file onto the vbs file which contains this code.


    CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

No comments:

Post a Comment