public static void ShutdownComputer(ShutdownComputerParameters parameters) { try { const string query = @"SELECT * FROM Win32_OperatingSystem WHERE Primary=TRUE"; WmiHelpers.ForEach(parameters.ComputerName, query, obj => { var inParams = obj.GetMethodParameters(@"Win32ShutdownTracker"); inParams[@"Comment"] = parameters.Comment; inParams[@"Flags"] = parameters.Flags; inParams[@"ReasonCode"] = 1; // Maintenance inParams[@"Timeout"] = parameters.Timeout; var outParams = obj.InvokeMethod(@"Win32ShutdownTracker", inParams, null); var result = (null == outParams ? null : outParams[@"ReturnValue"]) as uint?; if (null == result || 0 != result) { var message = string.Format(@"Failed to reboot {0} with error {1}", parameters.ComputerName, result); GlobalLogging.WriteLine(Logging.LogSeverity.Error, message); NotificationWindow.NotificationWindow.AddErrorMessage(message); } return(true); }, true); } catch (UnauthorizedAccessException) { var message = string.Format(@"Failed to reboot {0}, permission denied", parameters.ComputerName); GlobalLogging.WriteLine(Logging.LogSeverity.Error, message); NotificationWindow.NotificationWindow.AddErrorMessage(message); } catch (ManagementException e) { var message = string.Format("Failed to reboot {0}, WMI Error\n{1}", parameters.ComputerName, e.Message); GlobalLogging.WriteLine(Logging.LogSeverity.Error, message); NotificationWindow.NotificationWindow.AddErrorMessage(message); } catch (Exception e) { var message = string.Format("Failed to reboot {0}, unexpected error\n{1}\n{2}", parameters.ComputerName, e.GetType( ).Name, e.Message); GlobalLogging.WriteLine(Logging.LogSeverity.Error, message); NotificationWindow.NotificationWindow.AddErrorMessage(message); } }
public static void ShutdownComputer( ShutdownComputerParameters parameters ) { try { const string query = @"SELECT * FROM Win32_OperatingSystem WHERE Primary=TRUE"; WmiHelpers.ForEach( parameters.ComputerName, query, obj => { var inParams = obj.GetMethodParameters( @"Win32ShutdownTracker" ); inParams[@"Comment"] = parameters.Comment; inParams[@"Flags"] = parameters.Flags; inParams[@"ReasonCode"] = 1; // Maintenance inParams[@"Timeout"] = parameters.Timeout; var outParams = obj.InvokeMethod( @"Win32ShutdownTracker", inParams, null ); var result = (null == outParams ? null : outParams[@"ReturnValue"]) as uint?; if( null == result || 0 != result ) { var message = string.Format( @"Failed to reboot {0} with error {1}", parameters.ComputerName, result ); GlobalLogging.WriteLine( Logging.LogSeverity.Error, message ); NotificationWindow.NotificationWindow.AddErrorMessage( message ); } return true; }, true ); } catch( UnauthorizedAccessException ) { var message = string.Format( @"Failed to reboot {0}, permission denied", parameters.ComputerName ); GlobalLogging.WriteLine( Logging.LogSeverity.Error, message ); NotificationWindow.NotificationWindow.AddErrorMessage( message ); } catch( ManagementException e ) { var message = string.Format( "Failed to reboot {0}, WMI Error\n{1}", parameters.ComputerName, e.Message ); GlobalLogging.WriteLine( Logging.LogSeverity.Error, message ); NotificationWindow.NotificationWindow.AddErrorMessage( message ); } catch( Exception e ) { var message = string.Format( "Failed to reboot {0}, unexpected error\n{1}\n{2}", parameters.ComputerName, e.GetType( ).Name, e.Message ); GlobalLogging.WriteLine( Logging.LogSeverity.Error, message ); NotificationWindow.NotificationWindow.AddErrorMessage( message ); } }