Wednesday, April 24, 2013

Clear out the Unprovisioned Collection in 2007

Do you have issues with unprovisioned machines that stick around.  Here is some code to help you get rid of the unprovisioned machines.  This can be run at differnet intervals during the day or week depending on when you see it.  This is old that is in Vbscript.  Hopefully I can get going in powershell and get with the program.  :)

Dim Args 
Dim swbemLocator, SWbemServices, objCollection, oProviderLocation, oLocation  
Dim strComputerName, arrComputers, objComputer, sCollectionIDs
Dim objDirectRule
Dim strmessage, objshell

'CollectionIDs from which to remove the computer
sCollectionIDs = "ABC0019D"

'Main script
set objShell = CreateObject("WScript.Shell")
    Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
    swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.
    Set swbemServices = swbemLocator.ConnectServer(".", "root\SMS")
    Set oProviderLocation = swbemServices.InstancesOf("SMS_ProviderLocation")
    For Each oLocation In oProviderLocation
        If oLocation.ProviderForLocalSite = True Then
            Set swbemServices = swbemLocator.ConnectServer(oLocation.Machine, "root\sms\site_" + oLocation.SiteCode)
        End If       
Set arrComputers = SWbemServices.ExecQuery("select * from SMS_R_System WHERE Unknown=1")
For Each objComputer In arrComputers
   'DElete special..this will delete the machine from the system
Set SWbemServices = Nothing
Set SWbemLocator = Nothing