Sunday, February 21, 2010

How to start and stop windows service from SSIS

To start or stop a windows service from SSIS package add a Script Task and in design script page use the below code. Replace the ComputerName and ServiceName accordingly.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.ServiceProcess
Public Class ScriptMain
      Public Sub Main()
        Dim controller As New ServiceController
        Dim serviceStatus As ServiceControllerStatus
        Dim conter As Integer
        'Code below is to atop Window services.
        controller.MachineName = "ComputerName"
        controller.ServiceName = "ServiceName"
If ((controller.Status.Equals(serviceStatus.Running)) Or (controller.Status.Equals(serviceStatus.Paused))) Then
            controller.Stop()
            controller.Refresh()
      End If
        'Code below is to start Window services.
If ((controller.Status.Equals(serviceStatus.Stopped)) Or (controller.Status.Equals(serviceStatus.StopPending))) Then
            controller.Start()
            controller.Refresh()
      End If





































No comments: