Managing Windows Indexing Service with Visual Basic.NET using WMI - Passing input parameters to WMI for managing Indexing Service
(Page 6 of 6 )
In the previous section, we dealt with some of the WMI methods that don't have any input parameters. But, there will be cases where input parameters are necessary. One of these cases is "changing the start mode" of the indexing service.
To change the start mode, we need to provide the type of start mode to WMI (which is an input parameter). This input parameter will be gathered by the WMI method; it manages the indexing service accordingly.
The following script changes/modifies the start mode of indexing service.
Private Sub btnChangeStartMode_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnChangeStartMode.Click
Dim LateBoundObject As ManagementObject
LateBoundObject = New ManagementObject(Nothing, New
ManagementPath("\\SERVER\root\CIMV2:Win32_Service.Name=""CiSvc"""), Nothing)
Dim inParams As System.Management.ManagementBaseObject =
Nothing
inParams = LateBoundObject.GetMethodParameters
("ChangeStartMode")
inParams("StartMode") = "Automatic"
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("ChangeStartMode", inParams,
Nothing)
MessageBox.Show(outParams.Properties
("ReturnValue").Value.ToString)
LateBoundObject.Dispose()
End Sub
You could observe that everything is the same, except for some new lines as follows:
inParams = LateBoundObject.GetMethodParameters("ChangeStartMode")
inParams("StartMode") = "Automatic"
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("ChangeStartMode", inParams,
Nothing)
From the above statements, we can understand that we are working with the WMI method "ChangeStartMode" which accepts an input parameter called "StartMode" (which is of type string). First of all, we get all parameters into "inParams" with the help of "getMethodParameters", focussing on "ChangeStartMode" method. Then, we assign values for all those input parameters in the form of properties. Finally we invoke the WMI method using "InvokeMethod".
Summary
WMI leads the way to WBEM (Web Based Enterprise Management) in a very easy and pleasant manner. I focussed only on "Indexing Service" in this article, to work with WMI. But you can work with almost any service within Windows OS. Another great point is that WMI is transparent to ASP.NET as well. Try to convert the above application to a web application (ASP.NET). It works without any core changes!
One should also note that WMI does not supercede all of the features of every service (or the entire Win32 API). At the same time, WMI is getting enhanced and extended day by day. For example, we cannot work with "catalogs" in an indexing service using WMI. WMI does not support it (though it may be supported in future versions of WMI). To work with "catalogs" of the indexing service, we need to work with COM (you can expect my next article on it soon).
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |