private void AddScomServers()
        {
            _worker            = new BackgroundWorker();
            btnConnect.Enabled = false;
            _worker.DoWork    += (object doWorkSender, DoWorkEventArgs doWorkEventArgs) =>
            {
                doWorkEventArgs.Result = Helper.GetScomServers();
            };

            _worker.RunWorkerCompleted += (object runWorkerCompletedSender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs) =>
            {
                this.scomCompleted = true;
                // Hide loading gif.
                this.busyMessageLabel.Visible = this.busyPictureBox.Visible = false;
                btnConnect.Enabled            = true;
                tbxComputerName.Enabled       = tbxPassword.Enabled = tbxUsername.Enabled = cbxDrive.Enabled = true;
                if (runWorkerCompletedEventArgs.Error != null)// || !connectionSuccessful)
                {
                    string message = runWorkerCompletedEventArgs.Error != null ? runWorkerCompletedEventArgs.Error.Message : "Could not connect to the computer specified with the credentials supplied";
                    MessageBox.Show(message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                Collection <PSObject> result;
                if (runWorkerCompletedEventArgs.Result != null)
                {
                    result = (Collection <PSObject>)runWorkerCompletedEventArgs.Result;
                }
                else
                {
                    MessageBox.Show("Unable get list of servers from SCOM server, please add manually.", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                foreach (var computerName in result)
                {
                    string key = this.CredentialsRequiredPrefix + computerName;
                    if (!RemoteSystemInfos.Servers.ContainsKey(computerName.ToString()) &&
                        !this.serverList.Items.Contains(key) && !this.serverList.Items.Contains(computerName.ToString()))
                    {
                        this.serverList.Items.Add(key);
                        RemoteSystemInfos.AddOrUpdateEmptyServer(key, computerName.ToString());
                    }
                }

                // add localhost
                if (!RemoteSystemInfos.Servers.ContainsKey(RemoteSystemInfo.LocalhostName) && !this.serverList.Items.Contains(RemoteSystemInfo.LocalhostName))
                {
                    this.serverList.Items.Add(RemoteSystemInfo.LocalhostName);
                    RemoteSystemInfos.AddOrUpdate(RemoteSystemInfo.LocalhostName, string.Empty, string.Empty, string.Empty);
                }
            };

            _worker.RunWorkerAsync();
        }
        private void addServerButton_Click(object sender, EventArgs e)
        {
            var           selectedItems = this.serverList.SelectedItems;
            List <string> computerNames = new List <string>();

            if (selectedItems != null && selectedItems.Count > 1)
            {
                foreach (var selectedItemO in selectedItems)
                {
                    string selectedItem = (string)selectedItemO;
                    if (selectedItem.StartsWith(this.CredentialsRequiredPrefix))
                    {
                        selectedItem = selectedItem.Replace(this.CredentialsRequiredPrefix, string.Empty);
                    }

                    computerNames.Add(selectedItem);
                }
            }
            else
            {
                string computerName = tbxComputerName.Text;
                if (computerName == null)
                {
                    return;
                }

                computerNames.Add(computerName);
            }

            foreach (var computerName in computerNames)
            {
                if (RemoteSystemInfos.Servers.ContainsKey(computerName))
                {
                    MessageBox.Show(string.Format("Server already added: {0}\nDelete to re-add", computerName));
                    return;
                }

                string username    = tbxUsername.Text;
                string password    = tbxPassword.Text;
                string driveLetter = cbxDrive.Text;

                busyMessageLabel.Text    = "Trying to connect to " + computerName;
                busyMessageLabel.Visible = busyPictureBox.Visible = true;
                tbxComputerName.Enabled  = tbxUsername.Enabled = tbxPassword.Enabled = cbxDrive.Enabled = false;
                _worker            = new BackgroundWorker();
                btnConnect.Enabled = false;

                _worker.DoWork += (object doWorkSender, DoWorkEventArgs doWorkEventArgs) =>
                {
                    if (!string.IsNullOrEmpty(computerName) && Helper.IsComputerReachable(computerName))
                    {
                        doWorkEventArgs.Result = Helper.ConnectToServer(computerName, username, password);
                    }
                };

                _worker.RunWorkerCompleted += (object runWorkerCompletedSender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs) =>
                {
                    // Hide busy animation.
                    this.busyPictureBox.Visible = this.busyMessageLabel.Visible = false;
                    tbxComputerName.Enabled     = tbxUsername.Enabled = tbxPassword.Enabled = cbxDrive.Enabled = true;
                    btnConnect.Enabled          = true;
                    if (runWorkerCompletedEventArgs.Error != null)// || !connectionSuccessful)
                    {
                        string message = runWorkerCompletedEventArgs.Error != null ? runWorkerCompletedEventArgs.Error.Message : "Could not connect to the computer specified with the credentials supplied";
                        MessageBox.Show(message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        return;
                    }

                    bool result = runWorkerCompletedEventArgs.Result != null && (bool)runWorkerCompletedEventArgs.Result;
                    if (!result)
                    {
                        MessageBox.Show("Unable to connect to server", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        return;
                    }

                    this.tbxUsername.Items.Add(username);
                    autoCompleteStringCollection.Add(username);
                    this.tbxUsername.AutoCompleteCustomSource = autoCompleteStringCollection;
                    RemoteSystemInfos.AddOrUpdate(computerName, username, password, driveLetter);

                    var remoteSystemInfo = RemoteSystemInfos.Servers[computerName];
                    if (remoteSystemInfo.IISVersion >= 7 && Helper.IisVersion < 7)
                    {
                        MessageBox.Show(
                            "To migrate sites on a web server 7 and higher, run the tool from a system running Vista or above. ",
                            System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        RemoteSystemInfos.Servers.Remove(computerName);
                    }
                    else if (remoteSystemInfo.Error)
                    {
                        RemoteSystemInfos.Servers.Remove(computerName);
                    }
                    else
                    {
                        this.serverList.Items.Add(computerName);
                    }

                    if (!remoteSystemInfo.Error && RemoteSystemInfos.EmptyServers.ContainsKey(this.CredentialsRequiredPrefix + computerName))
                    {
                        RemoteSystemInfos.EmptyServers.Remove(this.CredentialsRequiredPrefix + computerName);
                        this.serverList.Items.Remove(this.CredentialsRequiredPrefix + computerName);
                    }
                };

                _worker.RunWorkerAsync();
            }
        }