Skip to content

Script for clearing last pxe advertisement in SCCM

Featured Replies

Thought I would share this script with you guys.

 

It´s a script for removing pxe advertisement from computers. All you need to do is to run the script and enter the computername you would like to clear.

 

Much easier than going into the console, finding the correct collection, rightclicking...

 

REM Code written by WMMAYMS - 2010

SiteServer = "<SITESERVERNAME>"

provSiteCode = "<SITECODE>"

 

strComputer = Inputbox("Enter Computer Name to the computer that you would like to reset","Reseting PXE")

If Len(strComputer) = 0 Then

'do nothing

Else

 

Set objLocator = CreateObject("WbemScripting.SWbemLocator")

Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode)

'get the resource ID of the computer

intResourceID = GetResourceID(strComputer)

end if

Function GetResourceID(strComputerName)

Set colResourceIDs = objSMS.ExecQuery _

("select ResourceID from SMS_R_System where Name = '" & _

strComputer & "'")

for each objResID in colResourceIDs

GetResourceID = objResID.ResourceID

next

End Function

 

'Remove pxe adv from computer with intResourceID as resourceID

set conexion = Connect(SiteServer, "", "")

ClearPxeAdvertisementResource conexion, intResourceID

Function Connect(server, userName, userPassword)

 

On Error Resume Next

 

Dim net

Dim localConnection

Dim swbemLocator

Dim swbemServices

Dim providerLoc

Dim location

 

Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")

swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy

 

' If the server is local, don't supply credentials.

Set net = CreateObject("WScript.NetWork")

If UCase(net.ComputerName) = UCase(server) Then

localConnection = true

userName = ""

userPassword = ""

server = "."

End If

 

' Connect to the server.

Set swbemServices= swbemLocator.ConnectServer _

(server, "root\sms",userName,userPassword)

If Err.Number<>0 Then

Wscript.Echo "Couldn't connect: " + Err.Description

Connect = null

Exit Function

End If

 

' Determine where the provider is and connect.

Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")

For Each location In providerLoc

If location.ProviderForLocalSite = True Then

Set swbemServices = swbemLocator.ConnectServer _

(location.Machine, "root\sms\site_" + _

location.SiteCode,userName,userPassword)

If Err.Number<>0 Then

Wscript.Echo "Couldn't connect:" + Err.Description

Connect = Null

Exit Function

End If

Set Connect = swbemServices

Exit Function

End If

Next

Set Connect = null ' Failed to connect.

End Function

 

Sub ClearPxeAdvertisementResource(connection,resourceID)

On Error Resume Next

Dim resources

Dim InParams

 

' Set up the Resource array parameter.

resources = Array(1)

resources(0) = resourceID

 

Set InParams = connection.Get("SMS_Collection").Methods_("ClearLastNBSAdvForMachines").InParameters.SpawnInstance_

InParams.ResourceIDs = resources

connection.ExecMethod "SMS_Collection","ClearLastNBSAdvForMachines", InParams

 

if Err.number <> 0 Then

WScript.Echo "Failed to clear PXE advertisement for resource: " & resourceID

Exit Sub

End If

End Sub

 

 

Save everything in red as a .vbs file and then edit the first to lines "Siteserver" and "provSiteCode" to match your environment.

 

Note that you still need the correct sccm permissions to be able to do this..

 

Cheers

  • Author

nice script, what does it do exactly ? edit the db directly or ?

 

It´s built from the SCCM SDK. It clears the PXE flag for a single computer in the same way that the console does it, only faster.

 

When you launch the script you will get a prompt asking for a computername. Just write the computername of the computer you wish to clear, press OK and then try reinstalling it again =)

 

 

It is also possible to modify the script to work on collection instead of computer objects.

  • 2 months later...

Is it possible to modify the script that i can run it directly in the end of an osd task sequence with no user interaction? (e.g. enter computername)

  • Author

Is it possible to modify the script that i can run it directly in the end of an osd task sequence with no user interaction? (e.g. enter computername)

 

 

Yes this should not be a problem.

If you replace the green part below with this:

 

strComputer = Wscript.Arguments.Item(0)

If Len(strComputer) = 0 Then

'do nothing

Else

That would make the script accept an argument instead of requiring a user input.

To run the script you would do something like this: C:\temp\clearpxescript.vbs COMPUTER1

 

This would reset the PXE flag for COMPUTER1

 

To make it dynamic you could use a task sequence variable containing the computername of the computer that you are deploying to.

 

 

Original code:

REM Code written by WMMAYMS - 2010

SiteServer = "<SITESERVERNAME>"

provSiteCode = "<SITECODE>"

strComputer = Inputbox("Enter Computer Name to the computer that you would like to reset","Reseting PXE")

If Len(strComputer) = 0 Then

'do nothing

Else

 

Set objLocator = CreateObject("WbemScripting.SWbemLocator")

Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode)

'get the resource ID of the computer

intResourceID = GetResourceID(strComputer)

end if

Function GetResourceID(strComputerName)

Set colResourceIDs = objSMS.ExecQuery _

("select ResourceID from SMS_R_System where Name = '" & _

strComputer & "'")

for each objResID in colResourceIDs

GetResourceID = objResID.ResourceID

next

End Function

 

'Remove pxe adv from computer with intResourceID as resourceID

set conexion = Connect(SiteServer, "", "")

ClearPxeAdvertisementResource conexion, intResourceID

Function Connect(server, userName, userPassword)

 

On Error Resume Next

 

Dim net

Dim localConnection

Dim swbemLocator

Dim swbemServices

Dim providerLoc

Dim location

 

Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")

swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy

 

' If the server is local, don't supply credentials.

Set net = CreateObject("WScript.NetWork")

If UCase(net.ComputerName) = UCase(server) Then

localConnection = true

userName = ""

userPassword = ""

server = "."

End If

 

' Connect to the server.

Set swbemServices= swbemLocator.ConnectServer _

(server, "root\sms",userName,userPassword)

If Err.Number<>0 Then

Wscript.Echo "Couldn't connect: " + Err.Description

Connect = null

Exit Function

End If

 

' Determine where the provider is and connect.

Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")

For Each location In providerLoc

If location.ProviderForLocalSite = True Then

Set swbemServices = swbemLocator.ConnectServer _

(location.Machine, "root\sms\site_" + _

location.SiteCode,userName,userPassword)

If Err.Number<>0 Then

Wscript.Echo "Couldn't connect:" + Err.Description

Connect = Null

Exit Function

End If

Set Connect = swbemServices

Exit Function

End If

Next

Set Connect = null ' Failed to connect.

End Function

 

Sub ClearPxeAdvertisementResource(connection,resourceID)

On Error Resume Next

Dim resources

Dim InParams

 

' Set up the Resource array parameter.

resources = Array(1)

resources(0) = resourceID

 

Set InParams = connection.Get("SMS_Collection").Methods_("ClearLastNBSAdvForMachines").InParameters.SpawnInstance_

InParams.ResourceIDs = resources

connection.ExecMethod "SMS_Collection","ClearLastNBSAdvForMachines", InParams

 

if Err.number <> 0 Then

WScript.Echo "Failed to clear PXE advertisement for resource: " & resourceID

Exit Sub

End If

End Sub

  • 1 year later...

Hi,

 

I want to schedule a task to clear the last PXE advertisement to a whole collection. Can that be done using your script?

 

Thanks,

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.