locked
Delete client record from database script RRS feed

  • Question

  • Does anyone have or can help create a script that will delete a client record from the SCCM database remotely. The script will run on the client to be deleted. I want to eventually add this script to the start of a HTA that kicks off a build TS.

    I have one that runs and works locally on the SCCM server but this requires me to change the computer name every time. I want it to grab the resource ID of the computer it's running on and also be able to run in HTA so no Wscript.

    Here's a script I got from the SMS 2003 Recipe's book

    strSMSServer = <SMSServer>
    
    strComputer = "Computer1"
    
    Set objLoc =  CreateObject("WbemScripting.SWbemLocator")
    Set objSMS= objLoc.ConnectServer(strSMSServer, "root\sms")
    Set Results = objSMS.ExecQuery _
        ("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")
    For each Loc in Results
        If Loc.ProviderForLocalSite = True Then
            Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & _
                Loc.SiteCode)
            strSMSSiteCode = Loc.SiteCode
        end if
    Next
    
    'get the resource ID of the computer
    intResourceID = GetResourceID(strComputer)
    
    'Remove ResourceID
    Set objResource = GetObject( "WinMgmts:\\" & strSMSServer & _
        "\root\SMS\site_" & strSMSSiteCode & _
        ":SMS_R_System.ResourceID=" & cint(intResourceID))
    objResource.Delete_
    wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
    
    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


    Any help would be greatly appreciated.



    Friday, January 15, 2010 2:37 PM

Answers

  • Fixed it myself. Here's the code if anyone is interested (I was missing '\' before root on line 19:-

    '***************************************
    '   VBSCRIPT to DELETE CLIENT RECORD
    '***************************************
    
    SiteServer = "SiteServer"
    provSiteCode = "SiteCode"
    GetProviderAccount = "Username"
    GetProviderPassword = "password"
    
    strComputer = "computer"
    
    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode, GetProviderAccount, GetProviderPassword)
    
    'get the resource ID of the computer
    intResourceID = GetResourceID(strComputer)
    
    'Remove ResourceID
    Set objResource = GetObject( "WinMgmts:\\" & SiteServer & "\root\SMS\site_" & provSiteCode & ":SMS_R_System.ResourceID=" & cint (intResourceID))
    objResource.Delete_
    wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
    
    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
    • Marked as answer by PaulTown Friday, January 15, 2010 4:35 PM
    Friday, January 15, 2010 4:33 PM

All replies

  • This is what I have so far but it's failing when setting the object 'objResource'.

    Line: 19
    Char: 1
    Error: The remote server machine does not exist or is unavailable: 'GetObject'
    Code: 800A01CE
    Source: Microsoft VBScript runtime error


    '***************************************
    '   VBSCRIPT to DELETE CLIENT RECORD
    '***************************************
    
    SiteServer = "Server"
    provSiteCode = "SiteCode"
    GetProviderAccount = "username"
    GetProviderPassword = "password"
    
    strComputer = "computer"
    
    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode, GetProviderAccount, GetProviderPassword)
    
    'get the resource ID of the computer
    intResourceID = GetResourceID(strComputer)
    
    'Remove ResourceID
    Set objResource = GetObject( "WinMgmts:\\" & SiteServer & "root\SMS\site_" & provSiteCode & ":SMS_R_System.ResourceID=" & cint(intResourceID))
    objResource.Delete_
    wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
    
    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
    • Edited by PaulTown Friday, January 15, 2010 4:03 PM Code change
    Friday, January 15, 2010 3:44 PM
  • Fixed it myself. Here's the code if anyone is interested (I was missing '\' before root on line 19:-

    '***************************************
    '   VBSCRIPT to DELETE CLIENT RECORD
    '***************************************
    
    SiteServer = "SiteServer"
    provSiteCode = "SiteCode"
    GetProviderAccount = "Username"
    GetProviderPassword = "password"
    
    strComputer = "computer"
    
    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode, GetProviderAccount, GetProviderPassword)
    
    'get the resource ID of the computer
    intResourceID = GetResourceID(strComputer)
    
    'Remove ResourceID
    Set objResource = GetObject( "WinMgmts:\\" & SiteServer & "\root\SMS\site_" & provSiteCode & ":SMS_R_System.ResourceID=" & cint (intResourceID))
    objResource.Delete_
    wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
    
    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
    • Marked as answer by PaulTown Friday, January 15, 2010 4:35 PM
    Friday, January 15, 2010 4:33 PM
  • Set
     objResource = GetObject( "WinMgmts:\\"
     & SiteServer & "\root\SMS\site_"
     & provSiteCode & ":SMS_R_System.ResourceID="
     & cint
     (intResourceID))

    I find this "& cint (intResourceID))" fails (sometimes) on my SMS 2003 box.  Changing it to "& cstr (intResourceID))" fixes my issue.

    (Set objResource fails with an Err.No = 6, Overflow).

    Which makes sense, when the ResourceID being returned is > 32768.

    I've written about my experiences here

    Tuesday, March 30, 2010 8:27 AM
  • thank you, it worked!!!
    Tuesday, August 19, 2014 7:18 AM