Convert Vbscript to VB.Net

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I found some code from a google search on how to get and set windows services startup type. i would like to make an application that saves the startup types for every service to an xml file, in which whenever i re-format my computer and re-install windows, i can just run my program and have it set all the services to what was saved in the file. i know how to do everything except getting and setting the startup type. here is the vbscript code that i found:
VB.NET:
' From the book "Windows XP Cookbook"
' ISBN: 0596007256

' ------ SCRIPT CONFIGURATION ------
strSvcName = "MyMonitor"
strStartupType = "Automatic" ' can be "Automatic", "Manual", or "Disabled"
strComputer = "."
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'")
intRC = objService.Change(,,,,strStartupType)
' can alternatively use objService.ChangeStartup(strStartupType) method
if intRC > 0 then
 WScript.Echo "Error setting service startup type: " & intRC
else
 WScript.Echo "Successfully set service startup type"

end if
http://techtasks.com/code/viewbookcode/373

any ideas as to where i should look to do the equivalent in VB 2003 or VB 2005 ?
[/font]
 
You already know about the WMI Code Creator (but forgot or couldn't figure it out?)

With this tool I did these steps:
  • selected the 'Execute a method' tab
  • chose 'Win32_Service' class
  • selected 'ChangeStartMode' method
  • clicked the in-parameter and pasted/wrote a test value 'Disabled'
  • then just picked any service to test. ('Messenger')
I ran the code and verified in control panel services, test was executed with success.

Here is the generated code (style VB.Net) that should be not too hard to get use of:
VB.NET:
Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
    Public Class CallWMIMethod
        Public Overloads Shared Function Main() As Integer
            Try
                Dim classInstance As New ManagementObject( _
                    "root\CIMV2", _
                    "Win32_Service.Name='Messenger'", _
                    Nothing)
                ' Obtain [in] parameters for the method
                Dim inParams As ManagementBaseObject = _
                    classInstance.GetMethodParameters("ChangeStartMode")
                ' Add the input parameters.
                inParams("StartMode") =  "Disabled"
                ' Execute the method and obtain the return values.
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod("ChangeStartMode", inParams, Nothing)
                ' List outParams
                Console.WriteLine("Out parameters:")
                Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
            Catch err As ManagementException
                MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
            End Try
        End Function
    End Class
End Namespace
 
Back
Top