Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3677
  • Last Modified:

How can I delete a computer from SCCM using VB.NET (2010)

Hi there,

I would like to delete a computer from SCCM (completely delete the record) using VB.NET (2010).  Currently, I use a VB Script and pass arguments to it.  Can anyone tell me how to put this in VB.NET 2010 please?  VBS attached.

Thanks :)
Set args = WScript.Arguments

strSMSServer = args.Item(0)
strSMSSiteCode = args.Item(1)
strUserName =  args.Item(2)
strPassword =  args.Item(3)
strComputer = args.Item(4)

Set lLocator = CreateObject("WbemScripting.SWbemLocator")  
Set gService = lLocator.ConnectServer(strSMSServer,"root\sms\site_" & strSMSSiteCode, strUserName, strPassword)  
Set ResIDs = gService.ExecQuery("SELECT * from SMS_R_System where NetbiosName = '" & strComputer & "'")  

for each oResID in ResIDs
 	oResID.Delete_
Next
Wscript.Quit

Open in new window

0
jamie77777
Asked:
jamie77777
  • 3
  • 2
1 Solution
 
merowingerCommented:
Hi, i've created a very simple example for you. There no much error handling in, but i think it's a good start.
I've solved it with Textboxes where you've to define you SCCM Site, the credentials and the pc to delete form the database
Untitled.png
0
 
merowingerCommented:
And here's the matching vb.net code for this:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strComputer = TextBox1.Text
        Dim strSMSServer = TextBox2.Text
        Dim strSMSSiteCode = TextBox3.Text
        Dim strUserName = TextBox4.Text
        Dim strPassword = TextBox5.Text
        Dim ResIDs

        Try
            Dim lLocator = CreateObject("WbemScripting.SWbemLocator")
            Dim gService = lLocator.ConnectServer(strSMSServer, "root\sms\site_" & strSMSSiteCode, strUserName, strPassword)
            ResIDs = gService.ExecQuery("SELECT * from SMS_R_System where NetbiosName = '" & strComputer & "'")

        Catch ex As Exception
            MsgBox("Could not connect to SCCM Server - ErrorDescription:" & Err.Description)
            End
        End Try

        Try
            For Each oResID In ResIDs
                oResID.Delete_()
            Next
            MsgBox("Deletion successfully")
        Catch ex As Exception
            MsgBox("Could not delete " & strComputer & " - ErrorDescription:" & Err.Description)
        End Try
    End Sub

End Class

Open in new window

0
 
jamie77777Author Commented:
Cool thanks, will test...
0
 
jamie77777Author Commented:
Thanks, worked perfectly!  Have changed it to a function and attached here in case others wanted it.  Thank you very much!
Public Function DeleteFromSCCM(ByVal strComputer As String, _
                                       ByVal strSCCMServer As String, _
                                       ByVal strSCCMSiteCode As String, _
                                       ByVal strUserName As String, _
                                       ByVal strPassword As String)
        Dim oResID
        Dim ResIDs

        Try
            Dim lLocator = CreateObject("WbemScripting.SWbemLocator")
            Dim gService = lLocator.ConnectServer(strSCCMServer, "root\sms\site_" & strSCCMSiteCode, strUserName, strPassword)
            ResIDs = gService.ExecQuery("SELECT * from SMS_R_System where NetbiosName = '" & strComputer & "'")
        Catch ex As Exception
            MsgBox("Error: " & Err.Number & ": " & Err.Description)
            Return False
            End
        End Try

        Try
            For Each oResID In ResIDs
                oResID.Delete_()
            Next
            MsgBox("Successfully deleted computer: " & strComputer & " from SCCM!")
            Return True
        Catch ex As Exception
            MsgBox("Error: " & Err.Number & ": " & Err.Description)
            Return False
        End Try

    End Function

Open in new window

0
 
jamie77777Author Commented:
Complete.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now