/// <summary> /// Sets the se service logon right. /// </summary> /// <param name="wmi">The WMI.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> private bool SetSeServiceLogonRight(WMIServicesManager wmi) { if (!CDFMonitor.Instance.Config.AppSettings.UseServiceCredentials) { return(true); } if (!string.IsNullOrEmpty(_creds.UserName) && _creds.Domain != Properties.Resources.SessionName) { string userName = _creds.UserName.ToString(); if (!string.IsNullOrEmpty(_creds.Domain) && !_creds.UserName.Contains(_creds.Domain)) { userName = string.Format("{0}\\{1}", _creds.Domain, _creds.UserName); } // get remote path string path = wmi.GetPath(Properties.Resources.ServiceName); int id = wmi.RunProcess(string.Format("{0}\\cdfmonitor\\cdfmonitor.exe /seservicelogonright: {1}", wmi.GetSYSTEMROOT(), userName)); return(wmi.CheckProcess(_processName, id, true) == 0); } else { CDFMonitor.LogOutputHandler("skipping seservicelogonright"); } return(true); }
/// <summary> /// Checks the state of the service. /// </summary> /// <param name="wmi">The WMI.</param> /// <returns>RemoteStatus.</returns> private static RemoteStatus CheckServiceState(WMIServicesManager wmi) { RemoteStatus rs; WMIServicesManager.ServiceState ss = wmi.GetServiceState(Resources.ServiceName); switch (ss) { case WMIServicesManager.ServiceState.Unknown: rs = RemoteStatus.Unknown; break; case WMIServicesManager.ServiceState.StopPending: rs = RemoteStatus.Stopping; break; case WMIServicesManager.ServiceState.Stopped: rs = RemoteStatus.Stopped; break; case WMIServicesManager.ServiceState.StartPending: rs = RemoteStatus.Starting; break; case WMIServicesManager.ServiceState.Running: rs = RemoteStatus.Started; break; default: rs = RemoteStatus.Error; break; } return(rs); }
/// <summary> /// Uns the deploy. /// </summary> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool UnDeploy() { bool retVal = true; string destination = string.Empty; int i = 0; do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] == RemoteStatus.UnDeployed) { CDFMonitor.LogOutputHandler(string.Format("UnDeploy: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } CDFMonitor.LogOutputHandler("UnDeploy: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; destination = "\\\\" + machine + "\\admin$\\cdfmonitor"; int pid = 0; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status && (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.True && wmi.StopService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success && wmi.UninstallService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success) | (FileManager.CheckPath(destination, true) && ((pid = wmi.RunProcess(string.Format("{0}\\cdfmonitor\\cdfmonitor.exe /clean", wmi.GetSYSTEMROOT()))) != 0 & wmi.CheckProcess(_processName, pid, true) == 0) && FileManager.DeleteFolder(destination, false))) { RemoteList[machine] = RemoteStatus.UnDeployed; } else { CDFMonitor.LogOutputHandler("Fail:UnDeploy: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i)) { return(retVal); } } }while ((RemoteList.Count(v => v.Value == RemoteStatus.UnDeployed)) != RemoteList.Count && CheckProgress(true, string.Empty, i)); return(retVal); }
/// <summary> /// Stops the remote. /// </summary> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool StopRemote() { bool retVal = true; string destination = string.Empty; int i = 0; do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] == RemoteStatus.Stopped) { CDFMonitor.LogOutputHandler(string.Format("StopRemote: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } CDFMonitor.LogOutputHandler("StopRemote: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status && (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.True && wmi.StopService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success)) { if (wmi.GetServiceState(Resources.ServiceName) == WMIServicesManager.ServiceState.Stopped) { RemoteList[machine] = RemoteStatus.Stopped; } else { RemoteList[machine] = RemoteStatus.Error; } } else { CDFMonitor.LogOutputHandler("Fail:StopRemote: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i)) { return(retVal); } } }while ((RemoteList.Count(v => v.Value == RemoteStatus.Stopped) != RemoteList.Count) && CheckProgress(true, string.Empty, i)); return(retVal); }
/// <summary> /// Modifies the specified start mode. /// </summary> /// <param name="startMode">The start mode.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool Modify(string startMode) { bool retVal = true; string destination = string.Empty; WMIServicesManager.StartMode sm; if (!ConvertToStartMode(startMode, out sm)) { return(false); } int i = 0; do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] == RemoteStatus.Modified) { CDFMonitor.LogOutputHandler(string.Format("Modify: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } CDFMonitor.LogOutputHandler("Modify: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; destination = "\\\\" + machine + "\\admin$\\cdfmonitor"; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status && (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.True && (wmi.GetServiceState(Resources.ServiceName) != WMIServicesManager.ServiceState.Stopped && wmi.StopService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success) | wmi.ChangeStartMode(Resources.ServiceName, sm) == WMIServicesManager.ReturnValue.Success)) { if (sm == WMIServicesManager.StartMode.Automatic && (wmi.StartService(Resources.ServiceName) != WMIServicesManager.ReturnValue.Success)) { RemoteList[machine] = RemoteStatus.Error; } else { RemoteList[machine] = RemoteStatus.Modified; } } else { CDFMonitor.LogOutputHandler("Fail:Modify: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i)) { return(retVal); } } }while ((RemoteList.Count(v => v.Value == RemoteStatus.Modified) != RemoteList.Count) && CheckProgress(true, string.Empty, i)); return(retVal); }
/// <summary> /// Gathers this instance. /// </summary> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool Gather() { bool retVal = true; string source = string.Empty; string destination = string.Empty; if (!FileManager.CheckPath(_path, true)) { return(false); } int i = 0; do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] == RemoteStatus.Gathered) { CDFMonitor.LogOutputHandler(string.Format("Gather: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } CDFMonitor.LogOutputHandler("Gather: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; source = "\\\\" + machine + "\\admin$\\cdfmonitor"; destination = _path + "\\" + machine; int pid = 0; RemoteStatus rs = RemoteStatus.Unknown; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status && (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.True && ((rs = CheckServiceState(wmi)) != RemoteStatus.Started || wmi.StopService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success)) | (FileManager.CheckPath(source, false) && (pid = wmi.RunProcess(wmi.GetSYSTEMROOT() + "\\cdfmonitor\\cdfmonitor.exe /zip")) != 0 && wmi.CheckProcess(_processName, pid, true) == 0 && FileManager.CopyFiles(FileManager.GetFiles(source, "*.zip", SearchOption.TopDirectoryOnly), destination, true) && (FileManager.DeleteFiles(FileManager.GetFiles(source, "*.zip", SearchOption.TopDirectoryOnly), true) == FileManager.Results.Success) && (rs != RemoteStatus.Started || wmi.StartService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success))) { RemoteList[machine] = RemoteStatus.Gathered; } else { CDFMonitor.LogOutputHandler("Fail:Gather: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i, destination)) { return(retVal); } } }while ((RemoteList.Count(v => v.Value == RemoteStatus.Gathered) != RemoteList.Count) && CheckProgress(true, string.Empty, i)); return(retVal); }
/// <summary> /// Deploys the specified start mode. /// </summary> /// <param name="startMode">The start mode.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool Deploy(string startMode) { bool retVal = true; string destination = string.Empty; if (!CheckSource(_path)) { return(false); } WMIServicesManager.StartMode sm; if (string.IsNullOrEmpty(startMode)) { sm = WMIServicesManager.StartMode.Automatic; } else if (!ConvertToStartMode(startMode, out sm)) { return(false); } int i = 0; // Add current running exe to list so it doesnt have to be in staged dir List <string> fileList = FileManager.GetFiles(_path, "*.*", SearchOption.AllDirectories).ToList(); fileList.Add(AppDomain.CurrentDomain.FriendlyName); do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] == RemoteStatus.Deployed) { CDFMonitor.LogOutputHandler(string.Format("Deploy: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } CDFMonitor.LogOutputHandler("Deploy: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; destination = "\\\\" + machine + "\\admin$\\cdfmonitor"; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status && (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.True && (wmi.StopService(Resources.ServiceName) == WMIServicesManager.ReturnValue.Success)) | (FileManager.CheckPath(destination, true) && FileManager.CopyFiles(fileList.ToArray(), destination, true) && SetSeServiceLogonRight(wmi) && wmi.InstallService(Resources.ServiceName, Resources.FriendlyName, wmi.GetSYSTEMROOT() + "\\cdfmonitor\\cdfmonitor.exe /runningasservice", WMIServicesManager.ServiceType.OwnProcess, WMIServicesManager.OnError.UserIsNotNotified, sm, false, _creds, null, null, null) == WMIServicesManager.ReturnValue.Success)) { if (sm == WMIServicesManager.StartMode.Automatic && (wmi.ChangeStartMode(Resources.ServiceName, sm)) != WMIServicesManager.ReturnValue.Success || (wmi.StartService(Resources.ServiceName) != WMIServicesManager.ReturnValue.Success)) { RemoteList[machine] = RemoteStatus.Error; } else { RemoteList[machine] = RemoteStatus.Deployed; } wmi.ShowProperties(Resources.ServiceName); } else { CDFMonitor.LogOutputHandler("Fail:Deploy: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i)) { return(retVal); } } }while ((RemoteList.Count(v => v.Value == RemoteStatus.Deployed) != RemoteList.Count) && CheckProgress(true, string.Empty, i)); return(retVal); }
/// <summary> /// Checks remote machine for existence of utility. Returns true if successful. /// </summary> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool Check() { bool retVal = true; string source = string.Empty; int i = 0; do { i++; foreach (string machine in new List <string>(RemoteList.Keys)) { if (RemoteList[machine] != RemoteStatus.Unknown && RemoteList[machine] != RemoteStatus.Error) { CDFMonitor.LogOutputHandler(string.Format("Check: skipping machine {0} because of cached state: {1}", machine, RemoteList[machine])); continue; } source = "\\\\" + machine + "\\admin$\\cdfmonitor"; CDFMonitor.LogOutputHandler("Check: processing machine:" + machine + " state:" + RemoteList[machine].ToString()); WMIServicesManager wmi; RemoteStatus rs = RemoteStatus.Error; if (Ping(machine) && (wmi = new WMIServicesManager(machine, _creds)) != null && wmi.Status ) { if (wmi.IsServiceInstalled(Resources.ServiceName) == WMIServicesManager.ReturnValue.False) { rs = RemoteStatus.UnDeployed; } else { wmi.ShowProperties(Resources.ServiceName); rs = CheckServiceState(wmi); } RemoteList[machine] = rs; } else { CDFMonitor.LogOutputHandler("Fail:Check: unsuccessful."); RemoteList[machine] = RemoteStatus.Error; retVal = false; } if (!CheckProgress(false, machine, i)) { return(retVal); } } }while ((RemoteList.Count(v => (v.Value == RemoteStatus.Unknown | v.Value == RemoteStatus.Error)) != 0) && CheckProgress(true, string.Empty, i)); CDFMonitor.LogOutputHandler("Check: exit:" + retVal); return(retVal); }