Option
Explicit
Dim
sProviderServer
Dim
sSiteCode
Dim
sNamespace
Dim
sUsername
Dim
sPassword
Dim
sCollection
sProviderServer =
""
sSiteCode =
"ABC"
sNamespace =
"root\sms\site_"
& sSiteCode
sUsername =
""
sPassword =
""
sCollection =
"ABC00004"
ZTIProcess
Function
ZTIProcess
Dim
sMacAddress
Dim
sIPAddress
Dim
sUUID
Dim
sNetBiosName
Dim
oLocator
Dim
oSMS
Dim
sQuery
Dim
bFound
Dim
iResourceID
Dim
re
Dim
oSite
Dim
oParams
Dim
oResult
Dim
oLastError
Dim
oClients
Dim
oClient
Dim
oCollection
Dim
oNewRule
Dim
oAdvertisement
Dim
bTrying
Dim
sAdvert
Dim
i
sMacAddress = PXE.MacAddress
sIPAddress = PXE.IPAddress
sUUID = PXE.UUID
Set
re =
New
RegExp
re.Pattern =
":"
re.Global = true
sNetBiosName =
"MAC"
& re.Replace(sMacAddress,
""
)
If
sUUID =
"00000000-0000-0000-0000-000000000000"
or sUUID =
"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
then
sUUID =
""
End
if
PXE.LogTrace
"Processing request from MAC address = "
& sMacAddress &
", IP address = "
& sIPAddress &
", UUID = "
& sUUID
If
PXE.IPAddress =
"127.0.0.1"
or PXE.MacAddress =
"FF:FF:FF:FF:FF:FF"
then
PXE.LogTrace
"Ignoring ConfigMgr ping request"
Exit
Function
End
if
Set
oLocator = CreateObject(
"WbemScripting.SWbemLocator"
)
Set
oSMS = oLocator.ConnectServer(sProviderServer, sNamespace, sUsername, sPassword)
sQuery =
"SELECT * FROM SMS_R_System WHERE MacAddresses = '"
& sMacAddress &
"'"
If
sUUID <>
""
then
sQuery = sQuery &
" OR SMBIOSGUID = '"
& sUUID &
"'"
End
if
bFound =
False
Set
oClients = oSMS.ExecQuery(sQuery)
For
each oClient in oClients
bFound = true
iResourceID = oClient.ResourceID
PXE.LogTrace
"Found existing machine "
& oClient.NetbiosName &
" with ResourceID = "
& iResourceID
sNetBiosName = oClient.NetbiosName
Exit
For
Next
If
not bFound then
PXE.LogTrace
"Could not find machine with MAC address '"
& sMacAddress &
"' or SMBIOS UUID '"
& sUUID &
"'."
Set
oSite = oSMS.
Get
(
"SMS_Site"
)
Set
oParams = oSite.Methods_.Item(
"ImportMachineEntry"
).inParameters.SpawnInstance_()
oParams.NetbiosName = sNetBiosName
oParams.SMBIOSGUID = sUUID
oParams.MACAddress = sMacAddress
oParams.OverwriteExistingRecord = false
On
Error
Resume
Next
Set
oResult = oSite.ExecMethod_(
"ImportMachineEntry"
, oParams)
If
Err then
PXE.LogTrace
"Error importing machine entry: "
& Err.Description &
" ("
& Err.Number &
")"
Set
oLastError = CreateObject(
"WbemScripting.SWbemLastError"
)
PXE.LogTrace
"Last WMI error: "
& oLastError.Description
Exit
Function
End
if
On
Error
Goto 0
iResourceID = oResult.ResourceID
PXE.LogTrace
"Added new machine with ResourceID = "
& iResourceID
End
if
If
bFound then
sQuery =
"SELECT * FROM SMS_CM_RES_COLL_"
& sCollection &
" WHERE ResourceID = "
& iResourceId
bFound =
False
Set
oClients = oSMS.ExecQuery(sQuery)
For
each oClient in oClients
bFound = true
PXE.LogTrace
"Machine is already in collection "
& sCollection
Exit
For
Next
End
If
If
not bFound then
On
Error
Resume
Next
Set
oCollection = oSMS.
Get
(
"SMS_Collection.CollectionID='"
& sCollection &
"'"
)
If
Err then
PXE.LogTrace
"Error retrieving collection "
& sCollection &
": "
& Err.Description &
" ("
& Err.Number &
")"
Set
oLastError = CreateObject(
"WbemScripting.SWbemLastError"
)
PXE.LogTrace
"Last WMI error: "
& oLastError.Description
Exit
Function
End
if
On
Error
Goto 0
Set
oNewRule = oSMS.
Get
(
"SMS_CollectionRuleDirect"
).SpawnInstance_()
oNewRule.ResourceClassName =
"SMS_R_System"
oNewRule.RuleName = sNetBiosName
oNewRule.ResourceID = iResourceID
On
Error
Resume
Next
oCollection.AddMembershipRule oNewRule
If
Err then
PXE.LogTrace
"Error adding membership rule to "
& sCollection &
": "
& Err.Description &
" ("
& Err.Number &
")"
Set
oLastError = CreateObject(
"WbemScripting.SWbemLastError"
)
PXE.LogTrace
"Last WMI error: "
& oLastError.Description
Exit
Function
End
if
On
Error
Goto 0
PXE.LogTrace
"Added new membership rule to collection "
& sCollection
End
if
Set
oAdvertisement = oSMS.
Get
(
"SMS_Advertisement"
)
i = 0
bTrying =
True
Do
While
bTrying
Set
oParams = oAdvertisement.Methods_(
"GetAdvertisements"
).inParameters.SpawnInstance_()
oParams.ResourceID = iResourceID
Set
oResult = oAdvertisement.ExecMethod_(
"GetAdvertisements"
, oParams)
For
each sAdvert in oResult.AdvertisementIDs
PXE.LogTrace
"Found advertisement "
& sAdvert
bTrying =
False
Next
i = i + 1
If
bTrying and i > 10 then
PXE.LogTrace
"Giving up after 30 seconds of checking for new advertisements."
bTrying =
False
End
if
PXE.Sleep 3000
Loop
PXE.LogTrace
"Exiting PXEFilter.vbs"
End
Function