Solved

Improve vbscript

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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