Showing posts with label VBS. Show all posts
Showing posts with label VBS. Show all posts

Saturday, September 6, 2008

Available drive letters

My colleague needed to run subst command and therefore was looking for available drive letters. During the script creating I found out that running simple InStr() function is slower then to use Dictionary object (maybe it's a tip for other blogpost). Anyway I used InStr() because I stopped the script after first match.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set WshNetwork = WScript.CreateObject("WScript.Network")
Computer = WshNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:\\" & Computer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
Disks = Disks & lcase(left(objItem.DeviceID,1))
Next

For I = 122 To 97 Step -1
Character = Chr(i)
If InStr (Disks, Character) Then
Else
Result = Character
Exit For
End If
Next

WScript.Echo "Available drive letter: " & Result

The script connects to WMI and query all available logical disks. Then is stores the drive letter in string named Disks. As the drive letter is stored in DeviceID in form C: I converted it to lowercase and remove the colon. After the whole WMI part the Disks looks like this: "cdehpvx".

Then in For loop I am going thru alphabet from the end to the beginning (to say it exactly - from 'z' to 'i') and after first non-match exit the loop and return the letter.

Sunday, August 31, 2008

Retrieve list of installed programs

"Hi Dave, I'd like to list all installed software to text file for later parsing. My final point is to check if any of VMware software is installed."

OK, no problem for me. You have a lot of ways how to do that. Let me show you some of them.

1) From WMI via VBS
In this case we use Win32_Product WMI class and search it for specific software name:

Set objWMIService = GetObject("winmgmts:\\VMMORAVEC\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Product WHERE name LIKE '%vmware%'", "WQL")

For Each objItem In colItems
WScript.Echo objItem.Name
Next

2) From WMI via wmic
This way is easy but takes long time. especially when we ran at as "runas" command it takes five minutes per computer which was not accepted by requestor. Anyway this is very useful method how retrieve data from WMI.

wmic product find /I "VMware"

3) From Registry via VBS
In this case we'll check registry but through WMI StdRegProv class.

Const HKLM = &H80000002
Const strBaseKey = "Software\Microsoft\Windows\CurrentVersion\Uninstall\"
Set objReg = GetObject("winmgmts://vmmoravec/root/default:StdRegProv")
objReg.EnumKey HKLM,strBaseKey,arrSubKeys

For Each strSubKey In arrSubKeys
intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "DisplayName", strValue)
if InStr(strValue, "VMware") > 0 Then
WScript.Echo strvalue
End If
Next


4) From Registry via reg.exe
The way we used for our case. Fast and easy (and showing only VMware software):

reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /s ¦ find /I "VMware" ¦ find /I "DisplayName"

Output:

DisplayName REG_SZ VMware Infrastructure Client 2.5
DisplayName REG_SZ VMware Workstation
DisplayName REG_SZ VMware VI Toolkit (for Windows)

5) Do you want PowerShell version?
In PowerShell just use Get-WmiObject and pipe it like this:

Get-WmiObject Win32_Product ¦ Where {$_.name -match "vmware"} ¦ fl name

Conclusion
It's up to you which method you'd like to use. WMI is very powerful but slow and registry is sometimes hard to parse. As I mentioned before: for this case we used reg.exe because it fits perfectly our needs. Of course for other type of work you will choose different method.

Useful links