private void ProcessRemoteShutdownOpearation(string operationName, ShutdownCommands shutdownStyle) { IFavorite fav = this.GetSelectedFavorite(); if (fav == null) { return; } const string QUESTION_TEMPLATE = "Are you sure you want to {0} this machine: {1}\r\n" + "Operation requires administrator priviledges and can take a while."; var question = String.Format(QUESTION_TEMPLATE, operationName, fav.ServerName); string title = Program.Resources.GetString("Confirmation"); var confirmResult = MessageBox.Show(question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult != DialogResult.Yes) { return; } var options = new Tuple <ShutdownCommands, IFavorite>(shutdownStyle, fav); var shutdownTask = Task.Factory.StartNew(new Func <object, string>(TryPerformRemoteShutdown), options); shutdownTask.ContinueWith(new Action <Task <string> >(ShowRebootResult), TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Send a shutdown command to a (remote) computer. /// </summary> /// <param name="machineName"> The machinename or ip-address of computer to send shutdown command to. </param> /// <param name="shutdownCommand"> Shutdown type command. </param> /// <param name="credentials"> Optional network credentials for the (remote) computer. </param> /// <returns> 0 if the shutdown was succesfully send, else another integer value. </returns> /// <exception cref="ManagementException">An unhandled managed error occured.</exception> /// <exception cref="UnauthorizedAccessException">Access was denied.</exception> public static Int32 ForceShutdown(String machineName, ShutdownCommands shutdownCommand, Credential credential = null) { Int32 result = -1; ConnectionOptions options = new ConnectionOptions(); if (credential != null) { options.EnablePrivileges = true; options.Username = credential.UserNameWithDomain; options.Password = credential.Password; } ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", machineName), options); scope.Connect(); SelectQuery query = new SelectQuery("Win32_OperatingSystem"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); foreach (ManagementObject os in searcher.Get()) { ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown"); inParams["Flags"] = (Int32)shutdownCommand; ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null); result = Convert.ToInt32(outParams.Properties["returnValue"].Value); return(result); } return(result); }
/// <summary> /// Send a shutdown command to a (remote) computer. /// </summary> /// <param name="machineName">The machinename or ip-address of computer to send shutdown command to.</param> /// <param name="shutdownCommand">Shutdown type command.</param> /// <param name="credentials">Optional network credentials for the (remote) computer.</param> /// <returns>0 if the shutdown was succesfully send, else another integer value.</returns> /// <exception cref="ManagementException">An unhandled managed error occured.</exception> /// <exception cref="UnauthorizedAccessException">Access was denied.</exception> private static int ForceShutdown(string machineName, ShutdownCommands shutdownCommand, NetworkCredential credentials) { var options = CreateOptions(credentials); var scope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", machineName), options); scope.Connect(); var query = new SelectQuery("Win32_OperatingSystem"); using (var searcher = new ManagementObjectSearcher(scope, query)) { foreach (ManagementObject os in searcher.Get()) { var inParams = os.GetMethodParameters("Win32Shutdown"); inParams["Flags"] = (int)shutdownCommand; using (var outParams = os.InvokeMethod("Win32Shutdown", inParams, null)) { return(Convert.ToInt32(outParams.Properties["returnValue"].Value)); } } } return(-1); }
internal static bool ForceShutdown(IPersistence persistence, IFavorite favorite, ShutdownCommands shutdownCommand) { var guarded = new GuardedSecurity(persistence, favorite.Security); var security = guarded.GetResolvedCredentials(); var credentials = new NetworkCredential(security.UserName, security.Password, security.Domain); return(ForceShutdown(favorite.ServerName, shutdownCommand, credentials) == 0); }
/// <summary> /// Send a shutdown command to a (remote) computer. /// </summary> /// <param name="machineName">The machinename or ip-address of computer to send shutdown command to.</param> /// <param name="shutdownCommand">Shutdown type command.</param> /// <param name="credentials">Optional network credentials for the (remote) computer.</param> /// <returns>0 if the shutdown was succesfully send, else another integer value.</returns> /// <exception cref="ManagementException">An unhandled managed error occured.</exception> /// <exception cref="UnauthorizedAccessException">Access was denied.</exception> private static int ForceShutdown(String machineName, ShutdownCommands shutdownCommand, NetworkCredential credentials) { ConnectionOptions options = CreateOptions(credentials); var scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", machineName), options); scope.Connect(); var query = new SelectQuery("Win32_OperatingSystem"); using (var searcher = new ManagementObjectSearcher(scope, query)) { foreach (ManagementObject os in searcher.Get()) { ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown"); inParams["Flags"] = (Int32)shutdownCommand; using (ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null)) { return Convert.ToInt32(outParams.Properties["returnValue"].Value); } } } return -1; }
internal static bool ForceShutdown(IFavorite favorite, ShutdownCommands shutdownCommand) { ISecurityOptions security = favorite.Security.GetResolvedCredentials(); var credentials = new NetworkCredential(security.UserName, security.Password, security.Domain); return ForceShutdown(favorite.ServerName, shutdownCommand, credentials) == 0; }
/// <summary> /// Send a shutdown command to a (remote) computer. /// </summary> /// <param name="machineName"> The machinename or ip-address of computer to send shutdown command to. </param> /// <param name="shutdownCommand"> Shutdown type command. </param> /// <param name="credentials"> Optional network credentials for the (remote) computer. </param> /// <returns> 0 if the shutdown was succesfully send, else another integer value. </returns> /// <exception cref="ManagementException">An unhandled managed error occured.</exception> /// <exception cref="UnauthorizedAccessException">Access was denied.</exception> public static Int32 ForceShutdown(String machineName, ShutdownCommands shutdownCommand, Credential credential = null) { Int32 result = -1; ConnectionOptions options = new ConnectionOptions(); if (credential != null) { options.EnablePrivileges = true; options.Username = credential.UserNameWithDomain; options.Password = credential.Password; } ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", machineName), options); scope.Connect(); SelectQuery query = new SelectQuery("Win32_OperatingSystem"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); foreach (ManagementObject os in searcher.Get()) { ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown"); inParams["Flags"] = (Int32) shutdownCommand; ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null); result = Convert.ToInt32(outParams.Properties["returnValue"].Value); return result; } return result; }
private void ProcessRemoteShutdownOpearation(string operationName, ShutdownCommands shutdownStyle) { IFavorite fav = this.GetSelectedFavorite(); if (fav == null) return; const string QUESTION_TEMPLATE = "Are you sure you want to {0} this machine: {1}\r\n" + "Operation requires administrator priviledges and can take a while."; var question = String.Format(QUESTION_TEMPLATE, operationName, fav.ServerName); string title = Program.Resources.GetString("Confirmation"); var confirmResult = MessageBox.Show(question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult != DialogResult.Yes) return; var options = new Tuple<ShutdownCommands, IFavorite>(shutdownStyle, fav); var shutdownTask = Task.Factory.StartNew(new Func<object, string>(TryPerformRemoteShutdown), options); shutdownTask.ContinueWith(new Action<Task<string>>(ShowRebootResult), TaskScheduler.FromCurrentSynchronizationContext()); }