示例#1
0
        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(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;
            }
        }