?
Solved

Improve vbscript

Posted on 2014-09-08
1
Medium Priority
?
278 Views
Last Modified: 2014-09-13
i write a vbscript that base on OS install Pre Requirement Application and i want to know if i can improve this script
BAClient-TSM1.vbs
0
Comment
Question by:David Regev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 40311331
Hi, I can't really see anything wrong with that script, other than HKEY_LOCAL_MACHINE not being defined, so the registry queries probably didn't work right.

On Error Resume Next
'================================================================================================
'** GENERAL VARIABLE DECLARATIONS
'================================================================================================
Const HKLM = &H80000002
Const HKEY_LOCAL_MACHINE = &H80000002
Dim objWMI, strComputerName ,intBitWidth ,strOSVersion,strOSVersion1 ,objShell ,SrtDriverLetter ,Carmel ,MapNetDrive
strComputerName = "."
intBitWidth = getBitWidth(strComputerName)
StrOSVersion = getOSVersion(strComputerName)
StrOSVersion1 = strOSVersion & intBitWidth
Set objWMI = GetObject ("winmgmts:")
Set colServices = objWMI.ExecQuery("Select * From Win32_Service Where PathName Like '%dsmcsvc.exe%'")
Set objShell = CreateObject ("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objEnv = objShell.Environment("PROCESS")
SrtDriverLetter = "x:"
Carmel = "\\crnt13\install$\Programs Install\Servers\TSM\Prerequisites"
MapNetDrive = SrtDriverLetter & Carmel 'Prerequirement Application
'================================================================================================
Visual2010X86 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}"
Visual2010X64 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1D8E6291-B0D5-35EC-8441-6616F567A0F7}"
Visual2012X86 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{6C772996-BFF3-3C8C-860B-B3D48FF05D65}"
Visual2012X64 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{3C28BFD4-90C7-3138-87EF-418DC16E9598}"
HotFixID2K8R2 = "2680097"
status = CheckParticularHotfix(strComputerName, HotFixID2K8R2)
'================================================================================================
' Check if services Exist , if the services Exist this is Upgrade Case if not its Clean Install
'================================================================================================
objEnv("SEE_MASK_NOZONECHECKS") = 1
For Each objService In colServices
	If objService.Started Then
		objService.StopService
	End If
Next
'================================================================================================
' Map Network Folder
'================================================================================================
If FSO.DriveExists ("x:") = False Then
	objNetwork.MapNetworkDrive SrtDriverLetter ,Carmel
Else
	objNetwork.RemoveNetworkDrive SrtDriverLetter
	wscript.Sleep 1000
	objNetwork.MapNetworkDrive SrtDriverLetter ,Carmel
End If
'=============================================================================================
'Windows 2008R2 Or Windows 2008 x64 Prerequisites
'=============================================================================================
If (instr(strOSVersion1,"6.1" Or "6.064")) Then
	If KeyExists(HKLM, Visual2010X86) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x86_2010.exe /q /norestart",0,1
	End If
	If KeyExists(HKLM, Visual2010X64) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x64_2010.exe /q /norestart",0,1
	End If
	If KeyExists(HKLM, Visual2012X86) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x86_2012.exe /q /norestart",0,1
	End If
	If KeyExists(HKLM, Visual2012X64) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x64_2012.exe /q /norestart",0,1
	End If
	If status = False Then
		objShell.Run SrtDriverLetter & "\Windows6.1-KB2680097-v2-x64.msu /quiet /norestart",0,1
	End If
End If
'=============================================================================================
'Windows 2008 32Bit
'=============================================================================================
If (InStr(strOSVersion1,"6.032")) Then
	If KeyExists(HKLM, Visual2010X86) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x86_2010.exe /q /norestart",0,1
	End If
	If KeyExists(HKLM, Visual2012X86) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x86_2012.exe /q /norestart",0,1
	End If
End If
'=============================================================================================
'Windows 2003 32&64Bit Prerequisites
'=============================================================================================
If (InStr(strOSVersion1,"5.2")) Then
	If (instr(intBitWidth,"64")) Then
		If KeyExists(HKLM, Visual2010X86) = False Then
			objShell.Run SrtDriverLetter & "\vcredist_x86_2010.exe /q /norestart",0,1
		End If
		If KeyExists(HKLM, Visual2010X64) = False Then
			objShell.Run SrtDriverLetter & "\vcredist_x64_2010.exe /q /norestart",0,1
		End If
	End If
Else' Win2k3_X86
	If KeyExists(HKLM, Visual2010X86) = False Then
		objShell.Run SrtDriverLetter & "\vcredist_x86_2010.exe /q /norestart",0,1
	End If
End If
'=============================================================================================
'End Installation
'=============================================================================================
If FSO.DriveExists ("x:") = True Then
	objNetwork.RemoveNetworkDrive SrtDriverLetter
End If
For Each objService In colServices
	If objService.StopService Then
		objService.StartService
	End If
Next
objEnv.Remove("SEE_MASK_NOZONECHECKS")
Set objWMI = Nothing
Set objShell = Nothing
Set objNetwork = Nothing
Set fso = Nothing
Set objEnv = Nothing

Function getBitWidth(strComputerName)
	Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\default:StdRegProv")
	strKeyPath = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
	strValueName = "Identifier"
	objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
	If (InStr(strValue,"64")) Then
		getBitWidth = 64
	Else
		getBitWidth = 32
	End If
End Function

Function getOSVersion(strComputerName)
	Set objWMI = GetObject("winmgmts://" & strComputerName & "/root/cimv2")
	Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
	For Each objItem In colItems
		strOSVersion = Left(objItem.Version, 3)
	Next
	getOSVersion = strOSVersion
End Function

Function KeyExists(Key, KeyPath)
	Set objReg = GetObject("winmgmts:!root/default:StdRegProv")
	If objReg.EnumKey(Key, KeyPath, arrSubKeys) = 0 Then
		KeyExists = True
	Else
		KeyExists = False
	End If
End Function

Function CheckParticularHotfix(strPuter, strHotfixID)
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPuter & "\root\cimv2")
	If err.number <> 0 Then
		CheckParticularHotfix = "WMI could not connect to computer '" & strPuter & "'"
		exit function 'No reason to continue
	end if
	strWMIforesp = "Select * from Win32_QuickFixEngineering where HotFixID = 'Q" & strHotfixID & "' OR HotFixID = 'KB" & strHotfixID & "'"
	Set colQuickFixes = objWMIService.ExecQuery (strWMIforesp)
	If err.number <> 0 Then 'if an error occurs
		CheckParticularHotfix = "Unable to get WMI hotfix info"
	Else 
		'Error number 0 meaning no error occured
		tal = colQuickFixes.count
		If tal > 0 Then
			CheckParticularHotfix = True 'HF installed
		Else
			CheckParticularHotfix = False 'HF not installed
		End If
	End If
	Set colQuickFixes = Nothing
	Err.Clear
	On Error GoTo 0
End Function

Open in new window


You probably could have used UNC paths to the vcredist files instead of mapping a drive, but that's a minor issue.

Regards,

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question