Category Archives: VBScripts

VBScript to list shares and permissions in Windows OS

I thought that this would be an easy script to find, but surprising there are no complete script to be found. I found the following powershell script in the respository

It works and lists the shares, but it does not product the path of the share!

Found another helpful posts for VBScript version here:

It works but for some reasons the security permissions are not correct

The closest I find is this:

Works well BUT it again does not list the path of share. Why in the world would I not want to know where a share path is? Read the rest of this entry »


Posted by on February 27, 2013 in Scripts, VBScripts, Windows



Using Get-ADObject to query subnets and sites

Okay, I did not develop the scripts myself; I modified them from this site, I needed this for quick and easy queries

#List all subnets for a site (okay its a onliner, but you can always use a variable to replace the site name)

foreach ( $SubnetDN in (Get-ADObject -Identity
 -properties *).siteObjectBL) 
{Get-ADObject -Identity $SubnetDN -properties * | % {$} }

#List all subnets (long process)
Get-ADObject -Filter 'ObjectClass -eq "subnet"'
 -SearchBase 'CN=Configuration,DC=fabrika,DC=com'

#List all sites (long process)
Get-ADObject -Filter 'ObjectClass -eq "site"'
 -SearchBase 'CN=Configuration,DC=fabrika,DC=com'

 #List the site associated with a particular subnet" 
Get-ADObject -Filter 'ObjectClass -eq "subnet" 
-and name -like "*"' -SearchBase 
'CN=Configuration,DC=fabrika,DC=com' -properties siteobject
Leave a comment

Posted by on November 29, 2012 in Scripts, VBScripts, Windows



VBScript: Adding date/time stamp to log file name

Very often when we do logging for our script we would also want to apply a date/time stamp to them. Unfortunately VB does no offer any easy way to do this. This is how I do it:

LogFile = GetScriptPath() & "iStar_" & _
 cStr(Year(now())) & Pad(cStr(Month(now())),2) & _
 Pad(cStr(Day(now())),2) & Pad(cStr(Hour(now())),2) & _
 Pad(cStr(Minute(now())),2) & Pad(cStr(Second(now())),2) & _

You will see an additional function Pad(), this is necessary to ensure constant number of strings. The function simply added addition “0” for single digit numbers to make them 2 digits. It is of course a useful function for other stuff.

Function Pad(CStr2Pad, ReqStrLen)
Dim Num2Pad

 Pad = CStr2Pad
 if len(CStr2Pad) < ReqStrLen then
 Num2Pad = string((ReqStrlen - len(CStr2Pad)), "0")
 Pad = Num2Pad & CStr2Pad 
 end if
End Function

Posted by on October 16, 2012 in Scripts, VBScripts


VBScript: object not found, but its there… I am sure.

This is a simple VB logic, it checks if a file exists is another directory can move it there if not. It also logs an entry regarding the move. However, could you spot any problem with this?

  1. set f = fso.getfile(“c:\temp\myfile.txt”)
  2. newdir = “c:\temp2”
  3. If Not fso.FileExists( newdir & “\” & Then
  4. fso.MoveFile f.path, newdir
  5. l.writeline now & “: ” & “Moved file = ” & & “, To = ” & newdir
  6. End If

When this is run, it returns an error “Object Not Found” at line 5.

It took me awhile to figure it out although it should have been obvious.

The error occurs at line 5 at “” object. The problem is that in line 4, the file object is moved to a new location, but in line 5 we ended up reference a stale object or an object that is no more there, its been moved!

So the correct way to fix is to save the into a variable (e.g. oldfile) and reference in line 5 such as:

l.writeline now & ": " & "Moved file = " & oldfile & ", To = " & newdir
Leave a comment

Posted by on October 16, 2012 in Scripts, VBScripts


VBScript to grab server names from a list of HP Blade enclosures

Challenge: How to get all blade’s server names (iLO name really) from a list of Enclosure’s IP address? This so far works only for HP Blade C series enclosure

set objArg = WScript.Arguments
if objArg.Count >= 4 then

  set WshShell = WScript.CreateObject("WScript.Shell")

  Encl_IP = objArg(0)
  Encl_Name = objArg(1)
  LogonID = objArg(2)
  LogonPWD = objArg(3) "telnet.exe -f .\" & Encl_Name & "_" _
   & Encl_IP & ".log " & Encl_IP
  WScript.Sleep 2000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys LogonID
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys LogonPWD
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys "SHOW SERVER LIST"
  WScript.Sleep 1500

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys "exit"
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1500

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys ("{Enter}")

  Set fso = CreateObject("Scripting.FileSystemObject")
  set f = fso.OpenTextFile(".\" & Encl_Name & "_" & Encl_IP & ".log")

  Is_SOL = false  'Is start of line?
  SvrInfo = ""
  do while not f.AtEndOfStream
    t = f.readline
    if instr(t,"Bay iLO Name ") > 0 then
      t = f.readline
      t = f.readline
      Is_SOL = true
    elseif instr(t,"Totals:") > 0 then
      Is_SOL = false
      exit do
    end if

    if Is_SOL then  'grab server name from current line
      t = mid(t,5,29) & vbTab & Encl_Name & vbTab & Encl_IP
      SvrInfo = t & vbCrLf & SvrInfo
    end if



  set f1 = fso.OpenTextFile(".\ServerList.log",8,true)
  f1.Writeline SvrInfo


  Wscript.echo " "
  Wscript.echo "<encl. IP> <encl. name>  "
  Wscript.echo " "
  Wscript.echo "Log file for telnet session = <encl. name>_log.log"

end if
Leave a comment

Posted by on August 1, 2012 in Scripts, VBScripts


Tags: ,

Setting the correct encoding when using Out-File

One caveat I realized today when trying to create a file using Out-file in powershell.

The command is simple enough:

dir c:\temp\*.exe | Out-file c:\test.txt -append

After that, I used for loop in command prompt to process the txt file

for /f %a in (c:\test.txt) do @echo %a

Guess what? Nothing happened! The above command does not work as it should!

This is due to the encoding used by Out-file. The default encoded in unicode, which the batch command could not read. So if you intend to generate a file for tools like batch commands to process, you must ensure that the encoding is correct.Using this command will make the txt file readable by the FOR command later.

dir c:\temp | Out-file c:\test.txt -append -Encoding "default"
Leave a comment

Posted by on June 27, 2012 in Scripts, VBScripts



Using WMI to query HP blade Enclosure information

Recently, we had some issues with HP cSeries blade enclosures which requires me to gather the firmware information of the enclosure, blade iLO and blade ROM.

Using WMIExplorer I found that the WMI information is kept in the root\HPQ and got a quick and dirty script to query the information I need.

set objArg = WScript.Arguments
if objArg.Count >= 1 then
strComputer = objArg(0)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\HPQ")
Set colItems = objWMIService.ExecQuery _
    ("Select * from HP_BladeEnclosureCS")
For Each objItem in colItems
    Encl_Name = objItem.Name
    Encl_IP = objItem.ManagementIPAddress
    exit for
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_BladeEnclosureFW")
For Each objItem in colItems
    Encl_FW = objItem.VersionString
    exit for
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_SystemROMFirmware")
For Each objItem in colItems
    If InStr(objItem.Caption, "Active") > 0 then
      Blade_FW = objItem.VersionString
    end if 
    exit for
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_MPFirmware")
For Each objItem in colItems
    Blade_iLO_FW = objItem.VersionString
    exit for
Wscript.echo strcomputer & vbTab & Blade_FW & vbTab & Blade_iLO_FW _
  & vbTab & Encl_Name & vbTab & Encl_Ip & vbTab & Encl_FW
end if
Leave a comment

Posted by on May 28, 2012 in Scripts, VBScripts


Tags: ,

VBScript: Adding strings with comma into keys causes “The key is already associated…” error

These past few days as I am furiously pounding the keyboard trying to re-engineer someone else script using dictionary, I came across an expected problem:

I had an LDAP query against an OU to grab all user objects in that OU. As you know, depending on how your organize your AD OU, the DN of the user object usual looks like this (for example) “CN=Wong,\ Kelvin,OU=Users,OU=SGP,DC=MYCO,DC=COM”

So I have an array of DNs and want to use that to create a dictionary. So I did the followng:

Set d = CreateObject("Scripting.Dictionary")
for i=0 to ubound(arrDN)-1
  d.add arrDN(i), "X"

When this ran, it threw out “The key is already associated…” error. At first I thought I had duplicated DNs in the OU, but that is impossible in AD.

It turns out that the keys never got added because each comma in the DN string becomes a delimiter and nothing was added, plus the error was generated. It took me a while to figure this out because I was passing the  key string as a variable and did not expect it will interpret the commas.

So the proper way to create a dictionary if your key strings contains commas is close them with single or double quotes:

Set d = CreateObject("Scripting.Dictionary")
for i=0 to ubound(arrDN)-1
  d.add """" & arrDN(i) & """" , "X"

Now the issue here is that if you want to reference an item using the key or the key itself, you will need to add the quotes into your string also. For example

s = "CN=Wong,\ Kelvin,OU=Users,OU=SGP,DC=MYCO,DC=COM"
if d.Exists("""" & s & """") then wscript.echo "Found it!"
Leave a comment

Posted by on May 4, 2012 in Scripts, VBScripts


VBScript: comparing two files using dictionary object

Here is a VBScript function to compare 2 files and get the changes between them. The function will return a multi-line string with what was added, modified and deleted between the old and new file.

This function was initially developed for me to compare changes between an old and new input file in my script. I need to know which server was added or removed and which server had its parameters modified. The input files is a delimited file using “~” as a delimiter and contains 2 values in each line: “server name” & “parameter”. Read the rest of this entry »

Leave a comment

Posted by on May 6, 2011 in Scripts, VBScripts



Scripting Network configurations

I have the most difficult task of writing a script that will perform some post-setup configuration on Windows server that should do the following:

  1. Team required NICs and set the speed/duplex accordingly
  2. Set the IP configuration information for the NIC (teamed) on pubic LAN
  3. Set all NICs to “show icon in notification area”
  4. Disable NICs that are not in use
  5. Rename all NICs to based on our standard
  6. Unbind specific NICs from network protocols and services if not needed
  7. Set the binding order of the NICs

So far the results are pretty good, I have managed to get 1, 2, 4 and 5 completed. 3 is easy, just a bit more coding that I wanted. 6 and 7 are proofing to be an impossible task if you are not a C++ or VBS programmer.

The problem with admin scripting of Windows network is that there is no one consistent interface or provider in which you can perform the various actions. You may need to visit the network via WMI, the registry, explorer|control panel, netsh, vendor specific CLIs and even DDK programming, if you need to achieve all of the tasks above.

Anyway, I will post my scripts here as I get each components completed


I know there have been requests for the script. I had it using in my company to deploy severs over 2 years so I can say that it stable. However, my initial thought about sharing the code here is that its pretty monolithic, i.e ugly (because there was some time constraint in creating the script) and am somewhat embarrassed to post it in public in a place where so many programming and software engineering guru exists.

But again, I don’t know of anyone posting any equivalent type of script to share and as a person who believes strongly in sharing technical knowledge (amongst other things), I have decided bite the bullet to post the script here. Hopefully, it will help your productivity in your own work in time to come.

Please let me know what you don’t understand about the script and I will explain it to you. Ap0logise for the bad programming practise in the codes.

A bit about the logic of the script and only for HP servers:

  • Port 1 (embedded) teamed with port 1 of the first external NIC slot
  • Teamed NIC is set to FailOnFault as default
  • Port2 (embedded) is used for backup (TSM) at default to 100/full – Optional
  • Port 2 of the external NIC slot is used of cluster heartbeat annd default to 10/full – Only for clusters
  • All NICs unused are disabled

Here is the main scripts: SetNicConfig.wsf

Here is the function library: FuncUtils.vbs


Posted by on November 5, 2010 in Scripts, VBScripts, Windows