public override void OpenUserInterfaceElement(string rawDeviceList) { _parsedListCache = ParseDeviceList(rawDeviceList); var deleteProfilesContext = new DeleteOldProfilesPromptViewModel(); _windowService.ShowDialog <DeleteOldProfilesPrompt>(deleteProfilesContext); if (!deleteProfilesContext.Result) { var msg = $"Action {ActionName} canceled by user."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } if (deleteProfilesContext.DayCount < 0) { var msg = $"Action {ActionName} aborted: cannot delete profiles from < 0 days.."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } _dayCountCache = deleteProfilesContext.DayCount; }
public override void RunCommand(string rawDeviceList) { var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { foreach (var device in devlist) { CancellationToken.Token.ThrowIfCancellationRequested(); if (device == "") { continue; } ResultConsole.AddConsoleLine(ParseResponse(NetworkServices.PingTest(device), device)); } } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
protected void ResetCancelToken(string actionName, OperationCanceledException e) { ResultConsole.AddConsoleLine($"Operation {actionName} canceled."); Logger.LogMessage($"Operation {actionName} canceled by user request. {e.Message}"); ResetCancelToken(); }
protected void WriteToFailedLog(string actionName, List <string> failedList) { var logFile = $"{actionName.Replace(" ", "_")}_failed_log.txt"; var path = $"{Config.ResultsDirectory}\\{logFile}"; var sb = new StringBuilder(); if (File.Exists(path)) { File.Delete(path); Logger.LogMessage($"Deleted file {path}"); Thread.Sleep(1000); } foreach (var device in failedList) { sb.AppendLine(device); } try { FileAndFolderServices.WriteToTextFile(path, sb.ToString(), Logger); Logger.LogMessage($"Wrote \"{actionName}\" results to file {path}"); ResultConsole.AddConsoleLine($"There were {failedList.Count} computers that failed the process. They have been recorded in the log at {path}"); } catch (Exception e) { Logger.LogError($"Unable to write to {path}.", e); ResultConsole.AddConsoleLine($"There were {failedList.Count} computers that failed the process. However, there was an exception attempting to write to the failed log file."); } }
protected virtual void RunScheduleTrigger(string scheduleId, string deviceList) { var devlist = ParseDeviceList(deviceList); var failedlist = new List <string>(); Connection = new ConnectionOptions { EnablePrivileges = true }; try { Parallel.ForEach(devlist, (device) => { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); return; } ManagementScope remote = null; var remoteConnectExceptionMsg = ""; try { remote = WmiService.ConnectToRemoteWmi(device, Scope, Connection); } catch (Exception ex) { remoteConnectExceptionMsg = ex.Message; } if (remote != null) { SccmClientService.TriggerClientAction(scheduleId, remote); } else { ResultConsole.AddConsoleLine($"Error connecting to WMI scope {device}. Process aborted for this device."); Logger.LogWarning($"Error connecting to WMI scope {device}. Process aborted for this device. Exception message: {remoteConnectExceptionMsg}", null); failedlist.Add(device); } }); } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } catch (Exception) { } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void RunCommand(string rawDeviceList) { var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { foreach (var device in devlist) { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); continue; } var remote = _wmiServices.ConnectToRemoteWmi(device, _wmiServices.RootNamespace, new ConnectionOptions()); if (remote != null) { var query = new ObjectQuery("SELECT username FROM Win32_ComputerSystem"); var searcher = new ManagementObjectSearcher(remote, query); var queryCollection = searcher.Get(); foreach (var resultobject in queryCollection) { var result = $"{resultobject["username"]} logged in to {device}"; if (result == $" logged in to {device}" || result == $" logged in to {device}") { result = $"There are no users logged in to {device}!"; } ResultConsole.AddConsoleLine(result); } } else { Logger.LogWarning("There was an error connecting to WMI namespace on " + device, null); ResultConsole.AddConsoleLine("There was an error connecting to WMI namespace on " + device); } } } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void RunCommand(string rawDeviceList) { var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { foreach (var device in devlist) { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); continue; } if (FileAndFolderServices.ValidateDirectoryExists(device, GpoCacheDir, ActionName, Logger)) { var dirtyContents = Directory.EnumerateDirectories($"\\\\{device}\\C${GpoCacheDir}").ToList(); var contents = new List <string>(); foreach (var directory in dirtyContents) { var cleanedPath = $"{GpoCacheDir}\\{directory.Substring(directory.LastIndexOf("\\") + 1)}"; contents.Add(cleanedPath); } foreach (var dir in contents) { FileAndFolderServices.CleanDirectory(device, dir, Logger); } _psExecServices.RunOnDeviceWithoutAuthentication(device, "cmd.exe /C gpupdate.exe /force"); } else { ResultConsole.AddConsoleLine( "Unable to validate the directory for the GPO cache. Please delete manually."); } } } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void OpenUserInterfaceElement(string rawDeviceList) { _parsedListCache = ParseDeviceList(rawDeviceList); _credToken = CredentialManager.RequestCredentials(); if (_credToken == null) { var msg = $"Action {ActionName} canceled by user."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } }
public override void RunCommand(string rawDeviceList) { var connOps = new ConnectionOptions { EnablePrivileges = true }; var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { Parallel.ForEach(devlist, (device) => { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); return; } var remote = _wmiServices.ConnectToRemoteWmi(device, _wmiServices.RootNamespace, connOps); if (remote != null) { _wmiServices.ForceRebootRemoteDevice(device, remote); } else { Logger.LogWarning($"There was an error connecting to WMI namespace on {device}", null); ResultConsole.AddConsoleLine($"There was an error connecting to WMI namespace on {device}"); } }); } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void RunCommand(string rawDeviceList) { var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { Parallel.ForEach(devlist, (device) => { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine( $"Device {device} failed connection verification. Added to failed list."); return; } var sc = new ServiceController("CMRCService", device); sc.Start(); ResultConsole.AddConsoleLine($"CMRCService started on {device}"); }); } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } catch (Exception ex) { ResultConsole.AddConsoleLine($"Ended with error: {ex.Message}"); Logger.LogError(ex.Message, ex); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void OpenUserInterfaceElement(string rawDeviceList) { _parsedListCache = ParseDeviceList(rawDeviceList); var sccmRegHackContext = new SccmRegHackOptionViewModel(); _windowService.ShowDialog <SccmRegHackOptionsPrompt>(sccmRegHackContext); if (!sccmRegHackContext.Result) { var msg = $"Action {ActionName} canceled by user."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } _remoteAccessEnabled = sccmRegHackContext.RemoteAccessEnabled; _requiresUserApproval = sccmRegHackContext.RequiresUserApproval; _showConnectionBanner = sccmRegHackContext.ShowConnectionBanner; _showTaskBarIcon = sccmRegHackContext.ShowTaskbarIcon; _allowAccessOnUnattended = sccmRegHackContext.AllowAccessOnUnattended; _allowLocalAdministratorsToRemoteControl = sccmRegHackContext.AllowLocalAdministratorsToRemoteControl; }
public override void OpenUserInterfaceElement(string rawDeviceList) { _parsedListCache = ParseDeviceList(rawDeviceList); fileCopyContextCache = new FileCopyPromptViewModel(); _windowService.ShowDialog <FileCopyPrompt>(fileCopyContextCache); if (!fileCopyContextCache.Result) { var msg = $"Action {ActionName} canceled by user."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } if (string.IsNullOrWhiteSpace(fileCopyContextCache.FilePath)) { var msg = $"Action {ActionName} aborted: source path was empty."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } }
public override void RunCommand(string rawDeviceList) { var connOps = new ConnectionOptions { EnablePrivileges = true }; var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); try { foreach (var device in devlist) { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); continue; } var remote = _wmiServices.ConnectToRemoteWmi(device, _wmiServices.RootNamespace, connOps); if (remote != null) { ObjectQuery query = new SelectQuery("Win32_ComputerSystem"); var searcher = new ManagementObjectSearcher(remote, query); ManagementObjectCollection queryCollection = null; try { queryCollection = searcher.Get(); } catch (Exception e) { Logger.LogWarning($"QueryCollection returned with exception.", e); ResultConsole.AddConsoleLine($"QueryCollection returned with exception {e.Message}"); continue; } if (queryCollection.Count == 0) { Logger.LogWarning($"Query returned null or empty result list for device {device}", null); ResultConsole.AddConsoleLine($"Query returned null or empty result list for device {device}"); continue; } foreach (ManagementObject resultobject in queryCollection) { ResultConsole.AddConsoleLine($"{device} returned model ID {resultobject["Model"]}"); } } else { var msg = ($"There was an error connecting to WMI namespace on {device}"); Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); } } } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
private void CallbackMethod() { var failedlist = new List <string>(); try { Parallel.ForEach(_parsedListCache, (device) => { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); return; } var fileName = fileCopyContextCache.FilePath.Split(new char[] { '\\' }).Last(); string destPath; if (string.IsNullOrWhiteSpace(fileCopyContextCache.DestinationPath)) { destPath = $"\\\\{device}\\C$\\"; } else { if (fileCopyContextCache.DestinationPath.StartsWith("\\")) { fileCopyContextCache.DestinationPath = fileCopyContextCache.DestinationPath.Remove(0, 1); } if (fileCopyContextCache.DestinationPath.EndsWith("\\")) { fileCopyContextCache.DestinationPath = fileCopyContextCache.DestinationPath.Remove(fileCopyContextCache.DestinationPath.Length - 1, 1); } destPath = $"\\\\{device}\\C$\\{fileCopyContextCache.DestinationPath}\\"; } ResultConsole.AddConsoleLine($"Copying file {fileName} to device {device}"); try { if (FileAndFolderServices.ValidateDirectoryExists(device, fileCopyContextCache.DestinationPath, ActionName, Logger)) { File.Copy(fileCopyContextCache.FilePath, destPath + fileName, fileCopyContextCache.Overwrite); Logger.LogMessage($"Copied file {fileName} to {destPath}"); } else if (fileCopyContextCache.CreateDestination) { Logger.LogMessage($"Creating directory {fileCopyContextCache.DestinationPath}"); Directory.CreateDirectory(destPath); Thread.Sleep(100); File.Copy(fileCopyContextCache.FilePath, destPath + fileName, fileCopyContextCache.Overwrite); } else { ResultConsole.AddConsoleLine("Unable to copy, destination doesn't exist."); Logger.LogMessage("Unable to copy, destination doesn't exist."); } } catch (Exception e) { ResultConsole.AddConsoleLine($"Unable to copy file {fileName}. Error: {e.Message}"); Logger.LogWarning($"Unable to copy file {fileName}.", e); } }); } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
private void CallbackMethod() { var failedlist = new List <string>(); try { Parallel.ForEach(_parsedListCache, (device) => { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); return; } var remote = _wmi.ConnectToRemoteWmi(device, _wmi.RootNamespace, new ConnectionOptions()); if (remote != null) { var searcher = new ManagementObjectSearcher(remote, new ObjectQuery(ProfileQuery)); var collection = searcher.Get(); var profiles = new List <ManagementObject>(); foreach (ManagementObject profileObject in collection) { CancellationToken.Token.ThrowIfCancellationRequested(); var queryObjDate = profileObject?["LastUseTime"]; if (queryObjDate == null) { continue; } var date = ManagementDateTimeConverter.ToDateTime(queryObjDate.ToString()); if (DateTime.Now.DayOfYear - date.DayOfYear >= _dayCountCache) { profiles.Add(profileObject); } } var countText = $"{profiles.Count} profile{(profiles.Count == 1 ? String.Empty : "s")} slated for deletion.\n" + $"Deleting profiles older than {_dayCountCache} days on device {device}."; ResultConsole.AddConsoleLine(countText); try { Parallel.ForEach(profiles, (queryObj) => { CancellationToken.Token.ThrowIfCancellationRequested(); var path = queryObj["LocalPath"]; queryObj.Delete(); queryObj.Dispose(); ResultConsole.AddConsoleLine($"Delete {path} from device {device} done."); }); } catch (Exception e) { Logger.LogError($"Deleting profiles on device {device} returned an error. {e.Message}", e); } } }); } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }
public override void RunCommand(string rawDeviceList) { var devlist = ParseDeviceList(rawDeviceList); var failedlist = new List <string>(); var connOps = new ConnectionOptions { EnablePrivileges = true }; try { foreach (var device in devlist) { CancellationToken.Token.ThrowIfCancellationRequested(); if (!NetworkServices.VerifyDeviceConnectivity(device)) { failedlist.Add(device); ResultConsole.Instance.AddConsoleLine($"Device {device} failed connection verification. Added to failed list."); continue; } var remote = _wmiServices.ConnectToRemoteWmi(device, _wmiServices.RootNamespace, connOps); if (remote != null) { ObjectQuery query = new SelectQuery("Win32_OperatingSystem"); var searcher = new ManagementObjectSearcher(remote, query); var queryCollection = searcher.Get(); foreach (var resultobject in queryCollection) { var ro = resultobject as ManagementObject; // Obtain in-parameters for the method var inParams = ro.GetMethodParameters("Win32Shutdown"); // Add the input parameters. inParams["Flags"] = 4; try { // Execute the method and obtain the return values. var outParams = ro.InvokeMethod("Win32Shutdown", inParams, null); ResultConsole.AddConsoleLine($"Returned with value {_wmiServices.GetProcessReturnValueText(Convert.ToInt32(outParams["ReturnValue"]))}"); } catch (Exception e) { ResultConsole.AddConsoleLine($"Error running {ActionName} due to a .Net ManagementExcept error. There are likely no users logged on!"); Logger.LogWarning($"Error running {ActionName} due to a .Net ManagementExcept error.", e); } } } else { Logger.LogWarning($"There was an error connecting to WMI namespace on {device}", null); ResultConsole.AddConsoleLine($"There was an error connecting to WMI namespace on {device}"); } } } catch (OperationCanceledException e) { ResetCancelToken(ActionName, e); } if (failedlist.Count > 0) { WriteToFailedLog(ActionName, failedlist); } }