Ryan Smith
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("WbemScriptin g.SWbemLoc ator")
Dim gService = lLocator.ConnectServer(str SMSServer, "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
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("WbemScriptin
Dim gService = lLocator.ConnectServer(str
ResIDs = gService.ExecQuery("SELECT
'*** 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
ASKER
Thanks, Im giving this a try now.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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].F ormat("\\{ 0}\root\sm s\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?
I know that the .text is coming from my application that i create.
Line 23) Dim mScope As New ManagementScope([String].F
Line 36) Dim cls As New ManagementClass(scope.Path
please find my answers
that will take values from textbox
line number 36 you can leave as is.
that will take values from textbox
line number 36 you can leave as is.
ASKER
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.
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
ASKER
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??
ASKER
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.
ASKER
Can this be done within the program?
Yes...
ASKER
lol.. ok I'll be creating another question...
ASKER
I created a new Question for ya! "Adding SCCM Computer Association to Collection"
ASKER
naman_goel are you going to help me with my other question?
Open in new window