using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace vCenterHAService
{
    public partial class vCenterHAService : ServiceBase
    {
        public vCenterHAService()
        {
            this.ServiceName = "vCenter Server HA Service";
            this.EventLog.Source = "vCenter Server HA Service";
            this.EventLog.Log = "Application";
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;

            if (!EventLog.SourceExists("vCenter Server HA Service"))
                EventLog.CreateEventSource("vCenter Server HA Service", "Application");

            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            String commandstart = @"service-control.bat --start >> C:\\vCenterHAServiceStatus-%date:~4,2%%date:~7,2%%date:~10,4%.txt";
            var StartServiceControl = new ProcessStartInfo("cmd.exe", "/c" + commandstart)
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                WorkingDirectory = "C:\\Program Files\\VMware\\vCenter Server\\bin",

            };
            var launcdoitstart = Process.Start(StartServiceControl);
             }

        protected override void OnStop()
        {
            base.OnStop();
            String commandstop = @"service-control.bat --stop --all >> C:\\vCenterHAServiceStatus-%date:~4,2%%date:~7,2%%date:~10,4%.txt";
            var StopServiceControl = new ProcessStartInfo("cmd.exe", "/c" + commandstop)
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                WorkingDirectory = "C:\\Program Files\\VMware\\vCenter Server\\bin",

            };
            var launcdoitstop = Process.Start(StopServiceControl);
             }

        }
    }
