private void btnExport_Click(object sender, EventArgs e) { Logger.EnteringMethod(); if (saveExport.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { btnExport.Enabled = false; Logger.Write("Saving to :" + saveExport.FileName); System.IO.StreamWriter writer = new System.IO.StreamWriter(saveExport.OpenFile(), Encoding.UTF8); writer.WriteLine("Computer Name;OU Name;Wsus Client ID;Last Logon;OS Name;OS Service Pack;OS Version;Service Status;Service Start Mode;Is In Wsus;Duplicate Wsus Client ID"); foreach (DataGridViewRow row in dtGrdVResult.SelectedRows) { ADComputer tempComputer = (ADComputer)row.Cells["ComputerName"].Value; writer.WriteLine( tempComputer.Name + ";" + tempComputer.OUName + ";" + tempComputer.SusClientID + ";" + tempComputer.LastLogon + ";" + tempComputer.OSName + ";" + tempComputer.OSServicePack + ";" + tempComputer.OSVersion + ";" + tempComputer.WuAuServiceStatus + ";" + tempComputer.IsInWsus.ToString() + ";" + tempComputer.HasDuplicateWsusClientID.ToString()); } writer.Flush(); writer.Close(); btnExport.Enabled = true; } }
private void btnStart_Click(object sender, EventArgs e) { Logger.EnteringMethod(); btnStart.Enabled = false; btnClose.Enabled = false; bool result = false; this.Cursor = Cursors.WaitCursor; foreach (DataGridViewRow row in dgvComputers.Rows) { ADComputer computer = (ADComputer)row.Cells["Computer"].Value; result = computer.CleanSoftwareDistributionFolder(_username, _password); Logger.Write(computer.Name + " : " + result); if (result) { row.Cells["Result"].Value = resMan.GetString("Succeeded"); } else { row.Cells["Result"].Value = resMan.GetString("Failed"); } } this.Cursor = Cursors.Default; btnStart.Enabled = true; btnClose.Enabled = true; }
private void SendCommandToComputer(object data) { ADComputer computer = null; DataForComputer dataForComputer = (DataForComputer)data; DataGridViewRow row = dataForComputer.Row; String[] options = dataForComputer.Options; System.Threading.CountdownEvent countDown = dataForComputer.CountDown; try { Action startAction = () => { lock (dtGrvComputers) { if (!_closing && !_aborting) { row.Cells["Status"].Value = resMan.GetString("SendingCommand"); computer = (ADComputer)row.Cells["Computer"].Value; } } Logger.Write("Will try to send InstallPendingUpdates on : " + computer.Name); }; if (!_closing && !_aborting) { this.Invoke(startAction); } ADComputer.InstallPendingUpdatesResult result = ADComputer.InstallPendingUpdatesResult.FailToSendCommand; if (!_closing && !_aborting) { result = computer.InstallPendingUpdates(options, Username, Password); } Action endAction = () => { Logger.Write("Result for " + computer.Name + " is : " + result.ToString()); lock (dtGrvComputers) { if (!_closing && !_aborting) { row.Cells["Status"].Value = resMan.GetString(result.ToString()); } } }; if (!_closing && !_aborting) { this.Invoke(endAction); } } catch (Exception) { } finally { countDown.Signal(); } }
private void UpdateSusClientIDColor() { Logger.EnteringMethod(); foreach (DataGridViewRow row in dtGrdVResult.Rows) { ADComputer computer = (ADComputer)row.Cells["ComputerName"].Value; if (computer.HasDuplicateWsusClientID) { row.Cells["SusClientID"].Style.BackColor = Color.OrangeRed; row.Cells["SusClientID"].Style.SelectionBackColor = Color.OrangeRed; } else { row.Cells["SusClientID"].Style.BackColor = Color.White; row.Cells["SusClientID"].Style.SelectionBackColor = Color.Teal; } } }
private void SearchOnlineComputers(object data) { SearchOnlineComputerData computerData = (SearchOnlineComputerData)data; object[] args = new object[2]; DataGridViewRow currentRow = computerData.Row; try { if (!cancelSearchOnlineComputers) { ADComputer computer = (ADComputer)currentRow.Cells["ComputerName"].Value; args[0] = currentRow; args[1] = "No"; if (computer.Ping(100)) { args[1] = "Yes"; } } } catch (Exception) { } try { if (!cancelSearchOnlineComputers) { DisplayOnLineStatusDelegate DisplayOnLineStatusHandler = new DisplayOnLineStatusDelegate(DisplayOnLineStatus); DisplayOnLineStatusHandler(args); } } catch (Exception) { } finally { computerData.countDown.Signal(); } }
private void ChangeServiceState(object parameters) { ServiceParameters serviceParams = (ServiceParameters)parameters; object[] args = new object[3]; args[0] = resMan.GetString("FailedToSendCommand"); args[1] = serviceParams.RowIndex; args[2] = "ServiceStatus"; if (!_closing && !_aborting && !_wrongCrendentialsWatcher.IsAbortRequested) { lock (_ChangeServiceStateLocker) { try { if (!_closing && !_aborting && !_wrongCrendentialsWatcher.IsAbortRequested) { ADComputer computer = new ADComputer(dtGrdVResult.Rows[serviceParams.RowIndex].Cells["ComputerName"].Value.ToString()); bool result = false; if (serviceParams.Command == "RestartService") { computer.ManageService(serviceParams.ServiceName, "StopService", serviceParams.Login, serviceParams.Password); result = computer.ManageService(serviceParams.ServiceName, "StartService", serviceParams.Login, serviceParams.Password); args[0] = result ? resMan.GetString("Running") : resMan.GetString("Failed"); } else { result = computer.ManageService(serviceParams.ServiceName, serviceParams.Command, serviceParams.Login, serviceParams.Password); args[0] = result ? ((serviceParams.Command == "StartService") ? resMan.GetString("Running") : resMan.GetString("Stopped")) : resMan.GetString("Failed"); } } else { args[0] = resMan.GetString("Aborted"); } } catch (UnauthorizedAccessException) { _wrongCrendentialsWatcher.IsWrongCredentials = true; if (!_wrongCrendentialsWatcher.ContinueWithFailedCredentials) { if (MessageBox.Show(resMan.GetString("CredentialFailed"), resMan.GetString("FailedToConnect"), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { Logger.Write("interrupt on failed credentials."); _wrongCrendentialsWatcher.IsAbortRequested = true; args[0] = resMan.GetString("Aborted"); } else { Logger.Write("Continue with bad credentials."); _wrongCrendentialsWatcher.ContinueWithFailedCredentials = true; args[0] = resMan.GetString("FailedToConnect"); } } else { args[0] = resMan.GetString("FailedToConnect"); } } catch (Exception ex) { Logger.Write(ex.Message); args[0] = resMan.GetString("FailedToSendCommand"); } } } else { args[0] = resMan.GetString("Aborted"); } UpdateRow(args); }
private void ResetSusClientID(object obj) { ASyncClientParameters parameters = (ASyncClientParameters)obj; try { object[] args = new object[3]; ADComputer computer = (ADComputer)dtGrdVResult.Rows[parameters.RowIndex].Cells["ComputerName"].Value; args[0] = resMan.GetString("FailedToSendCommand"); args[1] = parameters.RowIndex; args[2] = "SusClientID"; if (!_closing && !_aborting && !chkBxDontPing.Checked && !computer.Ping((int)nupPingTimeout.Value)) { args[0] = resMan.GetString("Unreachable"); } else { if (!_closing && !_aborting) { lock (_ResetSusClientIDLocker) { try { if (!_closing && !_aborting && !_wrongCrendentialsWatcher.IsAbortRequested) { if (computer.ResetWsusClientID(parameters.Login, parameters.Password)) { args[0] = resMan.GetString("Reset"); } else { args[0] = resMan.GetString("Failed"); } } else { args[0] = resMan.GetString("Aborted"); } } catch (UnauthorizedAccessException) { _wrongCrendentialsWatcher.IsWrongCredentials = true; if (!_wrongCrendentialsWatcher.ContinueWithFailedCredentials) { if (MessageBox.Show(resMan.GetString("CredentialFailed"), resMan.GetString("FailedToConnect"), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { Logger.Write("interrupt on failed credentials."); _wrongCrendentialsWatcher.IsAbortRequested = true; args[0] = resMan.GetString("Aborted"); } else { Logger.Write("Continue with bad credentials."); _wrongCrendentialsWatcher.ContinueWithFailedCredentials = true; args[0] = resMan.GetString("FailedToConnect"); } } else { args[0] = resMan.GetString("FailedToConnect"); } } catch (Exception ex) { Logger.Write(ex.Message); args[0] = resMan.GetString("FailedToSendCommand"); } } } else { args[0] = resMan.GetString("Aborted"); } } UpdateRow(args); } catch (Exception) { } finally { parameters.CountEvent.Signal(); } }
private void QueryWuAuServiceStatus(object obj) { char[] separator = new char[1] { ';' }; ASyncClientParameters parameters = (ASyncClientParameters)obj; try { int index = parameters.RowIndex; string login = parameters.Login; string password = parameters.Password; object[] args = new object[3]; System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Properties.Settings.Default.Language); ADComputer computer = (ADComputer)dtGrdVResult.Rows[index].Cells["ComputerName"].Value; args[0] = resMan.GetString("FailedToConnect"); args[1] = index; args[2] = "ServiceStatus"; if (!_closing && !_aborting && !chkBxDontPing.Checked && !computer.Ping((int)nupPingTimeout.Value)) { args[0] = resMan.GetString("Unreachable"); } else { if (!_closing && !_aborting) { lock (_QueryWuAuSrvStatusLocker) { string result = string.Empty; try { if (!_closing && !_aborting && !_wrongCrendentialsWatcher.IsAbortRequested) { result = computer.QueryWuAuSrvStatus(login, password); if (result.IndexOf(';') != -1) { string[] tab = result.Split(separator); if (tab != null && tab.Length == 2) { result = resMan.GetString(tab[0]) + " " + tab[1]; } } args[0] = result; } else { args[0] = resMan.GetString("Aborted"); } } catch (UnauthorizedAccessException) { _wrongCrendentialsWatcher.IsWrongCredentials = true; if (!_wrongCrendentialsWatcher.ContinueWithFailedCredentials) { if (MessageBox.Show(resMan.GetString("CredentialFailed"), resMan.GetString("FailedToConnect"), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { Logger.Write("interrupt on failed credentials."); _wrongCrendentialsWatcher.IsAbortRequested = true; args[0] = resMan.GetString("Aborted"); } else { Logger.Write("Continue with bad credentials."); _wrongCrendentialsWatcher.ContinueWithFailedCredentials = true; args[0] = resMan.GetString("FailedToConnect"); } } else { args[0] = resMan.GetString("FailedToConnect"); } } catch (Exception ex) { Logger.Write(ex.Message); args[0] = resMan.GetString("FailedToSendCommand"); } } } else { args[0] = resMan.GetString("Aborted"); } } UpdateRow(args); } catch (Exception) { } finally { parameters.CountEvent.Signal(); } }
private void btnSearchComputers_Click(object sender, EventArgs e) { Logger.Write("Will search for computers"); ChangeUIAccess(false); lblProgress.Text = resMan.GetString("SearchingComputerInAD"); lblProgress.Refresh(); _aborting = false; dtGrdVResult.Rows.Clear(); dtGrdVResult.Refresh(); this.Cursor = Cursors.WaitCursor; if (ouSelector.SearchInAllAD) { ADcomputers = ADHelper.GetComputers("LDAP://" + txtBxDomainName.Text); } else { ADcomputers = ADHelper.GetComputers(ouSelector.SelectedOUList); } Logger.Write("Found " + ADcomputers.Count + " computers in domaine " + txtBxDomainName.Text); lblProgress.Text = ADcomputers.Count + resMan.GetString("ComputersFound"); WsusWrapper wsus = WsusWrapper.GetInstance(); computers.Clear(); prgBrSearch.Value = 0; prgBrSearch.Maximum = ADcomputers.Count; lblProgress.Text = resMan.GetString("SearchingComputersInWSUS"); lblProgress.Refresh(); foreach (ADComputer computer in ADcomputers) { if (!_closing && !_aborting) { ADComputer tempComputer = new ADComputer(computer.Name, computer.OUName, computer.LastLogon, computer.OSName, computer.OSServicePack, computer.OSVersion); try { Logger.Write("Searching " + computer.Name + " in WSUS..."); Microsoft.UpdateServices.Administration.IComputerTarget target = wsus.GetComputerTargetByName(computer.Name); if (target != null) { tempComputer.IsInWsus = true; Logger.Write("...Found it."); } else { Logger.Write("...not found."); tempComputer.IsInWsus = false; } } catch (Exception) { } if (!computers.Contains(tempComputer)) { computers.Add(tempComputer); } prgBrSearch.Value++; prgBrSearch.Refresh(); } else { break; } } DisplayComputers(); ChangeUIAccess(true); this.Cursor = Cursors.Default; }
private void ctxMnuComputer_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { Logger.EnteringMethod(); Credentials cred = Credentials.GetInstance(); lblCredentials.Text = cred.CredentialNotice; Logger.Write(lblCredentials); List <ADComputer> targetComputers = new List <ADComputer>(); FrmRemoteExecution remoteExecution = new FrmRemoteExecution(); Logger.Write(dtGrdVResult.SelectedRows.Count + " computers selected."); foreach (DataGridViewRow row in dtGrdVResult.SelectedRows) { Logger.Write("Selecting " + row.Cells[0].Value.ToString()); targetComputers.Add((ADComputer)row.Cells[0].Value); } ctxMnuComputer.Hide(); switch (e.ClickedItem.Name) { case "GetWsusClientID": btnGetSusClientID_Click(null, null); break; case "ResetWsusClientID": btnResetWsusClientID_Click(null, null); break; case "StartService": Logger.Write("Start Service"); if (dtGrdVResult.SelectedRows.Count != 0) { if (cred.InitializeCredential() == false) { return; } } _aborting = false; ManageService("wuauserv", "StartService", cred.Login, cred.Password); break; case "StopService": Logger.Write("Stop Service"); if (dtGrdVResult.SelectedRows.Count != 0) { if (cred.InitializeCredential() == false) { return; } } _aborting = false; ManageService("wuauserv", "StopService", cred.Login, cred.Password); break; case "RestartService": Logger.Write("Restart Service"); if (dtGrdVResult.SelectedRows.Count != 0) { if (cred.InitializeCredential() == false) { return; } } _aborting = false; ManageService("wuauserv", "RestartService", cred.Login, cred.Password); break; case "QueryServiceStatus": Logger.Write("Querying Service Status"); this.Cursor = Cursors.WaitCursor; _wrongCrendentialsWatcher = new WrongCredentialsWatcher(); _aborting = false; GetWuAuServiceStatus(); this.Cursor = Cursors.Default; break; case "ShowWindowsUpdateLog": Logger.Write("Show WindowsUPdateLog"); if (cred.InitializeCredential() == false) { return; } this.Cursor = Cursors.WaitCursor; ADComputer computer = new ADComputer(dtGrdVResult.SelectedRows[0].Cells["ComputerName"].Value.ToString()); if (!chkBxDontPing.Checked && !computer.Ping((int)nupPingTimeout.Value)) { MessageBox.Show(resMan.GetString("ComputerUnreachable")); } else { computer.OpenWindowsUpdateLog(cred.Login, cred.Password); } this.Cursor = Cursors.Default; break; case "SendDetectNow": Logger.Write("Send DetectNow"); remoteExecution.Show(this); remoteExecution.SendDetectNow(targetComputers, cred.Login, cred.Password); break; case "SendReportNow": Logger.Write("Send ReportNow"); remoteExecution.Show(this); remoteExecution.SendReportNow(targetComputers, cred.Login, cred.Password); break; case "SendRebootNow": Logger.Write("Send RebootNow"); FrmRebootCommand rebootCommand = new FrmRebootCommand(targetComputers, cred.Login, cred.Password); rebootCommand.Show(); break; default: Logger.Write("**** Unable to detect the command"); break; } }
private void ctxMnuComputer_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { Logger.EnteringMethod(e.ClickedItem.Name); List <ADComputer> targetComputers = new List <ADComputer>(); FrmRemoteExecution remoteExecution = new FrmRemoteExecution(); foreach (DataGridViewRow row in dGVComputer.SelectedRows) { if (row.Visible) { targetComputers.Add((ADComputer)row.Cells["ComputerName"].Value); } } ctxMnuComputer.Hide(); Credentials cred = Credentials.GetInstance(); if (targetComputers.Count != 0) { if (cred.InitializeCredential() == false) { return; } } lblCredentialNotice.Text = cred.CredentialNotice; Logger.Write(lblCredentialNotice); switch (e.ClickedItem.Name) { case "DetectNow": remoteExecution.Show(this); remoteExecution.SendDetectNow(targetComputers, cred.Login, cred.Password); break; case "ReportNow": remoteExecution.Show(this); remoteExecution.SendReportNow(targetComputers, cred.Login, cred.Password); break; case "RebootNow": FrmRebootCommand rebootCommand = new FrmRebootCommand(targetComputers, cred.Login, cred.Password); rebootCommand.Show(); break; case "ShowPendingUpdates": System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); System.Security.SecureString securePassword = new System.Security.SecureString(); startInfo.FileName = Environment.CurrentDirectory + @"\ShowPendingUpdates.exe"; if (!System.IO.File.Exists(startInfo.FileName)) { Logger.Write("Unable to find : " + startInfo.FileName); MessageBox.Show(resMan.GetString("UnableToFindShowPendingUpdates")); } else { startInfo.Arguments = targetComputers[0].Name; if (!string.IsNullOrEmpty(cred.Login) && !string.IsNullOrEmpty(cred.Password)) { foreach (Char letter in cred.Password) { securePassword.AppendChar(letter); } startInfo.UserName = cred.Login; startInfo.Password = securePassword; startInfo.Domain = ADHelper.GetDomainName(); } startInfo.UseShellExecute = false; startInfo.WorkingDirectory = Environment.CurrentDirectory; try { System.Diagnostics.Process.Start(startInfo); } catch (Exception ex) { Logger.Write("**** " + ex.Message); MessageBox.Show(ex.Message); } } break; case "InstallPendingUpdates": FrmInstallPendingUpdatesNow installPendingUpdates = new FrmInstallPendingUpdatesNow(); installPendingUpdates.Username = cred.Login; installPendingUpdates.Password = cred.Password; installPendingUpdates.Computers = targetComputers; installPendingUpdates.ShowDialog(); break; case "ShowCurrentLogonUser": this.Cursor = Cursors.WaitCursor; ADComputer remoteComputer = new ADComputer(dGVComputer.SelectedRows[0].Cells["ComputerName"].Value.ToString()); string logonUser = remoteComputer.GetCurrentLogonUser(cred.Login, cred.Password); if (!string.IsNullOrEmpty(logonUser)) { MessageBox.Show(resMan.GetString("CurrentLogonUserIs") + " : " + logonUser); } else { MessageBox.Show(resMan.GetString("UnableToGetLogonUser")); } this.Cursor = Cursors.Default; break; case "ShowWindowsUpdateLog": this.Cursor = Cursors.WaitCursor; ADComputer computer = new ADComputer(dGVComputer.SelectedRows[0].Cells["ComputerName"].Value.ToString()); if (!computer.Ping(100)) { MessageBox.Show(resMan.GetString("ComputerUnreachable")); } else { computer.OpenWindowsUpdateLog(cred.Login, cred.Password); } this.Cursor = Cursors.Default; break; case "CleanSoftwareDistributionFolder": this.Cursor = Cursors.WaitCursor; FrmCleanSoftwareDistributionFolder frmCleanSoftwareDistributionFolder = new FrmCleanSoftwareDistributionFolder(targetComputers, cred.Login, cred.Password); frmCleanSoftwareDistributionFolder.ShowDialog(); this.Cursor = Cursors.Default; break; default: break; } }
private void SendCommandToComputer(object args) { CommandParameters parameters = (CommandParameters)args; DataGridViewRow row = parameters.Row; ADComputer computer = parameters.TargetComputer; if (!_wrongCrendentialsWatcher.IsAbortRequested && !_closing) { try { DisplayMessage(row, resMan.GetString("Pinging")); if (computer.Ping(100)) { Logger.Write(computer.Name + " Ping ok"); DisplayMessage(row, resMan.GetString("SendingCommand")); ConnectionOptions connOptions = new ConnectionOptions(); connOptions.Impersonation = ImpersonationLevel.Impersonate; connOptions.EnablePrivileges = true; if (parameters.Login != null && parameters.Password != null && computer.Name.ToLower() != localComputerName.ToLower()) { connOptions.Username = parameters.Login; connOptions.Password = parameters.Password; } ManagementScope mgmtScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computer.Name), connOptions); if (!_wrongCrendentialsWatcher.IsAbortRequested && !_closing) { lock (wmiLock) { try { if (!_wrongCrendentialsWatcher.IsAbortRequested && !_closing) { mgmtScope.Connect(); } else { DisplayMessage(row, resMan.GetString("Aborted")); ChangeBackColor(row, Color.Gainsboro); _failed++; DisplayProgress(); } } catch (UnauthorizedAccessException) { _wrongCrendentialsWatcher.IsWrongCredentials = true; DisplayMessage(row, resMan.GetString("Exception")); ChangeBackColor(row, Color.Silver); _failed++; DisplayProgress(); if (!_wrongCrendentialsWatcher.ContinueWithFailedCredentials) { if (MessageBox.Show(resMan.GetString("CredentialFailed"), resMan.GetString("FailedToConnect"), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { Logger.Write("interrupt on failed credentials."); _wrongCrendentialsWatcher.IsAbortRequested = true; DisplayMessage(row, resMan.GetString("Aborted")); ChangeBackColor(row, Color.Gainsboro); } else { Logger.Write("Continue with bad credentials."); _wrongCrendentialsWatcher.ContinueWithFailedCredentials = true; } } } } if (mgmtScope.IsConnected && !_closing) { ObjectGetOptions objectGetOptions = new ObjectGetOptions(); ManagementPath mgmtPath = new ManagementPath("Win32_Process"); ManagementClass processClass = new ManagementClass(mgmtScope, mgmtPath, objectGetOptions); ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); inParams["CommandLine"] = parameters.CommandLine; if (!_wrongCrendentialsWatcher.IsAbortRequested) { processClass.InvokeMethod("Create", inParams, null); processClass.Dispose(); mgmtPath = null; objectGetOptions = null; mgmtScope = null; connOptions = null; DisplayMessage(row, resMan.GetString("CommandSent")); ChangeBackColor(row, Color.LightGreen); _success++; DisplayProgress(); } } } else { DisplayMessage(row, resMan.GetString("Aborted")); ChangeBackColor(row, Color.Gainsboro); } } else { Logger.Write(computer.Name + " don't Ping"); DisplayMessage(row, resMan.GetString("Noping")); ChangeBackColor(row, Color.Silver); _failed++; DisplayProgress(); } } catch (Exception ex) { Logger.Write("**** " + computer.Name + " throw an execption : " + ex.Message); DisplayMessage(row, resMan.GetString("Exception")); ChangeBackColor(row, Color.Silver); _failed++; DisplayProgress(); } finally { countDown.Signal(); } } else { countDown.Signal(); } }