Salt * Wet * Bytes

October 17, 2009

Getting the network adapters’ order in Windows

Filed under: AdminScripts, Networking — saltwetfish @ 7:22 am
Tags: , ,

Now, one of the biggest problem for us when deployment new servers is that we want to get everything as automated as possible. One of the most complex to automate is networking configuration. That is, you need to figure out which nic to team and which to set as heartbeat or backup nic, etc. In this, I use nic and network adapter interchangeably.

The most persistent problem is getting a ordered list of network adapters from Windows. Windows itself does not present nics in any order. So your slot 0 (embedded) port 1 could be named as “local area network connection 3″ or called “Broadcom 1GB PCI Ethernet Adapter 4″ and the slot 10 port 2 nic could be named as the first nic. (more…)

March 31, 2008

VBScript: Faking the include directive

Filed under: AdminScripts — saltwetfish @ 4:34 am
Tags:

VBScript does not provide any include directive that is common in many other languages. Luckily, the language provides a pseudo-include statement, EXECUTEGLOBAL and EXECUTE, which we can use to fake the include directive.

Why do we need to do that in our scripts? Well, very often, we want to distribute a standard set of codes, for example, templated functions and procedures. You want to ensure that all your scripts are running from the same versions. If those functions are included inside the vbscript, then it will be impossible to control your standard functions’ version.

A real life example: I have a report class which is used by various Start of Day (SOD) scripts to provide a summary text file about their status. I don’t want to copy the codes into each script and have to update every script everytime I add or update the function of the class. Includes come in handy in this case. (more…)

January 2, 2008

HTA Scripting: displaying output

Filed under: AdminScripts — saltwetfish @ 8:58 am
Tags: ,

One of the most frustrating issue facing vbscripters who is moving over to HTA scripting must be the seemingly lack of ability for the form to display its output as you code runs.

The first attempt most scripter will do is to define a spanID and try to write the output to it. For example:

thisText = “Starting script now…”
Do loop
Do something….
thisText = thisText & <BR> & “doing something now… “
SpanID.InnerHTML = thisText
if done then Exit do
Loop

One would reasonably expect the dialog window to continuously update itself with the output text. However, we would quickly realise that the output come appears only after the entire loop has completed and the subprocedure ended or you may a call to a msgbox or other output functions. From the UI, it appears that the form hangs (as it process the loop) and then outputs the entire output after the delay.

One of the MS Article, Updating the Display During Lengthy Operations, has this to say:

Let’s imagine that the script above did execute as the author intended, and the user saw the count value on the display rise from 0 to 500. In this hypothetical situation, every time the display updated, events such as onresize and onchange would be generated. Should these events be handled in script immediately? Probably not as that could have effects on the script that was running in the original function—if a global variable’s value was changed, for example. An alternative would be to queue up all the events to be processed when the original function finally executed, but this too has a number of problems and seems far from efficient. It is useful for script to be able to execute and decide how things should be on the screen and then have all updates occur simultaneously when the function completes, rather than having a gradual display change on different parts of the screen.

Found a solution which uses a hidden cmd window to force the form to update:

Set WSHShell = CreateObject(“WScript.Shell”)
thisText = “Starting script now…”
Do loop
Do something….
thisText = thisText & <BR> & “doing something now… “
SpanID.InnerHTML = thisText
WSHShell.run “%comspec% /c “, 0, true
if done then Exit do
Loop

Tested the solution and viola! the output comes alive!

HTA Scripting: sleep function

Filed under: AdminScripts — saltwetfish @ 8:33 am
Tags: ,

Okay, for all the HTA newbies, the sleep function much be one of the most searched piece of code (the other is how to write a console-like output). Having been a full-fledged vbscript scripter, we don’t think much about the wscript.sleep method until we try to use it on a HTA script.. it does not work!

The Microsoft Scripting center’s solution for this either to use the DHTML method setTimeOut. I don’t really like the solution because it doesn’t really “sleep” the application. Rather, it sets a time delay before the application runs. The sample application is too simplistic. It calls a stub which creates a timer to launch the application at the specified delay time and passes control back to the form.

But what if I have a process that needs to pause a few seconds before it continues? I don’t think that the setTimeOut method can address this well enough.

There are others out there that recommends 3rd party addon, but wanted a more native solution.

After searching for a while, I chanced upon this little workaround in Rube Goldberg Memorial Scripting Page. Its not sometime I really like because it depends on the ping command, but it presents a better workaround than the setTimeout method for what I want to do.

Msgbox now

ccSleep 15
MsgBox now

Sub ccSleep(seconds)
set oShell = CreateObject(“Wscript.Shell”)
cmd = “%COMSPEC% /c ping -n ” & 1 + seconds & ” 127.0.0.1>nul”
oShell.Run cmd,0,1
End Sub

June 28, 2006

Getting input from command prompt from VBScript

Filed under: AdminScripts — saltwetfish @ 6:58 am
Tags:

Was looking around the net to find out how I can get input from users via the command prompt in a VBScript. Most of the examples giving are for the inputbox, which uses the Windows dialog. After a few refinements, I finally found what I wanted:

http://www.mhuffman.com/notes/language/vbs_intro.htm#INPUT

input2.vbs

Option Explicit

Dim name

Wscript.StdOut.Write “Enter your name: ” ‘prompt user for input
name = Wscript.StdIn.ReadLine ‘get user’s name

Wscript.Echo “Hello, ” & name & ” , welcome to CIS 121!”
‘ could also use Wscript.StdOut.WriteLine

Blog at WordPress.com.