Link to home
Start Free TrialLog in
Avatar of Ryan Smith
Ryan SmithFlag for United States of America

asked on

Create SCCM Computer Association with VB.net

Good Morning

This is my first time posting on this forum and I made sure I searched everything I can think of before posting this. I'm new to programming so I’m using VB.net. What I’m trying to do is create a program that will allow some of our non SCCM admins to create a computer association in SCCM by only importing the MAC address and system name. I found some code that allows me to delete an abject from SCCM (which works great) But I’m looking for something "Create" Please see the code below... Please excuse my noob comments..


'************************************************
Imports System.Management
 
Public Class Form1
 
    '*** Functions when the Add system button is clicked
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addsys.Click
 

        '*** Declares the variables (some are not being used right now)
 
        Dim strComputer = SysName.Text
 
        Dim strSMSServer = "XXXX"
 
        Dim strSystemMAC = SysMAC.Text
 
        Dim strSMSSiteCode = "XXXX"
 
        Dim strUserName = Uname.Text
 
        Dim strPassword = PW.Text
 
        Dim ResIDs
 

        '*** Connecting to SCCM
 
        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 & "'")
 

            '*** Message if Connection failed
 
        Catch ex As Exception
 
            MsgBox("Could not connect to SCCM Server - ErrorDescription:" & Err.Description)
 
            End
 
        End Try
 

        '***This Deletes the Object from SCCM I want it to Add an object with System Name and MAC
 
        Try
 
            For Each oResID In ResIDs
 
                oResID.Delete_()
 
            Next
 
            MsgBox("Deletion successfully")
 

            '*** message if the system can't be deleted
 
        Catch ex As Exception
 
            MsgBox("Could not delete " & strComputer & " - ErrorDescription:" & Err.Description)
 
        End Try
 
    End Sub
Avatar of Naman Goel
Naman Goel
Flag of India image

Please find the code for this

Public Shared Function Connect() As ManagementScope
	Try
		Dim connOptions As New ConnectionOptions()
		connOptions.Username = "domain\administrator"
		connOptions.Password = "password"
		connOptions.Authentication = AuthenticationLevel.PacketPrivacy
		Dim mScope As New ManagementScope("\\IP\root\sms\site_SITECODE", connOptions)
		mScope.Connect()
		Return mScope
	Catch e As System.Management.ManagementException
		Console.WriteLine("Failed to connect", e.Message)
		Throw
	End Try
End Function


''' <summary>
''' 
''' </summary>
''' <param name="scope"></param>
Public Shared Sub CallMethod(scope As ManagementScope)
	Try
		' Get the client's SMS_Client class.
		Dim cls As New ManagementClass(scope.Path.Path, "SMS_Site", Nothing)

		' Set up current site code as input parameter for SetAssignedSite.
		Dim inParams As ManagementBaseObject = cls.GetMethodParameters("ImportMachineEntry")
		inParams("NetbiosName") = "System Name"
		inParams("SMBIOSGUID") = "4C4C4544-0036-4210-8046-CAC04F313259"
		inParams("MACAddress") = "00:22:19:57:4B:4E"
		' Assign the Site code.
		Dim outMPParams As ManagementBaseObject = cls.InvokeMethod("ImportMachineEntry", inParams, Nothing)
	Catch e As ManagementException
		Throw New Exception("Failed to execute method", e)
	End Try
End Sub

Open in new window

Avatar of Ryan Smith

ASKER

Thanks, Im giving this a try now.
This looks soooo promising but I’m sorry for this post... As i said in my first post I’m still a beginner. I created a new project and copy and pasted the code you provided in new .vb page. I get no errors with the code so that’s great.  I also had to Import the Sstem.Management refrence

Next I changed the domain account, password and the IP address of my SCCM server. I also game the system a name and kept your mac and SMBIOSGUID. Then I selected start to run the program nothing happened... duhh. I know i need to create some kind of button to call this function to run.  But how do I link the click button action to tell it to run this function? Eventually I’m going to change the "NetbiosName", "SMBIOSGUID",  and "MACAddress" to take input from a text box.
ASKER CERTIFIED SOLUTION
Avatar of Naman Goel
Naman Goel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, this will be my last post and I'll close this. My question now is about the inputs..

I know that the .text is coming from my application that i create.

Line 23) Dim mScope As New ManagementScope([String].Format("\\{0}\root\sms\site_{1}", txtIPAddress.Text, txtSiteCode.Text), connOptions) - Do I have to input my server name or IP in "{0}" and my site code in {1}?  or is that populated by my application text boxes named "textIPAddress.text" and "TxtSiteCode.Text"?

Line 36) Dim cls As New ManagementClass(scope.Path.Path, "SMS_Site", Nothing) - Do i have to enter anything for "SMS_Site" or leave it as is?
please find my answers

that will take values from textbox

line number 36 you can leave as is.
ok, everything seems to be in place. When i go to run the application and input the information.. nothing happens.. Im wondering if its running because even if i input inforrect data in the txtIPAddress field.. nothing happens.. no error.. nothing..
try to debug it by placing a break point in both the methods and let me know your error.

This is working in my environment. There must be some problem in your input also if you are running your application from system where site server is installed the use following instead of above code for Connect method.

Public Function Connect() As ManagementScope
			Try
				Dim connOptions As New ConnectionOptions()

				connOptions.Authentication = AuthenticationLevel.Packet
				Dim mScope As New ManagementScope([String].Format("root\sms\site_{0}", txtSiteCode.Text), connOptions)
				mScope.Connect()
				Return mScope
			Catch e As System.Management.ManagementException
				Console.WriteLine("Failed to connect", e.Message)
				Throw
			End Try
		End Function

Open in new window

Thank you so much!!!! My inputs were wrong! I'm closing this question now! Thanks... But umm.. one last thing. If i wanted to add this system to a specific collection.. How will I go about that??
Walked me all the way through it.. Very Great Job!
That's very simple for that you need to add create collection rule to add that object to collection.
Can this be done within the program?
Yes...
lol.. ok I'll be creating another question...
I created a new Question for ya! "Adding SCCM Computer Association to Collection"
naman_goel are you going to help me with my other question?