ConfigMgr 2007 and clearing a Computers’ Last PXE Advertisement by script

In a previous post I showed a script to remove a computer from a collection. This post will be an add-on to that previous post. As we are removing the computer from the collection anyway, we can as well perform a Clear Last PXE Advertisement –action. By doing this, it’s not necessary to perform a manual action the next time the computer needs to be re-imaged.

ClrLstPXEAdvAn easy way to do this is to run a script at the end of a Task Sequence that will clear the last PXE Advertisement. This makes sure that a computer can get re-imaged as soon it gets added to the correct collection. For this you can use the script from this post.

The usage of this script is cscript <ScriptName>.vbs /ComputerName:[ComputerName]. Keep in mind that it needs to be run with an account that has enough rights in ConfigMgr. See also this picture for an example.

Option Explicit

DIM objSWbemLocator, objSWbemServices, ProviderLocation, Location, Connection
DIM colResourceID, objResourceID, iResourceID, aResources, InParams
DIM sComputerName, sSiteServerName, objArguments

sSiteServerName = “<SiteServerName>”

‘ Check arguments
Set objArguments = Wscript.Arguments
If WScript.Arguments.Count = 1 Then
   sComputerName = objArguments.Named.Item(“ComputerName”)
   Wscript.Echo “Usage: ClearPxeAdvertisement.vbs /ComputerName:[ComputerName]”
End If

‘ MAIN Script
Set Connection = ConnectToSMSProvider(sSiteServerName)
iResourceID = GetResourceID(Connection, sComputerName)

aResources = Array(1)
aResources(0) = iResourceID

Set InParams = Connection.Get(“SMS_Collection”).Methods_(“ClearLastNBSAdvForMachines”).InParameters.SpawnInstance_
InParams.ResourceIDs = aResources

Connection.ExecMethod “SMS_Collection”,”ClearLastNBSAdvForMachines”, InParams
WScript.Echo “Cleared PXE advertisement for resource: ” & iResourceID

‘ Function to RETURN a Connection to the SMS Provider
Function ConnectToSMSProvider(ServerName)
   Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
   Set objSWbemServices = objSWbemLocator.ConnectServer(ServerName, “root\sms”)
   Set ProviderLocation = objSWbemServices.InstancesOf(“SMS_ProviderLocation”)
   For Each Location In ProviderLocation
      If Location.ProviderForLocalSite = True Then
         Set objSWbemServices = objSWbemLocator.ConnectServer(Location.Machine, “root\sms\site_” + Location.SiteCode)
         Set ConnectToSMSProvider = objSWbemServices
      End If
End Function

‘ Function to RETURN a ResourceID by a ComputerName
Function GetResourceID(Connection, ComputerName)
   Set colResourceID = Connection.ExecQuery(“Select ResourceID from SMS_R_System where Name = ‘” & ComputerName & “‘”)
   For Each objResourceID in colResourceID
      GetResourceID = objResourceID.ResourceID
End Function


There also exists an (basic) example on MSDN about How to Clear a PXE Advertisement for a Configuration Manager Resource, here:


3 thoughts on “ConfigMgr 2007 and clearing a Computers’ Last PXE Advertisement by script

  1. Hello,

    I’ve tested this solution at the and of my OSD task sequence but it seems not to suppress the PXE flag however my return logs are fine.
    I get the right resourceID and the function to clear the flag returns success but I can’t make PXE boot after my computer is built.
    Do you have any idea ?


  2. Hello

    I’ve solved my problem, it was due to the fact I haven’t published any advertisement on my new computer….


Leave a Comment