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 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 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 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); } }