Solved

Improve vbscript

Posted on 2014-09-08
1
236 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
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
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

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now