In the last Post we created a simple webservice. So far it is working, but yet not really useful.
Now we will add the basic functionality to it which we need in the future. This will be the Database connection to our MDT Database and some functions which will help us to contact the SCCM Server. First we want to be able to have access to the MDT Database. For this we need a connection string. Just open the web.config file from our webservice project and find the following entry:
<connectionStrings/>
This means that there isn't any Connection String configured yet. Just replace it with the following
<connectionStrings>
<add name="MDTDB" connectionString="Data Source=YourMDTDBServer;Initial Catalog=YourMDTDB;Integrated Security=SSPI;"/>
</connectionStrings>
Adjust the settings for the Database server and name to your environment. I used Integrated Security, if you need to supply a specific username and password to support a different scenario just use the following entry instead (or whatever else fits to your environment):
<connectionStrings>
<add name="MDTDB" connectionString="Data Source=YourMDTDBServer;Initial Catalog=YourMDTDB;User ID=YourUserName; Password=YourPassword;"/>
</connectionStrings> And if we have the web.config open already, we also add two additional values we need to contact our SCCM Server. Look for the entry
<appSettings/>
and replace it with
<appSettings>
<add key="RootServer" value="YourSCCMServer"/>
<add key="SLPServer" value="YourSCCMServer"/>
</appSettings>
Again, adjust YourSCCMServer to your environment.
I prefer to place our webservice at the same location as the UnknownComputer webservice from MDT 2008. There is no need to re-invent the wheel and we can make use of both webservices and both can share the same environment. In this case both will use the same web.config. So use "your" web.config for testing on your local machine and just change the web.config on your webserver again as described. There won't be many changes so it shouldn't be a problem to keep them consistent. Actually you probably only have to add the Connection String as the other Application Settings are already available.
If you prefer to create your own location for the webservice you might need to have some additional changes in the web.config. But this will not be part of this post.
OK, the web.config has been changed. Let's go to our webservice. To ease the further programming we will just import a couple of namespaces we need. Add them at the top of your Service1.asmx.vb directly after the already existing Imports statements.
Imports System.Management
Imports System.Web.Configuration
Then we would need to create a couple of global Variables to store the values we just added to the web.config. Directly after the Class definition add the following lines
Private _RootServer As String = ""
Private _SlpServer As String = ""
Private _ConnectionString As String = ""
Now we need to publish the information we stored in the web.config to this variables as soon as somebody opens the webservice. To do so, we create a procedure called New() which is called if something creates a new instance of this class. Or easier to say if somebody calls this webservice.
Public Sub New()
' Read the root server parameter
Me._RootServer = WebConfigurationManager.AppSettings.Item("RootServer")
Me._SlpServer = WebConfigurationManager.AppSettings.Item("SLPServer")
' Get Connection String from web.config
Me._ConnectionString = ConfigurationManager.ConnectionStrings("MDTDB").ConnectionString
End Sub
Actually this would be enough to start using the MDT Database from a function, which I will show in the next post. One last thing before we go ahead to this, we want to add a Reference to our Project which enables us to use WMI for some of the functions. Almost everything in SMS/SCCM can and should be done via WMI, so it's becoming a necessity. To add a reference to WMI into our project, just right-click on your project and choose Add Reference... . In the window which pops up scroll down to the entry "System.Management" and click on OK.
To have a test if everything is still working as supposed just press Control + F5 as explained in the last Post.
A lot of stuff to read and this webservice still can't do more than just "Hello World". Don't be disappointed in the next post we will finally add our first custom function to this webservice. Woohoo.
Download the full source files.