Solved

Improve vbscript

Posted on 2014-09-08
1
247 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

776 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