C# Install SCCM Package on Client
I needed to be able to kick of an installation of a SCCM Package on a client from a C# windows form. I discovered the SDKClient WMI Namespace and am using the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//Populate Listview with Packages try { ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath, select); ManagementObjectCollection instances = searcher.Get(); foreach (ManagementObject queryObj in instances) { if (queryObj["FullName"].ToString().StartsWith("Package")) { ListViewItem lvi = new ListViewItem(); lvi.Name = queryObj["FullName"].ToString(); lvi.Text = queryObj["Description"].ToString(); lvi.Tag = queryObj["ProgramID"].ToString() + "^" + queryObj["PackageID"].ToString(); lvi.ToolTipText = queryObj["Description"].ToString(); this.ListView1.Items.Add(lvi); } } } catch { } private void btnRun_Click(object sender, EventArgs e) { try { // Split ProgramID and PackageID out form string string[] progInfo = listTopSolutions.SelectedItems[0].Tag.ToString().Split('^'); programID = progInfo[0]; packageID = progInfo[1]; // Get the object on which the method will be invoked ManagementClass processClass = new ManagementClass(@"root\ccm\clientsdk", "CCM_ProgramsManager", null); // Get an input parameters object for this method ManagementBaseObject inParams = processClass.GetMethodParameters("ExecuteProgram"); // Fill in input parameter values inParams["ProgramID"] = programID; inParams["PackageID"] = packageID; // Execute the method ManagementBaseObject outParams = processClass.InvokeMethod("ExecuteProgram", inParams, null); } catch { } |
C# convert WMI DateTime to standard DateTime
WMI Dates and times are in DMTF datetime format. The following is the command to convert a given DMTF datetime to DateTime.
1 |
DateTime Date = ManagementDateTimeConverter.ToDateTime(WMIDate); |
Microsoft Documentation
C# Change service start mode
Here is some code to Change the StartMode on one or more services using C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
string ns = @"root\cimv2"; string query = "select * from Win32_Service"; string compname = "ComputerName"; ConnectionOptions options = new ConnectionOptions(); ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\{1}", compname, ns), options); scope.Connect(); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query)); ManagementObjectCollection retObjectCollection = searcher.Get(); foreach (ManagementObject mo in searcher.Get()) { name = mo.Properties["Name"].Value.ToString().Trim().ToLower(); state = mo.Properties["State"].Value.ToString().Trim(); startmode = mo.Properties["StartMode"].Value.ToString().Trim(); switch (name) { case "service1": changemode(mo, "Manual"); break; case "winmgmt": changemode(mo, "Auto"); break; } } private void changemode(ManagementObject mo, string startmode) { ManagementBaseObject inParams = mo.GetMethodParameters("ChangeStartMode"); inParams["startmode"] = startmode; ManagementBaseObject outParams = mo.InvokeMethod("ChangeStartMode", inParams, null); startmode = mo.Properties["StartMode"].Value.ToString().Trim(); } |
C# Start service on remote machine
Here is a simple method to start a service on a remote system using C#. Call the method using
1 |
string status = servicestart("computerName", "Service"); |
Method will attempt to change the status and return the current Status
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private string servicestart(string compname, string service) { ServiceController sc = new ServiceController(); sc.ServiceName = service; sc.MachineName = compname; string result = sc.Status.ToString(); if (sc.Status == ServiceControllerStatus.Stopped) { try { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); result = sc.Status.ToString(); } catch (InvalidOperationException) { // do something } } return result; } |