Posts Tagged ‘script’
Use of Shell, Registry, Keyboard and Voice in VBScript (Windows)
Look here to learn how to create Visual Basic Scripts.
Using Visual Basic Script you can also access elementary Windows services, some them being really useful to irritate your friends. If at any point of the script there is an error stating that the double-quotes used in it are invalid characters, just delete the copied ones and type them again manually.
With the use of a shell, you can make VBScripts run programs and other executables. To do so you have to specify following line in the beginning of the script:
Set objShell = wscript.CreateObject(“wScript.Shell”)
Afterwards you can run commands with
objShell.run program
where you replace program with the program you want to run.
To add an executable to the autostart list via registry, you use the following script:
objShell.Regwrite “HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\some_name.exe”,”C:\the\files\path.vbs
Replace the name with any name, it really can be any name, and the path of the file with the correct path. The script will then be executed on every start-up (be careful if you use this in combination with more dangerous scripts).
To make the script itself type as if it was the keyboard, use this script (a shell must be set for this as in the first script shown here):
objShell.sendkeys”key_goes_here”
Replace key_goes_here with the keys the script is supposed to hit. Some of them are embraced by curly brackets, as for example “{CAPSLOCK}”, “{NUMLOCK}”, “{SCROLLOCK}” and “{bs}” (backspace). Most however go without, as do all letter keys and enter (“~”). Combining all the locks with a loop usually gives a great script!
To make Windows’ integrated voice say some words defined by you, use the following script:
Set objVoice = CreateObject(“SAPI.SpVoice”)
objVoice.Speak “blabla”
Replace blabla with some serious things your computer has to say to you.
Messages, Loops and Pauses with Visual Basic Script
Visual Basic Script is a simple scripting language you can use in Windows to compile executable .vbs files. They can be created with a notepad document, save the script by choosing All files in the Save as type drop-down menu and give it a name ending with .vbs (if you leave the first drop-down menu on text-document, notepad will attach an invisible text-document suffix on the created file, no matter if you say so in the document name).
Now to the scripting VBScript can be used to either ease the use of Windows (as seen here) or to prank the hell out of your friends’ computers.
Here are a few useful script lines:
MsgBox “Text goes here!”,extracodes+go+here,”Title goes here!”
This shows a simple Windows Messagebox with a text and a title. The Extracodes determine special characteristics. They are defined in numbers and are seperated with a plus symbol: 0-5 define the available buttons. 0=Ok; 1=Ok,Cancel; 2=Abort,Retry,Ignore; 3=Yes,No,Cancel; 4=Yes,No; 5=Retry. Icons are defined by: 16=Critical Icon, 32=Warning Query Icon, 48=Warning Message Icon, 64=Info Icon. The code 4096 makes the window stay on top. Here is what following code looks like:
MsgBox “Hello hello, I’m a message box with a critical Error!”,2+16+4096,”Critical Error”
However, a simple messagebox will not impress anybody. How about a messagebox that reappears every time you close it? To do that, you need a loop. Simple put these two lines around the commands you want to loop:
do
[...]
loop
The commands will now be infinitely repeated until the script is interrupted. To put an interval into the loop, we let the script pause for a specific period of time. This is done with
wscript.sleep number
Replace number with an amount of milliseconds to wait.
Shortcuts for creating System Restore Points on Windows 7
On every Windows system able to create system restore points that undo any configuration changes made after their creation, there is also the possibility to create simple quick-link icons, consisting of a few line of code, that enable you to create restore points with a double-click. On Windows XP this is achieved with only two lines of code. Learn in this post, how it is done. In Windows Vista and 7 however, there are a few problems that stand in the way of our (automatic) one-click system restore point. First one is, that the script we need to run to create system restore points can only be run with administrative powers, so we need a way to get those. Second is the annoying User Account Control that asks as if we really want to run that script. This would not be such a great deal, but if you are the kind to create restoration points quite often or after a time schedule, the UAC may become a great pain.
To start with the administrative rights, there are more than one way to get those. The probably least complex one is to add the Run as administrator option to the menu appearing upon right-clicking the script:
This is done by adding the appropriate keys to the Windows Registry. To open that, open Run… by entering run into the Windows search bar in the main menu and clicking on the program. Type in regedit into Run and the Windows Registry will open. It consists of two columns, one on the left, containing the key directories, and one on the left, showing the keys’ values. Take on the left column and browse the HKEY_CLASSES_ROOT\VBSFile\Shell directory. Right-click it and select New > Key. Name it Runas and leave its values as they are (there is only one). Now right-click the Runas key and again select New > Key. Name the newly created key Command and leave the values as they are. Right-click the Command key and select Export…. Give it a name and save it somewhere you will find it. Go to the directive you saved it and open it with notepad. Erase all of its contents and paste this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VBSFile\Shell\Runas\Command]
@=hex(2):22,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,\
53,00,63,00,72,00,69,00,70,00,74,00,2e,00,65,00,78,00,65,00,22,00,20,00,22,\
00,25,00,31,00,22,00,20,00,25,00,2a,00,00,00
Save the file and double-click to merge it with the registry. The Open as administrator option should now be available to all Visual Basic Scripts (you can use the one from the linked XP tutorial with this option enabled).
However, this option neither is automatic nor does it circumvent the UAC. An option that enables creating a restoration point on double-click (but still, without disabling UAC) is to alter the script that you use for this. Open a new notepad (it has to be notepad) and paste the following code:
if wscript.arguments.count = 0 then
set objshell = createobject(“shell.application”)
objshell.shellexecute “wscript.exe”, wScript.scriptfullname & ” run”, , “runas”, vbnormalfocus
else
getobject(“winmgmts:\\.\root\default:systemrestore”).createrestorepoint “Automatic Restore Point (Win7 Script)”, 0, 100
end if
Save the script as *.vbs and make sure to select All files from the dropdown menu before saving! With this, you have enabled administrative rights beforehand every time you run the script. Now there still is a slightly circumstantial method to also disable the UAC prompt. To achieve this, you need to have a desktop shortcut pointing to a scheduled task that runs the script with highest privileges grantable. To access scheduled tasks, browse Menu > Control Panel > System > Administrative Tools > Scheduled Tasks. Create a new one without any trigger, executing your script (be sure to remember the task’s name, you will need it once more) and check the Run with highest privileges box on the first tab.
Now right-click your desktop and create a new shortcut. Let it point to following location (enter your own task-name in the last option):
C:\Windows\System32\schtasks.exe /run /tn “EnterYourTaskNameHere”
Upon opening, it should execute the script without asking for anything.
Create System Restore Points in Win XP automatically
Since older system restore points are deleted after some time, it is always good to create some new regularly. But to always have to browse through the countless menus day by day can be really annoying, that is why we will use Windows’ Notepad and Scheduled Tasks functions to ease our lives.
First we create a short script that creates system restore points in our notepad, therefore open one and type in these two lines:
Set auto_rp = getobject(“winmgmts:\\.\root\default:Systemrestore”)
auto_sys_rp = auto_rp.createrestorepoint (“Automatic System Restore Point”, 0, 100)
When saving, make sure to not save it as text document but select All Files from the Save as type dropdown menu and name it something like automatic_sysrp.vbs. vbs is the type of file here, make sure you have it correct, since otherwise the script will not be executable.
Now that you have created the script, you can doubleclick it to create a system restore point. But to have it done automatically, we are going to use Windows’ Scheduled Tasks function. Therefore, enter Start > Control Panel > Performance and Maintenance > Scheduled Tasks. Select File > New > Scheduled Task from the control bar. Give it a name, rightclick it and select Properties.
In the opened window, browse the location of the vbs script you just created and go to the Schedule tab. Enter a time when the restore point shall be created and click on Apply when you are finished. A new system restore point will now be created at the time you specified or on doubleclick upon the script.




