private void frmSimpleTasks_Load(object sender, EventArgs e) { lstPCs.ShowCheckBoxes = true; lstPCs.ListOnly = true; lblTZ.Text = "(" + TimeZoneInfo.Local.StandardName + ")"; lstType.Items.Add("Run a program"); lstType.Items.Add("Edit Registry"); lstType.Items.Add("Reset Windows Update Client"); lstRegAction.Items.Add("Add / Update value"); lstRegAction.Items.Add("Delete value"); lstRegAction.Items.Add("Delete tree"); lstRegValueType.Items.Add("REG_SZ"); lstRegValueType.Items.Add("REG_DWORD"); lstRegValueType.Items.Add("REG_QWORD"); lstRegValueType.Items.Add("REG_MULTI_SZ"); lstRegValueType.Items.Add("REG_EXPAND_SZ"); lstRegValueType.Items.Add("REG_BINARY"); lstRegRoot.Items.Add("HKEY_LOCAL_MACHINE"); lstRegRoot.Items.Add("HKEY_USERS"); panelRunApp.Dock = DockStyle.Fill; panelReg.Dock = DockStyle.Fill; panelRunApp.Visible = panelRunApp.Enabled = false; panelReg.Visible = panelReg.Enabled = false; lstType.SelectedIndex = 0; lstRegAction.SelectedIndex = 0; lstRegValueType.SelectedIndex = 0; lstRegRoot.SelectedIndex = 0; this.Text = "New Simple Task"; lblIntoGroup.Visible = false; if (MachineIDs != null) { foreach (string MID in MachineIDs) { lstPCs.SelectMachineID(MID); } } if (GroupID != null) { lstPCs.Visible = false; lblIntoGroup.Visible = true; lblIntoGroup.Text = "All computers located in the group:\n\n" + PathName; } if (editst != null) { lstPCs.SelectMachineID(editst.MachineID); txtName.Text = editst.Name; if (editst.ExecAfter == null) { chkDontExec.Checked = false; } else { chkDontExec.Checked = true; DTExecBefore.Value = TimeZoneInfo.ConvertTimeFromUtc(editst.ExecAfter.Value, TimeZoneInfo.Local); } switch (editst.Type) { case 1: //run app { lstType.SelectedIndex = 0; SimpleTaskRunProgramm stapp = JsonConvert.DeserializeObject <SimpleTaskRunProgramm>(editst.Data); txtRunArgs.Text = stapp.Parameters; txtRunExec.Text = stapp.Executable; txtRunUser.Text = stapp.User; break; } case 2: //edit registry { lstType.SelectedIndex = 1; SimpleTaskRegistry streg = JsonConvert.DeserializeObject <SimpleTaskRegistry>(editst.Data); lstRegAction.SelectedIndex = streg.Action; lstRegRoot.SelectedIndex = streg.Root; txtRegFolder.Text = streg.Folder; txtRegValueName.Text = streg.Valuename; lstRegValueType.SelectedIndex = streg.ValueType; txtRegValue.Text = streg.Data; break; } case 3: //Reset WU { lstType.SelectedIndex = 2; break; } } this.Text = "Edit Simple Task"; if (editst.ID == -1) { editst = null; } } DTExecBefore.CustomFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.FullDateTimePattern; DTExecBefore.Format = DateTimePickerFormat.Custom; chkDontExec_CheckedChanged(null, null); }
static bool ProcessSimpleTask(Network net, SimpleTask st, out Int64 NewID) { NewID = -1; Status.UpdateMessage(0, "Running Simple Task: " + st.Name); try { switch (st.Type) { case 1: #region Run { SimpleTaskRunProgramm rt = JsonConvert.DeserializeObject <SimpleTaskRunProgramm>(st.Data); if (string.IsNullOrWhiteSpace(rt.Executable) == true) { CompleteTask(net, st, 0xFFF1, "Simple Task Data is invalid."); break; } Process proc = new Process(); if (string.IsNullOrWhiteSpace(rt.User) == false) { PushRunningSessionList sessions = ProgramAgent.CPP.GetActiveTSSessions(); int SessionID = -1; foreach (PushRunningSessionElement session in sessions.Data) { if (rt.User.ToLower() == (session.Domain + "\\" + session.User).ToLower()) { SessionID = session.SessionID; break; } } if (SessionID == -1) { Int64?nid = net.PutSimpleTaskAside(st.ID); if (nid == null) { return(false); } else { NewID = nid.Value; return(true); } } int processid = ProgramAgent.CPP.StartAppAsUserID(Environment.ExpandEnvironmentVariables(rt.Executable), rt.Parameters, SessionID); if (processid == -1) { CompleteTask(net, st, 0xFFF3, "Cannot start process " + rt.Executable); break; } proc = Process.GetProcessById(processid); } else { try { proc.StartInfo.UseShellExecute = false; proc.StartInfo.FileName = Environment.ExpandEnvironmentVariables(rt.Executable); proc.StartInfo.Arguments = rt.Parameters; proc.Start(); } catch { CompleteTask(net, st, 0xFFF3, "Cannot start process " + rt.Executable); break; } } int Counter = 0; do { Thread.Sleep(1000); if (proc.HasExited == true) { break; } Counter++; if (Counter % 120 == 0) { net.Ping(); } } while (Counter < 3600); if (proc.HasExited == false) { proc.Kill(); CompleteTask(net, st, 0xFFF2, "Process has been killed, took too long."); break; } CompleteTask(net, st, proc.ExitCode, "Process completed successfully."); break; } #endregion case 2: #region Registry { SimpleTaskRegistry reg = JsonConvert.DeserializeObject <SimpleTaskRegistry>(st.Data); RegistryKey regroot = null; switch (reg.Root) { case 0: regroot = Registry.LocalMachine; break; case 1: regroot = Registry.Users; break; default: { CompleteTask(net, st, 0xFFF2, "Registry Root is invalid 0x" + reg.Action.ToString("X") + "."); break; } } if (regroot == null) { break; } RegistryValueKind regtype = RegistryValueKind.Unknown; switch (reg.ValueType) { case 0: regtype = RegistryValueKind.String; break; case 1: regtype = RegistryValueKind.DWord; break; case 2: regtype = RegistryValueKind.QWord; break; case 3: regtype = RegistryValueKind.MultiString; break; case 4: regtype = RegistryValueKind.ExpandString; break; case 5: regtype = RegistryValueKind.Binary; break; default: { CompleteTask(net, st, 0xFFF3, "Registry Value Type is invalid 0x" + reg.Action.ToString("X") + "."); break; } } if (regtype == RegistryValueKind.Unknown) { break; } if (string.IsNullOrWhiteSpace(reg.Folder) == true) { CompleteTask(net, st, 0xFFF4, "Registry Root Folder is missing."); break; } reg.Folder = reg.Folder.Trim(); if (reg.Folder.StartsWith("\\") == true) { reg.Folder = reg.Folder.Substring(1, reg.Folder.Length - 1); } if (reg.Folder.EndsWith("\\") == true) { reg.Folder = reg.Folder.Substring(0, reg.Folder.Length - 1); } if (string.IsNullOrWhiteSpace(reg.Folder) == true) { CompleteTask(net, st, 0xFFF5, "Registry Root Folder is missing."); break; } if (reg.Root == 1) //HKEY_USERS { string User = reg.Folder.Split('\\')[0].Trim(); using (RegistryKey r = regroot.OpenSubKey(User)) { //user not loaded (logged in) if (r == null) { Int64?nid = net.PutSimpleTaskAside(st.ID); if (nid == null) { return(false); } else { NewID = nid.Value; return(true); } } } } bool Success = false; string SuccessText = ""; switch (reg.Action) { case 0: //Add / Update key { using (RegistryKey k = regroot.CreateSubKey(reg.Folder)) { if (k != null) { switch (regtype) { case RegistryValueKind.String: case RegistryValueKind.ExpandString: k.SetValue(reg.Valuename, reg.Data, regtype); SuccessText = "Registry insertation completed successfully (REG_SZ/REG_EXPAND_SZ)."; Success = true; break; case RegistryValueKind.MultiString: k.SetValue(reg.Valuename, reg.Data.Split(new string[] { "\\0" }, StringSplitOptions.None), regtype); SuccessText = "Registry insertation completed successfully (REG_MULTI_SZ)."; Success = true; break; case RegistryValueKind.DWord: { int dword; if (reg.Data.ToLower().StartsWith("0x") == true) { if (int.TryParse(reg.Data.Substring(2, reg.Data.Length - 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out dword) == false) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } } else { if (int.TryParse(reg.Data, out dword) == false) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } } k.SetValue(reg.Valuename, dword, regtype); SuccessText = "Registry insertation completed successfully (REG_DWORD)."; Success = true; } break; case RegistryValueKind.QWord: { Int64 dword; if (reg.Data.ToLower().StartsWith("0x") == true) { if (Int64.TryParse(reg.Data.Substring(2, reg.Data.Length - 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out dword) == false) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } } else { if (Int64.TryParse(reg.Data, out dword) == false) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } } k.SetValue(reg.Valuename, dword, regtype); SuccessText = "Registry insertation completed successfully (REG_QWORD)."; Success = true; } break; case RegistryValueKind.Binary: { if (reg.Data.Length % 2 != 0) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } List <byte> bytedata = new List <byte>(); for (int i = 0; i < reg.Data.Length; i += 2) { int btmp; if (int.TryParse(reg.Data.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out btmp) == false) { CompleteTask(net, st, 0xFFF7, "Invalid data."); break; } bytedata.Add((byte)btmp); } k.SetValue(reg.Valuename, bytedata.ToArray(), regtype); SuccessText = "Registry insertation completed successfully (REG_BINARY)."; Success = true; } break; } } else { CompleteTask(net, st, 0xFFF6, "Cannot open registry " + reg.Folder + "."); break; } } } break; case 1: //Delete value { using (RegistryKey k = regroot.OpenSubKey(reg.Folder, true)) { if (k != null) { k.DeleteValue(reg.Valuename, false); SuccessText = "Registry value deletation completed successfully."; Success = true; } else { CompleteTask(net, st, 0xFFF6, "Cannot open registry " + reg.Folder + "."); break; } } } break; case 2: //Delete directory { string f = reg.Folder.Substring(0, reg.Folder.LastIndexOf('\\')); string v = reg.Folder.Substring(reg.Folder.LastIndexOf('\\') + 1); using (RegistryKey k = regroot.OpenSubKey(f, true)) { if (k != null) { k.DeleteSubKeyTree(v, false); SuccessText = "Registry tree deletation completed successfully."; Success = true; } else { CompleteTask(net, st, 0xFFF6, "Cannot open registry " + f + "."); break; } } } break; default: { CompleteTask(net, st, 0xFFF1, "Registry Action is invalid 0x" + reg.Action.ToString("X") + "."); break; } } if (Success == true) { CompleteTask(net, st, 0, SuccessText); } break; } #endregion case 3: #region Reset WU Client { try { ServiceController scm = new ServiceController("wuauserv"); if (scm.Status != ServiceControllerStatus.Stopped) { if (scm.CanStop == false) { CompleteTask(net, st, 1, "WU Reset: cannot stop Service"); break; } scm.Stop(); int Counter = 0; do { Counter++; Thread.Sleep(1000); if (Counter > 120) { CompleteTask(net, st, 2, "WU Reset: stop Service: timed out"); break; } } while (scm.Status != ServiceControllerStatus.Stopped); Thread.Sleep(1000); if (scm.Status != ServiceControllerStatus.Stopped) { scm.Stop(); Counter = 0; do { Counter++; Thread.Sleep(1000); if (Counter > 120) { CompleteTask(net, st, 3, "WU Reset: stop Service: timed out (2)"); break; } } while (scm.Status != ServiceControllerStatus.Stopped); } if (scm.Status != ServiceControllerStatus.Stopped) { CompleteTask(net, st, 4, "WU Reset: stop Service: didn't stop for the 2nd time"); break; } } string SDPath = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%\\SoftwareDistribution"); if (Directory.Exists(SDPath) == true) { Directory.Delete(SDPath, true); } string USOEXE = Environment.ExpandEnvironmentVariables("%SYSTEMROOT%\\System32\\usoclient.exe"); try { Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = USOEXE; p.StartInfo.Arguments = "RefreshSettings"; p.Start(); p.WaitForExit(30000); } catch { } try { Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = USOEXE; p.StartInfo.Arguments = "StartScan"; p.Start(); p.WaitForExit(30000); } catch { } CompleteTask(net, st, 0, "WU Reset completed successfully"); } catch (Exception ee) { CompleteTask(net, st, 0xFFFF, "SEH: " + ee.ToString()); break; } } break; #endregion default: { CompleteTask(net, st, 0xFFFF, "Does not know how to process Simple Task Type 0x" + st.Type.ToString("X") + "."); break; } } } catch (Exception ee) { CompleteTask(net, st, 0xFFF0, "SEH while processing Simple Task: " + ee.ToString()); return(false); } Status.UpdateMessage(0, "Completed Simple Task: " + st.Name); return(true); }
private void cmdOK_Click(object sender, EventArgs e) { if (txtName.Text.Trim() == "") { MessageBox.Show(this, "Please type in a name.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (lstPCs.CheckedItems.Count == 0 && GroupID == null) { MessageBox.Show(this, "Please select a least one computer.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } DateTime?DontExecBefore = null; if (chkDontExec.Checked == true) { DontExecBefore = DTExecBefore.Value; if (DTExecBefore.Value.Year < 2010) { MessageBox.Show(this, "Invalid date", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } if (GroupID != null) { lstPCs.LoadList(); lstPCs.UncheckItems(); SelectComputers(GroupID.Value); } switch (lstType.SelectedIndex) { case 0: //run app { if (txtRunExec.Text.Trim() == "") { MessageBox.Show(this, "Please enter at least the executable filename.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } SimpleTaskRunProgramm stapp = new SimpleTaskRunProgramm(); stapp.Executable = txtRunExec.Text.Trim(); stapp.Parameters = txtRunArgs.Text.Trim(); stapp.User = txtRunUser.Text.Trim(); RemovePreviousThing(); foreach (ListViewItem lst in lstPCs.CheckedItems) { ComputerData cd = (ComputerData)lst.Tag; Int64? ID = Program.net.SetSimpleTask(txtName.Text.Trim(), cd.MachineID, DontExecBefore, 1, stapp); if (ID == null) { MessageBox.Show(this, "Cannot create SimpleTask on Server: " + Program.net.GetLastError(), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } } if (GroupID != null) { MessageBox.Show(this, "Task" + (lstPCs.GetSelectedCount == 1 ? "" : "s") + " created for " + lstPCs.GetSelectedCount + " PC" + (lstPCs.GetSelectedCount == 1 ? "" : "s"), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Information); } this.DialogResult = DialogResult.OK; this.Close(); break; } case 1: //edit registry { if (txtRegFolder.Text.Trim() == "") { MessageBox.Show(this, "Please enter a folder.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } SimpleTaskRegistry streg = new SimpleTaskRegistry(); streg.Action = lstRegAction.SelectedIndex; streg.Root = lstRegRoot.SelectedIndex; streg.Folder = txtRegFolder.Text.Trim(); streg.Valuename = txtRegValueName.Text.Trim(); streg.ValueType = lstRegValueType.SelectedIndex; streg.Data = txtRegValue.Text; RemovePreviousThing(); foreach (ListViewItem lst in lstPCs.CheckedItems) { ComputerData cd = (ComputerData)lst.Tag; Int64? ID = Program.net.SetSimpleTask(txtName.Text.Trim(), cd.MachineID, DontExecBefore, 2, streg); if (ID == null) { MessageBox.Show(this, "Cannot create SimpleTask on Server: " + Program.net.GetLastError(), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } } if (GroupID != null) { MessageBox.Show(this, "Task" + (lstPCs.GetSelectedCount == 1 ? "" : "s") + " created for " + lstPCs.GetSelectedCount + " PC" + (lstPCs.GetSelectedCount == 1 ? "" : "s"), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Information); } this.DialogResult = DialogResult.OK; this.Close(); break; } case 2: //reset WU { SimpleTaskNix streg = new SimpleTaskNix(); streg.Dummy = ""; RemovePreviousThing(); foreach (ListViewItem lst in lstPCs.CheckedItems) { ComputerData cd = (ComputerData)lst.Tag; Int64? ID = Program.net.SetSimpleTask(txtName.Text.Trim(), cd.MachineID, DontExecBefore, 3, streg); if (ID == null) { MessageBox.Show(this, "Cannot create SimpleTask on Server: " + Program.net.GetLastError(), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } } if (GroupID != null) { MessageBox.Show(this, "Task" + (lstPCs.GetSelectedCount == 1 ? "" : "s") + " created for " + lstPCs.GetSelectedCount + " PC" + (lstPCs.GetSelectedCount == 1 ? "" : "s"), Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Information); } this.DialogResult = DialogResult.OK; this.Close(); break; } } }
private void uninstallToolStripMenuItem_Click(object sender, EventArgs e) { if (lstApps.SelectedItems.Count == 0) { return; } List <string> MIDs = new List <string>(); if (((AddRemoveAppReport)lstApps.SelectedItems[0].Tag).IsMSI == true) { foreach (ListViewItem lst in lstApps.SelectedItems) { AddRemoveAppReport lt = (AddRemoveAppReport)lst.Tag; if (MIDs.Contains(lt.MachineID.ToLower()) == false) { MIDs.Add(lt.MachineID.ToLower()); } if (lt.IsMSI == false) { MessageBox.Show(this, "You cannot mix MSI installations with non-MSI installations to uninstall in the selection.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } if (MessageBox.Show(this, "Do you really want to uninstall the selected " + lstApps.SelectedItems.Count.ToString() + " application" + (lstApps.SelectedItems.Count == 1 ? "" : "s") + " from " + MIDs.Count.ToString() + " computer" + (MIDs.Count == 1 ? "" : "s") + "?", Program.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } foreach (ListViewItem lst in lstApps.SelectedItems) { AddRemoveAppReport lt = (AddRemoveAppReport)lst.Tag; if (string.IsNullOrWhiteSpace(lt.ProductID) == true) { continue; } SimpleTaskRunProgramm run = new SimpleTaskRunProgramm(); if (lt.IsWOWBranch == false) { run.Executable = "%SYSTEMROOT%\\System32\\MSIExec.exe"; } else { run.Executable = "%SYSTEMROOT%\\SysWOW64\\MSIExec.exe"; } run.Parameters = "/x " + lt.ProductID + " /passive /quiet /norestart"; run.User = lt.Username; Program.net.SetSimpleTask("Uninstall: " + lt.Name + (string.IsNullOrWhiteSpace(lt.Username) == true ? "" : " (as " + lt.Username + ")"), lt.MachineID, null, 1, run); } } else { if (lstApps.SelectedItems.Count > 1) { MessageBox.Show(this, "Only one non MSI can be selected to uninstall.", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } AddRemoveAppReport lt = (AddRemoveAppReport)lstApps.SelectedItems[0].Tag; SimpleTaskRunProgramm run = new SimpleTaskRunProgramm(); if (lt.UninstallString.StartsWith("\"") == true) { if (lt.UninstallString.IndexOf('"', 1) == -1) { run.Executable = lt.UninstallString.Substring(1); } else { run.Executable = lt.UninstallString.Substring(1, lt.UninstallString.IndexOf('"', 1) - 1); run.Parameters = lt.UninstallString.Substring(lt.UninstallString.IndexOf('"', 1) + 1); } run.User = lt.Username; } else { if (lt.UninstallString.IndexOf(' ', 0) == -1) { run.Executable = lt.UninstallString; } else { run.Executable = lt.UninstallString.Substring(0, lt.UninstallString.IndexOf(' ')); run.Parameters = lt.UninstallString.Substring(lt.UninstallString.IndexOf(' ') + 1); } run.User = lt.Username; } SimpleTask st = new SimpleTask(); st.Data = JsonConvert.SerializeObject(run); st.MachineID = lt.MachineID; st.Type = 1; st.ID = -1; st.Name = "Uninstall: " + lt.Name + (string.IsNullOrWhiteSpace(lt.Username) == true ? "" : " (as " + lt.Username + ")"); frmSimpleTasks frm = new frmSimpleTasks(st); frm.ShowDialog(this); } }