|
| 1 | +using Microsoft.ConfigurationManagement.AdminConsole; |
| 2 | +using Microsoft.ConfigurationManagement.ManagementProvider; |
| 3 | +using Microsoft.ConfigurationManagement.AdminConsole.Schema; |
| 4 | +using Microsoft.ConfigurationManagement.AdminConsole.DialogFramework; |
| 5 | +using System; |
| 6 | +using System.Management; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading; |
| 9 | +using System.ServiceProcess; |
| 10 | + |
| 11 | +namespace ConfigMgr.QuickTools.Device |
| 12 | +{ |
| 13 | + public static class ClientActions |
| 14 | + { |
| 15 | + public static void RunClientActionMachinePolicy(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 16 | + { |
| 17 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000021}"); |
| 18 | + } |
| 19 | + |
| 20 | + public static void RunClientActionApplicationDeployment(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 21 | + { |
| 22 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000121}"); |
| 23 | + } |
| 24 | + |
| 25 | + public static void RunClientActionDataCollection(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 26 | + { |
| 27 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000003}"); |
| 28 | + } |
| 29 | + |
| 30 | + public static void RunClientActionFileCollection(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 31 | + { |
| 32 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000010}"); |
| 33 | + } |
| 34 | + |
| 35 | + public static void RunClientActionHardwareInventory(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 36 | + { |
| 37 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000001}"); |
| 38 | + } |
| 39 | + |
| 40 | + public static void RunClientActionFullHardwareInventory(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 41 | + { |
| 42 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000001}", true); |
| 43 | + } |
| 44 | + |
| 45 | + public static void RunClientActionSoftwareInventory(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 46 | + { |
| 47 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000002}"); |
| 48 | + } |
| 49 | + |
| 50 | + public static void RunClientActionSoftwareMetering(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 51 | + { |
| 52 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000031}"); |
| 53 | + } |
| 54 | + |
| 55 | + public static void RunClientActionSoftwareUpdateDeployment(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 56 | + { |
| 57 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000108}"); |
| 58 | + } |
| 59 | + |
| 60 | + public static void RunClientActionSoftwareUpdateScan(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 61 | + { |
| 62 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000113}"); |
| 63 | + } |
| 64 | + |
| 65 | + public static void RunClientActionUserPolicy(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 66 | + { |
| 67 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000026}"); |
| 68 | + } |
| 69 | + |
| 70 | + public static void RunClientActionWindowsInstaller(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 71 | + { |
| 72 | + ProcessClientAction(scopeNode, action, selectedResultObjects, "{00000000-0000-0000-0000-000000000032}"); |
| 73 | + } |
| 74 | + |
| 75 | + public static void ClientAction(IResultObject resultObject, string scheduleId, bool fullScan) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + if (fullScan) |
| 80 | + { |
| 81 | + ManagementScope inventoryAgentScope = new ManagementScope(string.Format(@"\\{0}\root\{1}", resultObject["Name"].StringValue, "ccm\\InvAgt")); |
| 82 | + ManagementClass inventoryClass = new ManagementClass(inventoryAgentScope.Path.Path, "InventoryActionStatus", null); |
| 83 | + |
| 84 | + // Query the class for the InventoryActionID object (create query, create searcher object, execute query). |
| 85 | + ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM InventoryActionStatus WHERE InventoryActionID = '{0}'", scheduleId)); |
| 86 | + using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(inventoryAgentScope, query)) |
| 87 | + { |
| 88 | + ManagementObjectCollection queryResults = searcher.Get(); |
| 89 | + |
| 90 | + // Enumerate the collection to get to the result (there should only be one item returned from the query). |
| 91 | + foreach (ManagementObject result in queryResults) |
| 92 | + { |
| 93 | + // Display message and delete the object. |
| 94 | + result.Delete(); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ManagementClass clientaction = new ManagementClass(string.Format(@"\\{0}\root\{1}:{2}", resultObject["Name"].StringValue, "ccm", "SMS_Client")); |
| 99 | + object[] methodArgs = { scheduleId }; |
| 100 | + clientaction.InvokeMethod("TriggerSchedule", methodArgs); |
| 101 | + clientaction.Dispose(); |
| 102 | + } |
| 103 | + catch (ManagementException ex) |
| 104 | + { |
| 105 | + ExceptionUtilities.TraceException(ex); |
| 106 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex, "An error occured while invoking WMI method."); |
| 107 | + } |
| 108 | + catch (COMException ex) |
| 109 | + { |
| 110 | + ExceptionUtilities.TraceException(ex); |
| 111 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex, "An error occured while connecting to host."); |
| 112 | + } |
| 113 | + //catch (UnauthorizedAccessException ex) |
| 114 | + //{ |
| 115 | + //} |
| 116 | + catch (Exception ex) |
| 117 | + { |
| 118 | + ExceptionUtilities.TraceException(ex); |
| 119 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void ProcessClientAction(ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, string schedulerId) |
| 124 | + { |
| 125 | + ProcessClientAction(scopeNode, action, selectedResultObjects, schedulerId, false); |
| 126 | + } |
| 127 | + |
| 128 | + private static void ProcessClientAction(ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, string schedulerId, bool full) |
| 129 | + { |
| 130 | + if (selectedResultObjects.ObjectClass == "SMS_Collection") |
| 131 | + { |
| 132 | + ConnectionManagerBase connectionManagerInstance = (scopeNode as ConsoleParentNode).RootConnectionNode.GetConnectionManagerInstance("WQL"); |
| 133 | + |
| 134 | + try |
| 135 | + { |
| 136 | + string query = string.Format("SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='{0}'", selectedResultObjects["CollectionID"].StringValue); |
| 137 | + using (IResultObject resultObject = connectionManagerInstance.QueryProcessor.ExecuteQuery(query)) |
| 138 | + { |
| 139 | + using (ClientActionsDialog clientActions = new ClientActionsDialog(resultObject, schedulerId, action, full)) |
| 140 | + { |
| 141 | + clientActions.ShowDialog(SnapIn.Console); |
| 142 | + return; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + catch (SmsQueryException ex) |
| 147 | + { |
| 148 | + ExceptionUtilities.TraceException(ex); |
| 149 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex); |
| 150 | + return; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if (selectedResultObjects.Count == 1) |
| 155 | + { |
| 156 | + foreach (IResultObject resultObject in selectedResultObjects) |
| 157 | + { |
| 158 | + ThreadPool.QueueUserWorkItem(arg => { ClientAction(resultObject, schedulerId, full); }); |
| 159 | + } |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + using (ClientActionsDialog clientActions = new ClientActionsDialog(selectedResultObjects, schedulerId, action, full)) |
| 164 | + { |
| 165 | + clientActions.ShowDialog(SnapIn.Console); |
| 166 | + return; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public static void RunClientActionRestartService(object sender, ScopeNode scopeNode, ActionDescription action, IResultObject selectedResultObjects, PropertyDataUpdated dataUpdatedDelegate, Status status) |
| 172 | + { |
| 173 | + ThreadPool.QueueUserWorkItem(arg => { RestartService(selectedResultObjects); }); |
| 174 | + } |
| 175 | + |
| 176 | + private static void RestartService(IResultObject resultObject) |
| 177 | + { |
| 178 | + try |
| 179 | + { |
| 180 | + using (ServiceController service = new ServiceController("SMS Agent Host", resultObject["Name"].StringValue)) |
| 181 | + { |
| 182 | + if (service.Status == ServiceControllerStatus.Running) |
| 183 | + { |
| 184 | + service.Stop(); |
| 185 | + service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(20)); |
| 186 | + |
| 187 | + service.Start(); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + catch (System.ServiceProcess.TimeoutException ex) |
| 192 | + { |
| 193 | + ExceptionUtilities.TraceException(ex); |
| 194 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex); |
| 195 | + } |
| 196 | + catch (Exception ex) |
| 197 | + { |
| 198 | + ExceptionUtilities.TraceException(ex); |
| 199 | + SccmExceptionDialog.ShowDialog(SnapIn.Console, ex); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments