Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Management;
- using System.Net.NetworkInformation;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace ServiceManager
- {
- class Service
- {
- private readonly string _ip;
- private readonly string _user;
- private readonly string _password;
- private readonly string _serviceName;
- public string ProcessOutput { get; set; }
- public string Status { get; set; }
- public Service(string ip, string user, string passwrod, string serviceName)
- {
- this._ip = ip;
- this._user = user;
- this._password = passwrod;
- this._serviceName = serviceName;
- }
- private ProcessStartInfo CreateStartInfo()
- {
- return new ProcessStartInfo
- {
- FileName = "cmd.exe",
- WindowStyle = ProcessWindowStyle.Hidden,
- CreateNoWindow = true,
- RedirectStandardOutput = true,
- UseShellExecute = false
- };
- }
- private Process CreateProcess(string action)
- {
- var parametrs = _ip + " " + _user + " " + _password + " " + action + " " + _serviceName;
- Process myProcess = new Process{StartInfo = CreateStartInfo()};
- myProcess.StartInfo.Arguments = @"/c" + Environment.CurrentDirectory + "/controlservice.bat " + parametrs;
- return myProcess;
- }
- public bool Stop()
- {
- var myProcess = CreateProcess("stop");
- try
- {
- myProcess.Start();
- ProcessOutput = myProcess.StandardOutput.ReadToEnd();
- if (ProcessOutput.Contains("успешно"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (Exception e)
- {
- ProcessOutput = e.Message;
- return false;
- }
- }
- public bool Start()
- {
- var myProcess = CreateProcess("start");
- try
- {
- myProcess.Start();
- ProcessOutput = myProcess.StandardOutput.ReadToEnd();
- if (ProcessOutput.Contains("успешно"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (Exception e)
- {
- ProcessOutput = e.Message;
- return false;
- }
- }
- public string GetPingStatus(string ip)
- {
- Ping ping = new Ping();
- return ping.Send(ip, 2000)?.Status.ToString();
- }
- private void GetStatus() {
- if (!string.Equals(GetPingStatus(_ip).ToLower(), GetPingStatus("127.0.0.1").ToLower()))
- {
- Status = "Unknown";
- }
- var connectionsOptions = new ConnectionOptions
- {
- Username = @".\" + _user,
- Password = _password,
- EnablePrivileges = true,
- Impersonation = ImpersonationLevel.Impersonate
- };
- var managementScope = new ManagementScope(string.Format($@"\\{_ip}\root\cimv2", connectionsOptions))
- {
- Options = connectionsOptions
- };
- managementScope.Connect();
- var query = new ObjectQuery(string.Format($@"Select * from Win32_Service where name = '{_serviceName}'"));
- var seacher = new ManagementObjectSearcher(managementScope, query);
- var servicesCollection = seacher.Get();
- Status=servicesCollection.OfType<ManagementObject>().FirstOrDefault()?.GetPropertyValue("State").ToString();
- }
- }
- }
Add Comment
Please, Sign In to add comment