private void Act_InstallUpdatesExecutor(object rowObject) { DataGridViewRow row = (DataGridViewRow)rowObject; try { OperSystemUtils operSys = new OperSystemUtils(ConfigurationFileHelper.RemoteOperationsUsesDCOM); row.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 160); row.DefaultCellStyle.SelectionBackColor = Color.Coral; DgvUtils.SetRowValue(ref row, WUCollums.Status, "Starting"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty); DgvUtils.SetRowValue(ref row, WUCollums.Progress, 0); string hostName = row.Cells["Host"].Value.ToString(); operSys.CopyPsExec(hostName); StreamReader reader = operSys.ExecWua("/install /showProgress", hostName); while (!reader.EndOfStream) { string line = reader.ReadLine(); Act_InstallUpdatesInterpretor(ref row, line); } } catch (Exception ex) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "ThreadError"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message); } finally { this.Sys_RemoveThreadRow(ref row); } }
private void Act_InstallUpdatesInterpretor(ref DataGridViewRow row, string line) { string[] lineParts = line.Split(':'); string operation = lineParts[0]; string[] parameters = new string[] { string.Empty }; if (lineParts.Length > 1) { parameters = lineParts[1].Split('|'); } WUAOperations operations = (WUAOperations)Enum.Parse(typeof(WUAOperations), operation); switch (operations) { case WUAOperations.WUA_Starting: { //WUA_Starting DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Starting"); break; } case WUAOperations.WUA_IsBusy: { //WUA_IsBusy DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Is Busy"); break; } case WUAOperations.WUA_FindingUpdates: { //WUA_FindingUpdates DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Finding Updates"); break; } case WUAOperations.WUA_NoApplicableUpdates: { //WUA_NoApplicableUpdates DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU No Applicable Updates"); break; } case WUAOperations.WUA_UpdateItem: { //WUA_UpdateItem:N|Title DgvUtils.SetRowValue(ref row, WUCollums.Updates, Convert.ToInt32(parameters[0])); break; } case WUAOperations.WUA_DownloadingStarted: { //WUA_DownloadingStarted DgvUtils.SetRowProgressColor(ref row, Color.Tomato, Color.Red); DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Downloading Started"); break; } case WUAOperations.WUA_DownloadingProgress: { //WUA_DownloadingProgress:N|N%|T% DgvUtils.SetRowValue(ref row, WUCollums.Progress, Convert.ToInt32(parameters[2])); break; } case WUAOperations.WUA_DownloadingCompleted: { //WUA_DownloadingCompleted:ResultCode DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Downloading Completed"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Downloading result: " + parameters[0]); break; } case WUAOperations.WUA_InstallationStarted: { //WUA_InstallationStarted DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Installation Started"); DgvUtils.SetRowValue(ref row, WUCollums.Progress, 0); DgvUtils.SetRowProgressColor(ref row, Color.MediumSpringGreen, Color.Green); break; } case WUAOperations.WUA_InstallationProgress: { //WUA_InstallationProgress:N|N%|T% DgvUtils.SetRowValue(ref row, WUCollums.Progress, Convert.ToInt32(parameters[2])); break; } case WUAOperations.WUA_InstallationCompleted: { //WUA_InstallationCompleted:ResultCode|RebootRequired DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Installation Completed"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Installation result: " + parameters[0]); DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, Convert.ToBoolean(parameters[1])); break; } case WUAOperations.WUA_InstallationResult: { //WUA_InstallationResult:N|ResultCode|RebootRequired break; } case WUAOperations.WUA_Finish: { //WUA_Finish DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Finish"); break; } case WUAOperations.WUA_InternalError: { //WUA_InternalError:Message DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Internal Error"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, parameters[0]); break; } case WUAOperations.WUA_Pending: { DgvUtils.SetRowValue(ref row, WUCollums.Updates, lineParts[1]); break; } } }
private void Act_InstallUpdatesExecutor(object rowObject) { DataGridViewRow row = (DataGridViewRow)rowObject; try { row.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 160); row.DefaultCellStyle.SelectionBackColor = Color.Coral; DgvUtils.SetRowValue(ref row, WUCollums.Status, "Starting"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty); DgvUtils.SetRowValue(ref row, WUCollums.Progress, 0); string hostName = row.Cells["Host"].Value.ToString(); string destFile1 = string.Format(@"\\{0}\Admin$\System32\Wua.exe", hostName); string destFile2 = string.Format(@"\\{0}\Admin$\System32\Interop.WUApiLib.dll", hostName); try { File.Copy("Wua.exe", destFile1, true); File.Copy("Interop.WUApiLib.dll", destFile2, true); } catch (Exception ex) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "StartError"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message); this.Sys_RemoveThreadRow(ref row); return; } using (Process process = new Process()) { process.StartInfo.FileName = "psexec.exe"; process.StartInfo.Arguments = string.Format(@"-s -accepteula \\{0} Wua.exe /install /showProgress", hostName); process.StartInfo.ErrorDialog = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); StreamReader reader = process.StandardOutput; while (!reader.EndOfStream) { string line = reader.ReadLine(); Act_InstallUpdatesInterpretor(ref row, line); } } } catch (Exception ex) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "ThreadError"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message); } finally { this.Sys_RemoveThreadRow(ref row); } }
private void Act_InstallUpdatesBatchExecutor(object rowObject) { bool isRebootRequired = false; DateTime lastReboot = new DateTime(); DataGridViewRow row = (DataGridViewRow)rowObject; string host = row.Cells["Host"].Value.ToString(); string operResult = string.Empty; Stopwatch sw = new Stopwatch(); sw.Start(); try { int minutesRebootLastRebootCheck = (int)numUpDownMinutesReboot.Value; int rebootCheckAttempts = (int)numUpDownAttemptsNumber.Value; // Failover Cluster Services if (chkBoxCluster.Checked) { DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Moving_ClusterResources (1/5)"); osManager.StartHostReadiness(host, ref row, chkBoxClusterResources.Checked); bool isClustered = Convert.ToBoolean(DgvUtils.GetRowValue(ref row, WUCollums.Cluster)); if (isClustered) { DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Executing remote powershell to move resources..."); osManager.FailOverClusterNode(host); } } // Install Updates DgvUtils.SetRowStyleForeColor(ref row, WUCollums.Status, Color.Black); DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Inst_Updates (2/5)"); Act_InstallUpdatesExecutor(row); //On error it stops the batch CheckBatchExecutionErrors(row); isRebootRequired = Convert.ToBoolean(DgvUtils.GetRowValue(ref row, WUCollums.RebootRequired)); int updatesInstalled = Convert.ToInt32(DgvUtils.GetRowValue(ref row, WUCollums.Updates)); // Reboot DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Rebooting (3/5)"); if (isRebootRequired) { int attempts = 0; string errorRebootMessage = string.Empty; osManager.StartReboot(host, ref row); pinger.BeginStart(host, row); // Testing server when it comes online do { //Wait 30 seconds Thread.Sleep(30000); if (attempts <= rebootCheckAttempts) { try { lastReboot = osManager.GetLastBootDateTimeObject(host, ref row); DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty); DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, lastReboot.ToString("dd/MM/yyyy HH:mm")); } catch { // returns a generic date just to keep the execution lastReboot = new DateTime(2000, 1, 1); } } else { errorRebootMessage = $"Number of attempts has been reached"; break; } attempts++; DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, $"Host has not completed the reboot yet...(attempt: {attempts})"); // Check if the last boot time attribute } while ((int)(DateTime.Now - lastReboot).TotalMinutes >= minutesRebootLastRebootCheck); // Stop the execution when it has reached the attempts limit if (!string.IsNullOrEmpty(errorRebootMessage)) { throw new Exception(errorRebootMessage); } DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Server Online"); pinger.BeginStop(row); osManager.StartHostReadiness(host, ref row, chkBoxClusterResources.Checked); } else { DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reboot not required"); } // Count Updates if (updatesInstalled != 0) { DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Count_Updates (4/5)"); // Give more time to rpc services start Thread.Sleep(40000); Act_CountUpdatesExecutor(row); //On error it stops the batch CheckBatchExecutionErrors(row); } DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Finished (5/5)"); } catch (Exception ex) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "ThreadError"); DgvUtils.SetRowStyleForeColor(ref row, WUCollums.Status, Color.Red); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message); } finally { sw.Stop(); operResult = DgvUtils.GetRowValue(ref row, WUCollums.OperationResults).ToString(); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Format("Duration: {0} min {1}", (int)sw.Elapsed.Duration().TotalMinutes, operResult)); if (semaphore.Release() == (int)numUpDownTreads.Value) { semaphore.Dispose(); } this.Sys_RemoveThreadRow(ref row); } }