VB script to check if a particular application is installed or not and write the output

madhusk
madhusk used Ask the Experts™
on
Hi Guys,

I managed to find the below script, which will check if a particular application is installed or not on a single computer, I have rewritten the script so that it can read the input from a text file. However it's throwing an error, loop expected in the last line. Can some one help me with this.

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const ForAppend = 8
 
On Error Resume Next
 
'Set variable required values
strLookFor = "User Profile Hive Cleanup Service"

'Read Text File
On Error Resume Next
dim objFSO
dim objFile

Const ForReading=1

strFile="C:\citrixservers.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.OpenTextFile(strFile,ForReading)

Do While objFile.AtEndOfStream<>True
strComputer = objFile.ReadLine

'strComputer = "."
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strEntry1a = "DisplayName"
strEntry1b = "DisplayVersion"
strTxtFile = "C:\checklog.txt"
 
Set objNetwork = CreateObject("WScript.Network")
currUser = objNetwork.UserName
currMachine = objNetwork.ComputerName
 
'Get all registry subkeys of strKey, loop through all 'DisplayName' keys and check if a match for strLookFor is found....
Set objReg = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
For Each strSubkey In arrSubkeys ' GO THROUGH ALL SUBKEYS
        objReg.GetStringValue HKLM, strKey & strSubkey, strEntry1a, strValue1 ' READ IN DisplayName as strValue1
        If Left(strValue1, Len(strLookFor)) = strLookFor Then ' If the name is a match...
        objReg.GetStringValue HKLM, strKey & strSubkey, strEntry1b, dispVer ' Read in DisplayVersion as dispVer
        boolFound = True
        Exit For
        End If        
Next
 
'Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strTxtFile, ForAppend, True)
 
'Continue to try to open the file until it isn't locked by someone else...
Do Until Err.Number <> 70
        Err.Clear
        Set objTextFile = objFSO.OpenTextFile(strTxtFile, ForAppend, True)
'WScript.Echo("In")
Loop
 
If boolFound = True Then
        txtMessage = "Machine: " & strComputer & ", has " & strLookFor & " version " & dispVer & " & installed!"
Else
        txtMessage = "Machine: " & strComputer & ", does not have " & strLookFor & " installed!"
End If
 
objTextFile.WriteLine(txtMessage)
objTextFile.Close
 
Set objNetwork = Nothing
'Set objFSO = Nothing
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you're opening "citrixservers.txt"

then you have 2 lines

Do While objFile.AtEndOfStream<>True
strComputer = objFile.ReadLine


This is the main loop for reading through citrixservers.txt

It has no corresponding "Loop" line to close the loop. I assume this should go here:

objTextFile.WriteLine(txtMessage)
objTextFile.Close
Loop

Author

Commented:
Perfect! Thanks for the help, Can you help me modify the script to get the install date aswell.
How about this.

the date will be in the format YYYYMMDD - as that's how it is stored in the registry.



Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const ForAppend = 8
 
On Error Resume Next
 
'Set variable required values
strLookFor = "User Profile Hive Cleanup Service"
'Read Text File
On Error Resume Next
dim objFSO
dim objFile

Const ForReading=1

strFile="C:\citrixservers.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.OpenTextFile(strFile,ForReading)

Do While objFile.AtEndOfStream<>True
strComputer = objFile.ReadLine

'strComputer = "."
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strEntry1a = "DisplayName"
strEntry1b = "DisplayVersion"
strTxtFile = "C:\checklog.txt"
 
Set objNetwork = CreateObject("WScript.Network")
currUser = objNetwork.UserName
currMachine = objNetwork.ComputerName
 
'Get all registry subkeys of strKey, loop through all 'DisplayName' keys and check if a match for strLookFor is found....
Set objReg = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
For Each strSubkey In arrSubkeys ' GO THROUGH ALL SUBKEYS
        objReg.GetStringValue HKLM, strKey & strSubkey, strEntry1a, strValue1 ' READ IN DisplayName as strValue1
        If Left(strValue1, Len(strLookFor)) = strLookFor Then ' If the name is a match...
        objReg.GetStringValue HKLM, strKey & strSubkey, strEntry1b, dispVer ' Read in DisplayVersion as dispVer
        objReg.GetStringValue HKLM, strKey & strSubkey, "InstallDate", dInstalled ' Read in Installdate as dInstalled
        boolFound = True
        Exit For
        End If        
Next
 
'Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strTxtFile, ForAppend, True) 
 
'Continue to try to open the file until it isn't locked by someone else...
Do Until Err.Number <> 70
        Err.Clear
        Set objTextFile = objFSO.OpenTextFile(strTxtFile, ForAppend, True)
'WScript.Echo("In")
Loop
 
If boolFound = True Then
        txtMessage = "Machine: " & strComputer & ", had " & strLookFor & " version " & dispVer & " & installed  on " & dInstalled &"!"
Else
        txtMessage = "Machine: " & strComputer & ", does not have " & strLookFor & " installed!"
End If
 
objTextFile.WriteLine(txtMessage)
objTextFile.Close
 Loop
Set objNetwork = Nothing
'Set objFSO = Nothing

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial