Ejemplo n.º 1
0
        private void StartReboot(string host, ref DataGridViewRow row)
        {
            object rebootResult = string.Empty;
            string rebootStatus = string.Empty;
            Color  resultColor  = Color.Black;

            try
            {
                DgvUtils.SetRowValue(ref row, WUCollums.Status, "OS Connecting");

                ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", host));
                scope.Options.EnablePrivileges = true;
                scope.Options.Timeout          = TimeSpan.FromSeconds(10);
                scope.Connect();

                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                using (ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query))
                {
                    foreach (ManagementObject mo in mos.Get())
                    {
                        ManagementBaseObject inParams = mo.GetMethodParameters("Win32Shutdown");
                        inParams["Flags"]    = "6";
                        inParams["Reserved"] = "0";

                        ManagementBaseObject outParams = mo.InvokeMethod("Win32Shutdown", inParams, null);

                        rebootResult = outParams["returnValue"];

                        if (Convert.ToInt32(rebootResult).Equals(0))
                        {
                            resultColor  = Color.Black;
                            rebootStatus = "OS Reboot completed";
                            rebootResult = "";
                            DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, false);
                        }
                        else
                        {
                            resultColor  = Color.Red;
                            rebootStatus = "OS Reboot error";
                            rebootResult = "Error: " + rebootResult;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultColor  = Color.Red;
                rebootStatus = "OS Reboot error";
                rebootResult = ex.Message;
            }
            finally
            {
                EndReboot(ref row);

                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.OperationResults, resultColor);
                DgvUtils.SetRowValue(ref row, WUCollums.Status, rebootStatus);
                DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, rebootResult);
            }
        }
Ejemplo n.º 2
0
        private void StartShutdownInternalViaDCOM(string host, ref DataGridViewRow row)
        {
            object shutdownResult = string.Empty;
            string shutdownStatus = string.Empty;
            Color  resultColor    = Color.Black;

            try
            {
                DgvUtils.SetRowValue(ref row, WUCollums.Status, "OS Connecting");

                ManagementScope scope = GetWmiManagementScope(host);

                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                using (ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query))
                {
                    foreach (ManagementObject mo in mos.Get())
                    {
                        // https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32shutdown-method-in-class-win32-operatingsystem
                        // 5 - Forced Shutdown (1 + 4) - Shuts down the computer to a point where it is safe to turn off the power. (All file buffers are flushed to disk, and all running processes are stopped.) Users see the message, It is now safe to turn off your computer.
                        ManagementBaseObject inParams = mo.GetMethodParameters("Win32Shutdown");
                        inParams["Flags"]    = "5";
                        inParams["Reserved"] = "0";

                        ManagementBaseObject outParams = mo.InvokeMethod("Win32Shutdown", inParams, null);

                        shutdownResult = outParams["returnValue"];

                        if (Convert.ToInt32(shutdownResult).Equals(0))
                        {
                            resultColor    = Color.Black;
                            shutdownStatus = "OS Force Shutdown completed";
                            shutdownResult = "";
                        }
                        else
                        {
                            resultColor    = Color.Red;
                            shutdownStatus = "OS Force Shutdown error";
                            shutdownResult = "Error: " + shutdownResult;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultColor    = Color.Red;
                shutdownStatus = "OS Force Shutdown error";
                shutdownResult = ex.Message;
            }
            finally
            {
                EndShutdown(ref row);

                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.OperationResults, resultColor);
                DgvUtils.SetRowValue(ref row, WUCollums.Status, shutdownStatus);
                DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, shutdownResult);
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty);
            }
        }
Ejemplo n.º 3
0
        public void StartHostReadinessViaDCOM(string host, ref DataGridViewRow row, bool hasActiveResources)
        {
            DateTime          lastBootDate = DateTime.MinValue;
            ServiceHostStatus automaticServicesStatus;

            DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, Color.Black);
            DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty);
            DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty);
            DgvUtils.SetRowValue(ref row, WUCollums.ServicesRunning, string.Empty);
            DgvUtils.SetRowValue(ref row, WUCollums.Cluster, false);

            try
            {
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, "OS Connecting");
                ManagementScope scope = GetWmiManagementScope(host);

                lastBootDate            = GetLasBoot(scope);
                automaticServicesStatus = GetAutomaticServiceStatus(scope);
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, lastBootDate.ToString("dd/MM/yyyy HH:mm"));
                DgvUtils.SetRowValue(ref row, WUCollums.Cluster, automaticServicesStatus.IsClustered);
                // Added as string to not broke the ordering in the Datagridview
                DgvUtils.SetRowValue(ref row, WUCollums.ServicesRunning, automaticServicesStatus.ServicesRunning.ToString());

                if (hasActiveResources && automaticServicesStatus.IsClustered)
                {
                    DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Executing remote powershell...");
                    string fqdnHostName         = $"{host}.{GetFqdnDomain(scope)}";
                    string resultOutput         = string.Empty;
                    int    activeResourcesCount = 0;

                    using (PsManager ps = new PsManager(fqdnHostName))
                    {
                        activeResourcesCount = ps.GetActiveClusterResources().Count;
                        resultOutput         = $"Clustered resources qty: [{activeResourcesCount}]";
                        if (activeResourcesCount != 0)
                        {
                            resultOutput = $"{resultOutput} - Resource Names: [{string.Join(", ", ps.GetActiveClusterResources().ToArray())}]";
                        }

                        DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, resultOutput);
                    }
                }
            }
            catch (Exception ex)
            {
                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, Color.Red);
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, ex.Message);
            }
            finally
            {
                EndReadiness(ref row);
            }
        }
Ejemplo n.º 4
0
        private void StartRebootInternalViaPsExec(string host, ref DataGridViewRow row)
        {
            object rebootResult = string.Empty;
            string rebootStatus = string.Empty;
            Color  resultColor  = Color.Black;

            try
            {
                DgvUtils.SetRowValue(ref row, WUCollums.Status, "OS Connecting");

                StreamReader executionOutput    = null;
                bool         rebootIsInProgress = false;

                OperSystemUtils operUtils  = new OperSystemUtils(remoteOperationsUsesDCOM);
                bool            execResult = operUtils.ExecProcessViaPsExec(host, "shutdown.exe", "/r /t 0", out executionOutput);

                if (execResult)
                {
                    string resultContent = executionOutput.ReadToEnd();
                    rebootIsInProgress = (resultContent == null || resultContent.Trim().Length == 0);

                    if (rebootIsInProgress)
                    {
                        resultColor  = Color.Black;
                        rebootStatus = "OS Reboot completed";
                        rebootResult = "";
                        DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, false);
                    }
                    else
                    {
                        resultColor  = Color.Red;
                        rebootStatus = "OS Reboot error";
                        rebootResult = "Error: " + resultContent;
                    }
                }
            }
            catch (Exception ex)
            {
                resultColor  = Color.Red;
                rebootStatus = "OS Reboot error";
                rebootResult = ex.Message;
            }
            finally
            {
                EndReboot(ref row);

                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.OperationResults, resultColor);
                DgvUtils.SetRowValue(ref row, WUCollums.Status, rebootStatus);
                DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, rebootResult);
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty);
            }
        }
Ejemplo n.º 5
0
        private void StartGetLastBoot(string host, ref DataGridViewRow row)
        {
            string lastBoot    = string.Empty;
            Color  resultColor = Color.Black;

            try
            {
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, "OS Connecting");

                DateTime date = DateTime.MinValue;

                ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", host));
                scope.Options.EnablePrivileges = true;
                scope.Options.Timeout          = TimeSpan.FromSeconds(10);

                if (Credentials.IsAlternativeCredentialEnabled)
                {
                    scope.Options.Username = Credentials.UserName;
                    scope.Options.Password = Credentials.Password;
                }

                scope.Connect();

                ObjectQuery query = new ObjectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem");
                using (ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query))
                {
                    foreach (ManagementObject mo in mos.Get())
                    {
                        date = ParseCIMDateTime(mo["LastBootUpTime"].ToString());
                    }
                }

                resultColor = Color.Black;
                lastBoot    = date.ToString("dd/MM/yyyy HH:mm");
            }
            catch (Exception ex)
            {
                resultColor = Color.Red;
                lastBoot    = ex.Message;
            }
            finally
            {
                EndGetLastBoot(ref row);

                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, resultColor);
                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, lastBoot);
            }
        }
Ejemplo n.º 6
0
        public void BeginShutdown(DataGridViewRow row, string host)
        {
            lock (listItemsActionRow)
            {
                if (!listItemsActionRow.Contains(row))
                {
                    DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, Color.Black);
                    DgvUtils.SetRowValue(ref row, WUCollums.Status, "OS Preparing");
                    //SetLastBootRowStyleFont(ref row, new Font(defaultCellStyle, FontStyle.Regular));

                    listItemsActionRow.Add(row);

                    RowMethodDelegate de = new RowMethodDelegate(StartShutdown);
                    de.BeginInvoke(host, ref row, null, null);
                }
            }
        }
Ejemplo n.º 7
0
 public void StartHostReadinessViaPsExec(string host, ref DataGridViewRow row, bool hasActiveResources)
 {
     try
     {
         DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, Color.Black);
         DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty);
         DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty);
         DgvUtils.SetRowValue(ref row, WUCollums.Status, "Starting");
         DateTime lastBootDate = GetLastBootDateTimeObject(host, ref row);
         DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, lastBootDate.ToString("dd/MM/yyyy HH:mm"));
         DgvUtils.SetRowValue(ref row, WUCollums.Status, "Finish");
     }
     catch (Exception ex)
     {
         DgvUtils.SetRowStyleForeColor(ref row, WUCollums.LastBoot, Color.Red);
         DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, ex.Message);
     }
     finally
     {
         EndReadiness(ref row);
     }
 }