root/TaskScheduler/TaskScheduler/Tasks/ServiceUpTask.cs

67
13
{
13
{
14
    [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
14
    [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
15
    [assembly: PermissionSet(SecurityAction.RequestMinimum, Name = "FullTrust")]
15
    [assembly: PermissionSet(SecurityAction.RequestMinimum, Name = "FullTrust")]
16
    public class ServiceUpTask : Task
16
    [DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]
17
    public class ServiceUpTask : ITask
17
    {
18
    {
18
        public string ServiceName { get; set; }
19
        public string ServiceName { get; set; }
19
        public string UserName { get; set; }
20
        public string UserName { get; set; }
...
...
23
24
24
        #region Overrides of Task
25
        #region Overrides of Task
25
26
26
        public override void Run()
27
        public void Run()
27
        {
28
        {
28
            if (!string.IsNullOrEmpty(ServiceName))
29
            if (!string.IsNullOrEmpty(ServiceName))
29
            {
30
            {
...
...
33
                {
34
                {
34
                    impersonationContext = AssumeIdentity(UserName, DomainName, Password);
35
                    impersonationContext = AssumeIdentity(UserName, DomainName, Password);
35
36
36
                    using (var service = ServiceBuilder(ServiceName, MachineName))
37
                    using (ServiceController service = ServiceBuilder(ServiceName, MachineName))
37
                    {
38
                    {
38
                        Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));
39
                        Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));
39
40
...
...
44
                        }
45
                        }
45
46
46
                        service.Refresh();
47
                        service.Refresh();
47
                
48
48
                        if (service.Status == ServiceControllerStatus.Stopped)
49
                        if (service.Status == ServiceControllerStatus.Stopped)
49
                        {
50
                        {
50
                            service.Start();
51
                            service.Start();
...
...
60
        }
61
        }
61
62
62
        /// <summary>
63
        /// <summary>
64
        /// Gets or sets the name.
65
        /// </summary>
66
        /// <value>The name.</value>
67
        public string Name { get; set; }
68
69
        /// <summary>
70
        /// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
71
        /// </summary>
72
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
73
        public bool Active { get; set; }
74
75
        /// <summary>
76
        /// Gets or sets the last ran.
77
        /// </summary>
78
        /// <value>The last ran.</value>
79
        public DateTime? LastRan { get; set; }
80
81
        /// <summary>
82
        /// Gets or sets the id.
83
        /// </summary>
84
        /// <value>The id.</value>
85
        public decimal? Id { get; set; }
86
87
        /// <summary>
63
        /// Builds the service.
88
        /// Builds the service.
64
        /// </summary>
89
        /// </summary>
65
        /// <param name="serviceName">Name of the service.</param>
90
        /// <param name="serviceName">Name of the service.</param>
...
...
67
        /// <returns></returns>
92
        /// <returns></returns>
68
        private static ServiceController ServiceBuilder(string serviceName, string machineName)
93
        private static ServiceController ServiceBuilder(string serviceName, string machineName)
69
        {
94
        {
70
            var service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
95
            ServiceController service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
71
            return service;
96
            return service;
72
        }
97
        }
73
98
...
...
91
                if (phToken != IntPtr.Zero)
116
                if (phToken != IntPtr.Zero)
92
                {
117
                {
93
                    var windowsIdentity = new WindowsIdentity(phToken);
118
                    var windowsIdentity = new WindowsIdentity(phToken);
94
                    var impersonationContext = windowsIdentity.Impersonate();
119
                    WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
95
                    CloseHandle(phToken);
120
                    CloseHandle(phToken);
96
                    return impersonationContext;
121
                    return impersonationContext;
97
                }
122
                }