widsnet.com
6Oct/110

Stop install on uncertified hardware

This is a script to abort the OS deployment during the task sequence if there is no Driver Package referenced in the Task Sequence that matches the Computer Model.

Uses variables "queryWMIUser" and "queryWMIPassword" that must be set on an appropriate collection. Preferably the queryWMIPassword variable is set to "Do not display this value in the ConfigMgr console"

The "queryWMIUser" must of course have the right to query WMI on all sites.

On Error Resume Next
Set oTSenv = CreateObject("Microsoft.SMS.TSEnvironment") 
strSiteServer = oTSenv("_SMSTSMP")
strTaskSequence = oTSenv("_SMSTSPackageID")
strMake = oTSenv("Make")
strModel = oTSenv("Model")
strUser = oTSenv("queryWMIUser")
strPassword = oTSenv("queryWMIPassword")
strTSStep = oTSenv("_SMSTSNextInstructionPointer") -1

If strTaskSequence = "" Then
	WScript.Echo "Unable to get variables from the task sequence, exiting!"
	WScript.Quit 0
End if

WScript.Echo "SiteServer variable: " & strSiteServer
WScript.Echo "TaskSequence variable: " & strTaskSequence
WScript.Echo "Make variable: " & strMake
WScript.Echo "Model variable: " & strModel
WScript.Echo "Query User: " & strUser
WScript.Echo "Query Password: ****"

If oTSenv("isVM") Then
	WScript.Echo "This is a virtual machine"
	WScript.Echo "No hardware check will be performed"
	WScript.Quit 0
End If

Set regEx = New RegExp
regEx.Pattern = "\(.+\)"
regEx.IgnoreCase = True
strModel = regEx.Replace(strModel, "")

regEx.Pattern = "(.*\d+).*$"
strModel = regEx.Replace(strModel, "$1")

WScript.Echo "Cleaned name: " & strModel
WScript.Echo ""

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objLocator.ConnectServer(strSiteServer, "root\sms", strUser, strPassword)
Set colItems = objWMIService.ExecQuery("SELECT * FROM SMS_ProviderLocation")
For Each objItem in colItems
	WScript.Echo "Site Code: " & objItem.SiteCode
	strSitecode = objItem.SiteCode
	WScript.Echo "Namespace Path: " & objItem.NamespacePath
Next

strWQL = "SELECT * FROM SMS_TaskSequenceReferencesInfo WHERE PackageID='" & strTaskSequence & "' AND ReferencePackageType=3 and ReferenceName LIKE '%" & strModel & "%'"
WScript.Echo "WQL Search string: " & strWQL
WScript.Echo ""

Set objWMIService = objLocator.ConnectServer(strSiteServer, "root\sms\site_" & strSitecode, strUser, strPassword)
Set colItems = objWMIService.ExecQuery(strWQL)
If colItems.Count > 0 Then
	For Each objItem in colItems
		WScript.Echo "Matching Driver package found"
		WScript.Echo "Driver Package Name: " & objItem.ReferenceName
		WScript.Echo "Driver Package ID: " & objItem.ReferencePackageID
		WScript.Echo "Running on certified hardware"
		WScript.Echo ""
	Next
Else
	strErr = "Hardware not certified!" & vbCrLf & _
		"Computer Model: " & strModel & vbCrLf & _
		"Model does not match any Driver Package Name assigned to this TS" & vbCrLf & _
		"Task Sequence: " & strTaskSequence & vbCrLf & _
		"Failure step: " & strTSStep
	WScript.Echo strErr
	MsgBox strErr, vbOKOnly + vbCritical +vbSystemModal, "Important Information"
	WScript.Quit 1625 ' This installation is forbidden by system policy. Contact your system administrator.
End If