Solved

Improve vbscript

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

749 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