private void Act_StopThreadAndReboot(DataGridViewRow row) { if (dataGridView.InvokeRequired) { DataGridViewRowDelegate de = new DataGridViewRowDelegate(Act_StopThreadAndReboot); dataGridView.Invoke(de, row); } else { lock (row) { string host = row.Cells["Host"].Value.ToString(); osManager.BeginReboot(row, host); lock (threadList) { if (threadList.ContainsKey(row)) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "WU Aborting, wait ..."); threadList[row].Abort(null); Thread.Sleep(5000); } } if (chkBoxEnablePingAfterBoot.Checked == true) { pinger.BeginStart(host, row); } } } }
private void Act_ResetAuthorizationOnWsus(DataGridViewRow row) { try { DgvUtils.SetRowValue(ref row, WUCollums.Status, "Starting"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Executing reset authorization (wuauclt.exe /resetauthorization /detectnow)"); OperSystemUtils operUtils = new OperSystemUtils(ConfigurationFileHelper.RemoteOperationsUsesDCOM); string host = row.Cells["Host"].Value.ToString(); bool operationResult = operUtils.ResetAuthorizationOnWsus(host); if (operationResult) { DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reset authorization executed successfully"); } else { DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reset authorization NOT executed"); } DgvUtils.SetRowValue(ref row, WUCollums.Status, "Finish"); } catch (Exception erro) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "ThreadError"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, erro.Message); } }
private void Act_StartCheckReboot(DataGridViewRow row) { try { DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, false); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty); OperSystemUtils operUtils = new OperSystemUtils(); string host = row.Cells["Host"].Value.ToString(); bool isRequiredBoot = operUtils.IsRebootRequired(host); if (isRequiredBoot) { DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, true); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reboot Required"); } else { DgvUtils.SetRowValue(ref row, WUCollums.RebootRequired, false); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reboot NOT Required"); } } catch (Exception erro) { DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, erro.Message); } }
private void CheckBatchExecutionErrors(DataGridViewRow row) { try { string batchStatus = DgvUtils.GetRowValue(ref row, WUCollums.Status).ToString(); string operationResult = DgvUtils.GetRowValue(ref row, WUCollums.OperationResults).ToString(); if (string.Equals(batchStatus, "ThreadError", StringComparison.CurrentCultureIgnoreCase)) { throw new Exception($"BatchExecutionError: {operationResult}"); } } catch { } }
private void Act_CountUpdatesExecutor(object rowObject) { DataGridViewRow row = (DataGridViewRow)rowObject; try { OperSystemUtils operSys = new OperSystemUtils(); 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(); try { operSys.CopyPsExec(hostName); } catch (Exception ex) { DgvUtils.SetRowValue(ref row, WUCollums.Status, "StartError"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message); this.Sys_RemoveThreadRow(ref row); return; } StreamReader reader = operSys.ExecWua("/countUpdates", 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_InstallUpdatesBatch(DataGridViewRow row, DateTime executionTime) { // Waiting execution schedule bool isBackgroundSet = false; do { if (!isBackgroundSet) { row.DefaultCellStyle.BackColor = Color.LightGreen; row.DefaultCellStyle.SelectionBackColor = Color.Green; DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, $"Waiting"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, $"Execution Scheduled for: {executionTime.ToString("hh:mm tt - dd/MM/yy")}"); isBackgroundSet = true; } else { // Add waiting thread to avoid high CPU consuption Thread.Sleep(30000); } } while (executionTime > DateTime.Now); DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Waiting thread"); DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, string.Empty); semaphore.WaitOne(); lock (threadList) { if (threadList.ContainsKey(row)) { return; } } Thread newThread = new Thread(Act_InstallUpdatesBatchExecutor); lock (threadList) { threadList.Add(row, newThread); } newThread.IsBackground = true; newThread.Start(row); }
private void Act_InstallUpdates(DataGridViewRow row) { lock (threadList) { if (threadList.ContainsKey(row)) { return; } } DgvUtils.SetRowValue(ref row, WUCollums.Status, "Initializing"); Thread newThread = new Thread(Act_InstallUpdatesExecutor); lock (threadList) { threadList.Add(row, newThread); } newThread.IsBackground = true; newThread.Start(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); } }