Solved

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

Posted on 2010-09-23
5
3,570 Views
Last Modified: 2013-11-25
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
Comment
Question by:jamie77777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 33805637
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
 
LVL 31

Accepted Solution

by:
merowinger earned 500 total points
ID: 33805641
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
 

Author Comment

by:jamie77777
ID: 33818496
Cool thanks, will test...
0
 

Author Comment

by:jamie77777
ID: 33818605
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
 

Author Closing Comment

by:jamie77777
ID: 33818608
Complete.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question