Create Service on Remote Machine

Private Sub CrateService(ByVal strComputer As String)

        Dim objWMIService, colServiceList, objService, errReturn, objSWbemLocator
        Const OWN_PROCESS = 16
        Const NOT_INTERACTIVE = False
        Const NORMAL_ERROR_CONTROL = 2
        Dim strName, strDisplay, strPath As String

        strName = txtName.Text
        strDisplay = txtDisplay.Text
        strPath = txtPath.Text

        Try

            objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

            If (strComputer = Environment.MachineName.ToString) Then

                objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\cimv2")
            Else
                objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", strTargetUser, strTargetPwd)
            End If

            objWMIService.Security_.ImpersonationLevel = 3

            objService = objWMIService.Get("Win32_BaseService")
            errReturn = objService.Create(strName, strDisplay, strPath, OWN_PROCESS, NORMAL_ERROR_CONTROL, strMode, NOT_INTERACTIVE, "NT AUTHORITY\LocalService", "")

            If (errReturn = 0) Then
                MessageBox.Show("Service Successfully Created.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (errReturn = 1) Then
                MessageBox.Show("Not Supported.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (errReturn = 2) Then
                MessageBox.Show("Access Denied.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (errReturn = 9) Then
                MessageBox.Show("Path Not Found.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (errReturn = 10) Then
                MessageBox.Show("Service Already Running.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf (errReturn = 14) Then
                MessageBox.Show("Service Disabled.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Service Creation Failed.", "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message.ToString, "Create Service", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End Try

        

End Sub

Related Posts by Categories



Stumble This Fav This With Technorati Add To Del.icio.us Digg This Add To Reddit Add To Facebook Add To Yahoo

0 comments:

Post a Comment