private void RunReportAndDelete(string host, string fileName) { Util.ConnectionTest.TestWmi(targDev); bool taskExists; user.AdminRightsExist = false; var ad = new AdTasks(targDev); ad.CheckLocale(); ad.CheckExistingRights(NoComboAdd, DevOutputFalse); taskExists = Util.QueryTaskScheduler(targDev, true); if (!user.AdminRightsExist & host == connection.HostName) { FileSystem.DeleteFile(fileName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin); targDev.mainForm.WriteToConsole(string.Format( "{1}*::CONFIRMED REMOVAL::*{1}" + "Deleted: {0}{1}", Util.Tools.ExtractSubstring(fileName, @"\"), Environment.NewLine)); } else if (user.AdminRightsExist & !taskExists) { ad.RemoveRights(); targDev.mainForm.WriteToConsole(string.Format( "{1}::RIGHTS STILL PRESENT::{1}" + "Deleted:{0}{1}", Util.Tools.ExtractSubstring(fileName, @"\"), Environment.NewLine)); } else if (taskExists) { targDev.mainForm.WriteToConsole("Removal Task Still exists"); targDev.mainForm.WriteToConsole(targDev.host.removalTask.Status); } targDev.mainForm.WriteToConsole(string.Format("Host Name: {0}{2}User ID:{1}{2}", connection.HostName, user.UserID, Environment.NewLine)); }
private void btnRemove_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtHostIP.Text)) { Util.MsgBox.Info("Host IP cannot be blank."); return; } targ.Dispose(); ToggleButtonsOnOff(false); user.AdminRightsExist = false; if (PreConnectCheck(txtHostIP.Text.Trim())) { bool checkRights, addAdminToCombo = false; const bool devOutput = false; if (string.IsNullOrWhiteSpace(user.UserID)) { checkRights = false; //User did not specify a user id, query host for admins. addAdminToCombo = true; //Add found admins to comboAdminList } else { checkRights = true; //user id specified, check if they have admin rights. } try { adTasksRemove = new AdTasks(targ); adTasksRemove.CheckLocale(); remoteTasksRemove = new RemoteTasks(targ); remoteTasksRemove.GetHostInfo(); } catch (Exception connectError) { eventLogs.LogError(connectError); ClearForm(false); return; } //Only runs when User ID is blank. //Checking rights and adding admin users to comboAdminList if (!checkRights) { adTasksRemove.CheckExistingRights(addAdminToCombo, devOutput); if (comboAdminList.Items.Count == 0) //No admin's found { MessageBox.Show("No user's found with admin rights."); } else { ToggleButtonsOnOff(false); //display comboAdminList box to select user to remove. comboAdminList.Visible = true; selectUserBox.Visible = true; } } else { adTasksRemove.CheckExistingRights(addAdminToCombo, devOutput); if (user.AdminRightsExist) { bool result = MsgBoxConfirmRemoval(); if (result) { adTasksRemove.RemoveRights(); adTasksRemove.CheckExistingRights(false, false); string jobFilePath = string.Format( @"\\{0}\c$\windows\tasks\{1}.job", connection.HostIP, user.UserID); try { if (File.Exists(jobFilePath)) //Delete existing scheduled tasks { File.Delete(jobFilePath); } } catch (Exception) { throw; } //**Can implement a final check to make sure rights //were removed. In tests/production use this has //not failed yet. StatusReport(3); CopyToClipboard(); } else if (!user.AdminRightsExist) //User specified ID does not have admin rights. { Util.MsgBox.Info(string.Format( "User {0} does not have admin rights on {1}.", user.UserID, connection.HostName)); } else { StatusReport(2); } } else { Util.MsgBox.Info(string.Format( "User {0} does not have admin rights on {1}.", user.UserID, connection.HostName)); } } } else { Util.MsgBox.Info("Failed to connect to host. Is host online?"); ClearForm(false); } if (!comboAdminList.Visible) { ToggleButtonsOnOff(true); } }