Posts Tagged ‘vbs’

How to Execute PowerShell Scripts Without Pop-Up Window

Monday, February 27, 2012 posted by CSch

PowerShell scripts are hard to run without any kind of popup. Without a small workaround, it may even be impossible, even if you specify the -WindowStyle Hidden switch – this will only result in the PowerShell window blinking up for a split second and disappearing afterwards.

You can circumvent this issue by launching the PowerShell script from a small VBScript which looks as follows:

command = "powershell.exe -nologo -command C:\Users\howtoforge\Desktop\loop.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0

Save the script as .vbs file. The -command switch is followed by the location of your PowerShell script – give the full path here (my PS script is on C:\Users\howtoforge\Desktop and is called loop.ps1). This VBS frame will cause the PowerShell script to work silently; it will no longer display any cmd window.

Break Lines in VBScript (Windows)

Monday, November 14, 2011 posted by CSch

If you have the problem that you have really long lines in your VBScript that contain only one command, but need to be broken to be able to overview it, you can use the Underscore (_) operator. Placing a space character followed by an underscore at the end of the line will make Windows treat the next line as if it was part of the previous line.
However this does not work as easy for quoted expressions. In this case you have to close the quote, place the underscore and concatenate the next line to the previous one with an ampersand (&) like this (it does not make any sense to break the line here, it is only for demonstration):

Set objFSO = CreateObject(“Scripting.File” _
& “SystemObject”)

Messages, Loops and Pauses with Visual Basic Script

Thursday, October 27, 2011 posted by CSch

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.

Create System Restore Points in Win XP automatically

Friday, October 7, 2011 posted by CSch

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.