Obtaining the SMS Provider Location in VB.Net
-
2012年2月18日 23:29
I use the VB.net code below to connect to SCCM. This all works well but I need to detect the site code rather than explicitly provide it: ("Dim scope As New ManagementScope("\\" & txtServer.Text & "\root\sms\site_xxx", connection)")
I know how this is done in VB Script but how is it achieved in VB.Net?
Thanks
Private Function Connect()
Try
Dim connection As New ConnectionOptions
connection.Username = txtUser.Text
connection.Password = txtPassword.Text
connection.Authority = "ntlmdomain:" & txtDomain.TextDim scope As New ManagementScope("\\" & txtServer.Text & "\root\sms\site_xxx", connection)
Return scope
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessException
MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
End TryReturn Err.Number
End Function
すべての返信
-
2012年2月19日 0:21
Here is some of the code that I use in C#. If you have difficulty in converting it, let me know:
string l_namespace = m_SccmServer.ConnectionScope.Path.NamespacePath; m_SiteCode = l_namespace.Substring(l_namespace.LastIndexOf("_", StringComparison.CurrentCulture) + 1);where m_SccmServer is defined:
private WqlConnectionManager m_SccmServer;
There doesn't seem to be a direct way to do it, but this has worked for me thus far.It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
-
2012年2月19日 9:38
I tried the following code but still struggling to obtain the sitecode from it:
Dim scope As New ManagementScope("\\" & txtServer.Text & "\root\sms", connection)
Dim lNameSpace As String = scope.Path.NamespacePathDim siteCode As String = lNameSpace.Substring(lNameSpace.LastIndexOf("_", StringComparison.CurrentCulture) + 1)
Return scope
I also tried:
Dim scope As New ManagementScope("\\" & txtServer.Text & "\root\sms", connection)
Dim lNameSpace As String = "\\" & txtServer.Text & "\" & scope.Path.NamespacePathDim siteCode As String = lNameSpace.Substring(lNameSpace.LastIndexOf("_", StringComparison.CurrentCulture) + 1)
Return scope
Both to no avail. Any help appreciated.
-
2012年2月19日 15:05In my particular situation, I am using the assemblies from the SDK and my connection is a WqlConnectionManager, rather than a ManagementScope object. You may want to give that a shot. I can't say that I have tried it using the WMI objects directly.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
-
2012年2月19日 22:07
Thanks Thomas, I think I know the assemblies you are alluding to (microsoft.configurationmanagement.managementprovider.dll and adminui.wqlqueryengine.dll). I still had some difficulty with these though I dare say I was just using incorrect syntax or something. In fact I have used these successfully in an identical C# project, I've just not yet managed to get them working in VB.
I have worked around the problem by referencing WBemScripting COM (Interop.WbemScripting.dll). I'm not terribly happy about this as I need to supply this dll with my compiled project which is messy, I'd have liked to use native .Net. I will return to this for the next version. Thanks for your help.
In the meantime if anyone can give me the native VB.net code for this I'd still be grateful.

