jspencer15
IS-IT--Management
Hi,
I'm building an asp.net website to administer SMS. There is a relatively simple function I've been using for quite some time in Visual Basic to add a computer to a collection. However when I take the code verbatim and add it to a .NET website I get inconsistent results. Sometimes the computer gets added, sometimes it doesn't I never see any errors or anything that would indicate there was a failure. What's even stranger is that sometimes it will appear as though the computer was added (if I click on the collection in the MMC I'll see the computer name listed in the right panel) yet when I right-click the collection, choose Properties, then choose Membership Rules, the computer isn't there! The VB code (which works consistently) is listed below. The only changes I've made in my asp.net page is replacing all occurences of msgbox with response.write. Any help or ideas would be greatly appreciated.
'This is the collection ID I want to add to: "A0100019"
'Computer to add to collection: "MyIBM"
'ResourceID of "MyIBM": 15853
Dim conOptions As New System.Management.ConnectionOptions
Dim mgmtScope As ManagementScope = New ManagementScope("\\SMSxx\root\sms\site_x01", conOptions)
Try
mgmtScope.Connect()
If mgmtScope.IsConnected Then
Dim objCollection As ManagementObject = New ManagementObject(mgmtScope, New ManagementPath("SMS_Collection.CollectionID='A0100019'"), Nothing)
Try
objCollection.Get()
MsgBox("We're connected")
Catch
MsgBox("Couldn't connect")
End Try
Dim objCollRule As ManagementClass = New ManagementClass(mgmtScope, New ManagementPath("SMS_CollectionRuleDirect"), Nothing)
Try
objCollRule.Get()
MsgBox("We're connected...")
Catch
MsgBox("Couldn't connect")
End Try
Try
Dim objNewRule As ManagementObject = objCollRule.CreateInstance()
Dim str1 As String = "SMS_R_System"
Dim str2 As String = "15853"
Dim str3 As String = "MyIBM"
objNewRule.SetPropertyValue("ResourceClassName", str1)
objNewRule.SetPropertyValue("ResourceID", str2)
objNewRule.SetPropertyValue("RuleName", str3)
Dim objArgs() As Object = {objNewRule}
objCollection.InvokeMethod("AddMemberShipRule", objArgs)
MsgBox("Should be done...")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I'm building an asp.net website to administer SMS. There is a relatively simple function I've been using for quite some time in Visual Basic to add a computer to a collection. However when I take the code verbatim and add it to a .NET website I get inconsistent results. Sometimes the computer gets added, sometimes it doesn't I never see any errors or anything that would indicate there was a failure. What's even stranger is that sometimes it will appear as though the computer was added (if I click on the collection in the MMC I'll see the computer name listed in the right panel) yet when I right-click the collection, choose Properties, then choose Membership Rules, the computer isn't there! The VB code (which works consistently) is listed below. The only changes I've made in my asp.net page is replacing all occurences of msgbox with response.write. Any help or ideas would be greatly appreciated.
'This is the collection ID I want to add to: "A0100019"
'Computer to add to collection: "MyIBM"
'ResourceID of "MyIBM": 15853
Dim conOptions As New System.Management.ConnectionOptions
Dim mgmtScope As ManagementScope = New ManagementScope("\\SMSxx\root\sms\site_x01", conOptions)
Try
mgmtScope.Connect()
If mgmtScope.IsConnected Then
Dim objCollection As ManagementObject = New ManagementObject(mgmtScope, New ManagementPath("SMS_Collection.CollectionID='A0100019'"), Nothing)
Try
objCollection.Get()
MsgBox("We're connected")
Catch
MsgBox("Couldn't connect")
End Try
Dim objCollRule As ManagementClass = New ManagementClass(mgmtScope, New ManagementPath("SMS_CollectionRuleDirect"), Nothing)
Try
objCollRule.Get()
MsgBox("We're connected...")
Catch
MsgBox("Couldn't connect")
End Try
Try
Dim objNewRule As ManagementObject = objCollRule.CreateInstance()
Dim str1 As String = "SMS_R_System"
Dim str2 As String = "15853"
Dim str3 As String = "MyIBM"
objNewRule.SetPropertyValue("ResourceClassName", str1)
objNewRule.SetPropertyValue("ResourceID", str2)
objNewRule.SetPropertyValue("RuleName", str3)
Dim objArgs() As Object = {objNewRule}
objCollection.InvokeMethod("AddMemberShipRule", objArgs)
MsgBox("Should be done...")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try