Yeshai Bouskila Just Another Site Of Mine

26Dec/100

VBScript – Using SUB’s and not the Sandwich and Functions

So, you have seen how do use some of my ideas (Well not only mine as you can find them across the WWW but this is my Blog so they are mine for this POST....)  and one of them was the WriteToLog Sub So let's talk about SUB's and Functions what we can do and use them for.

So what's the big idea? and whats the difference between a SUB and a Function?
Both the SUB and the Functions are Procedues that can be called within your script and execute some functionality without writing lines of code over and over....
There is one major difference between SUB's and Functions and it's that a Function can return a value and a SUB can only run the proceduer.
So just as an example my WriteToLog SUB is called from withing the script's and writes to a file based on arguments provided like Text, Time, File information ETC.
And as another Example that I will write about in a future date a Function that will return if  "RegistryItemExists" or "FileVersion" you catch my drift here...

A SUB (Again not the one from Subway) is a Subroutine or some might call it a procedure are basically an instance of code that you are going to use multiple times around your script but only right the main portion ounce.

So how do you start?
The Sub will Start with a Sub and a Value as well as some Arguments here is an example of a Copy SUB

Sub xCopyFile(xCopyFrom, xCopyTo)
----> Here goes the code
End Sub

So in this case I will write all my code inside the sub where xCopyFrom, xCopyTo will get values from the execution line in your code
So my execution line could look like

xCopyFile = "C:\Test.txt", "C:\Temp\Test.txt"

In this case
"C:\Test.txt" = "xCopyFrom" argument
"C:\Temp\Test.txt" = "xCopyTo" argument

And my real SUB could do some other things like verify that the destination

Now a Function
So a Function is writen in the same generic way to open and close the function


Function FileVersion(FileName)

-----> Code goes here

End Function

And in this case i'm going to get a file version for components I want to upgrade on the system and do something if needed.....
So the Code behind the function is simple as well (you do need some more code on your script...)


Function FileVersion(FileName)

FileVersion = objFSO.GetFileVersion(FileName)

End Function

So in this case from the script I will call the function in this way
MonitorFolderApp = FileVersion("C:\MonitorApp\MonitorApp.Exe")

So
MonitorFolderApp is the variable I want to assign the version to
= FileVersion calls the function
and ("what ever arguments", "Some More Arguments")  are the arguments you pass on to the Function

You might say why should I use a function just for one line of code? well you might be correct but here is an example for checking if a registry key exists as a function (I found this online somewhere, I will need to find where so I can give them some credit!)

Function xRegItemEX (xRegKey)
 On Error Resume Next

 If (Right(xRegKey, 1) = "\") Then
 objShell.RegRead xRegKey
 Select Case Err
 Case 0:
 xRegItemEX = true
 Case &h80070002:
 ErrDescription = Replace(Err.description, xRegKey, "")
 Err.clear
 objShell.RegRead "HKEY_ERROR\"
 If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
 xRegItemEX = true
 Else
 xRegItemEX = false
 End If
 Case Else:
 xRegItemEX = false
 End Select
 Else
 objShell.RegRead xRegKey

 Select Case Err
 Case 0:
 xRegItemEX = true
 Case Else
 xRegItemEX = false
 End Select
 End If
 On Error Goto 0
End Function
Print This Post Print This Post
16Aug/101

VBScript – In the beginning LOGGING

So what is VBScript? I'm not going to go into the whole what and why... you can read it all on WiKiPeDiA.
Sort Story
VBScript (Visual Basic Scripting Edition) is an Active Scripting language developed by Microsoft that is modelled on Visual Basic. It is designed as a “lightweight” language with a fast interpreter for use in a wide variety of Microsoft environments. VBScript uses the Component Object Model to access elements of the environment within which it is running; for example, the FileSystemObject (FSO) is used to create, read, update and delete files.
Read more on Wiki - http://en.wikipedia.org/wiki/VBScript

So now they what we can do with it and why as there are other solutions.
What did I use VBS for?
Anything from Go-Live with user intervention to enter details for Machine name changes, IP Changes, Registry Changes, DB Changes and much more to automate Go-Live and System staging.
Automate backup and restore, tie into active solutions via MS solutions...
HTA files are a nice touch to make everything look somewhat up to date.
VBScript is a simple but sofisticated way of automating enviromentatal solutions.

But before I start with anything i'm going to recommend LOGGING!!! so add the following code to the end of your scripts and LOG away.
There are many variations but I like the following one and it does have some variations, but here is what I like about it.
1. Create a few log files with the same SUB
2. Consistency with all your log files

In this example we use one log and one line, the log write line

WriteToLog("Generic Log.vbs - Write This")

The output line in the log file will be
8/16/2010 11:26:00 PM    :    Generic Log    :    Generic Log.vbs - Write This

Full Code

'----------------------------------------------------------------------
' Filename:  Generic Log.vbs
' Copyright (c) Yeshai Bouskila 2009
' All Rights Reserved
'
' Please Enter Updates with date and name including line of Change
'----------------------------------------------------------------------
'---------------------------------------------------------------------- 

 set objShell = CreateObject("Wscript.Shell")
 set objFSO = CreateObject("Scripting.FileSystemObject")

'--- Main Begins ---------------------------------------

 WriteToLog("Generic Log.vbs - Write This")

'--- Main Ends -----------------------------------------

'--- Write to log --------------------------------------
Sub WriteToLog(strLogMessage)
 Const ForAppending = 8
 Const vbsName = "Generic Log"

 strLogFileName = "C:\GenericLog.log"
 strLogEntryTime = NOW

 'test whether file exists To either write/append to file
 if objFSO.FileExists(strLogFileName) Then
 Set objLogFileTransaction = objFSO.OpenTextFile(strLogFileName, ForAppending)
 Else
 Set objLogFileTransaction = objFSO.CreateTextFile(strLogFileName)
 End if

 objLogFileTransaction.WriteLine strLogEntryTime &amp; chr(9) &amp; chr(58) &amp; chr(9) &amp; vbsName &amp; chr(9) &amp; chr(58) &amp; chr(9) &amp; strLogMessage
 objLogFileTransaction.Close
 WScript.StdOut.WriteLine strLogMessage
 WScript.StdOut.WriteLine ""
End Sub

Now let's say I want to write in one script to a few log files but want to keep it constant.
We will change our SUB line to request another parameter from the WriteToLog line in the script

Sub WriteToLog(strLogMessage,strLogFileName)
 

As well another change to the log name in the function

strLogFileName = "C:\"& strLogFileName & ".log"

So what did we just do if our write to log like looks like this?

WriteToLog "Something for the VBScript Log","WhatAlog"

first area is our strLogMessage, second area is the log name strLogName
strLogMessage = "Something for the VBScript Log"
strLogName = "WhatAlog"

There are unlimited options here, have fun and ask away if you need.

Print This Post Print This Post
27Jul/100

VBSEdit and HTA Edit Quick Recommendation

VBSEdit what's it all about...
For the past two years I've been scripting for customers.
At first I used simple text editors as NotePad ++ or UltraEdit but I simply wanted much more and while reviewing more then one option on the market VBS Edit seemed like the perfect fit (price and value).
With built in debugger, EXE conversions, HTA Editor included in the package, great GUI and sample scripts all for the  small price of $59 you can't really go wrong here.
Click on read more for some screen shots and a review (not mine)

AderSoft VBS Edit website - www.vbsedit.com - There is a free version but the Debugger will increment a wait timer

Print This Post Print This Post