/// <summary> /// Import a local Software Distribution Policy /// </summary> /// <param name="PolicyPath">Path to the XML File or URL on the SMS ManagementPoint</param> /// <param name="bPersistent">Create a persistant policy</param> /// <remarks>Persistant policies will not be removed automatically. Only a full/hard policy reset will remove these policies</remarks> /// <returns>CCM_SoftwareDistribution ManagementObject</returns> public ManagementObject ImportLocalPolicy(string PolicyPath, bool bPersistent) { string PolicyID = ""; string PolicyVersion = ""; ManagementObject SWDist = new ManagementObject(); try { XmlTextReader reader = new XmlTextReader(PolicyPath); reader.WhitespaceHandling = WhitespaceHandling.None; #region Extract MOF Data from XML while (!reader.EOF) { reader.Read(); if ((reader.Name == "Policy") & (reader.NodeType == XmlNodeType.Element)) { PolicyID = reader.GetAttribute("PolicyID"); PolicyVersion = reader.GetAttribute("PolicyVersion"); } if (reader.Name == "PolicyAction" & (reader.GetAttribute("PolicyActionType") == "WMI-MOF")) { reader.Read(); if ((reader.NodeType == XmlNodeType.CDATA) & (reader.HasValue)) { using (StreamWriter sw = new StreamWriter(Environment.ExpandEnvironmentVariables(@"%TEMP%\") + PolicyID + ".mof")) { sw.WriteLine(reader.Value); } } } } reader.Close(); #endregion #region Compile MOF to WMI //Compile the extracted MOF File to the local WMI Namespace root\ccm\policy ... Process mofcomp = new Process(); mofcomp.StartInfo.FileName = "mofcomp"; mofcomp.StartInfo.Arguments = @"-N:root\ccm\policy " + Environment.ExpandEnvironmentVariables(@"%TEMP%\") + PolicyID + ".mof"; mofcomp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; mofcomp.Start(); mofcomp.WaitForExit(); if (!mofcomp.HasExited) { mofcomp.Kill(); } mofcomp.Dispose(); #endregion #region Create remote policy //Copy the compiled local Policies... ManagementScope LocalScope = new ManagementScope(@"root\ccm\policy"); LocalScope.Connect(); WMI.Provider oProv = new WMI.Provider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\ccm\Policy"; SelectQuery Query = new SelectQuery("SELECT * FROM CCM_Scheduler_ScheduledMessage WHERE PolicyID='" + PolicyID + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(LocalScope, Query); ManagementObjectCollection SchedColl = searcher.Get(); foreach (ManagementObject MO in SchedColl) { WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy:CCM_Scheduler_ScheduledMessage")); } Query = new SelectQuery("SELECT * FROM CCM_SoftwareDistribution WHERE PolicyID='" + PolicyID + "'"); searcher = new ManagementObjectSearcher(LocalScope, Query); ManagementObjectCollection SWDistColl = searcher.Get(); foreach (ManagementObject MO in SWDistColl) { SWDist = MO; if (bPersistent) MO.SetPropertyValue("PolicySource", "LOCAL"); MO.SetPropertyValue("ADV_MandatoryAssignments", "True"); WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy:CCM_SoftwareDistribution")); } oProv.mScope.Path.NamespacePath = @"root\ccm\policy\machine\actualconfig"; foreach (ManagementObject MO in SchedColl) { //MO.SetPropertyValue("PolicySource", "LOCAL"); WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy\machine\actualconfig:CCM_Scheduler_ScheduledMessage")); } foreach (ManagementObject MO in SWDistColl) { //MO.SetPropertyValue("PolicySource", "LOCAL"); WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy\machine\actualconfig:CCM_SoftwareDistribution")); } if (bPersistent) { oProv.mScope.Path.NamespacePath = @"root\ccm\policy\machine\RequestedConfig"; foreach (ManagementObject MO in SchedColl) { MO.SetPropertyValue("PolicySource", "LOCAL"); WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy\machine\RequestedConfig:CCM_Scheduler_ScheduledMessage")); } foreach (ManagementObject MO in SWDistColl) { MO.SetPropertyValue("PolicySource", "LOCAL"); MO.SetPropertyValue("ADV_MandatoryAssignments", "True"); WMI.Provider.ManagementObjectCopy(MO, oProv.mScope, new ManagementPath(@"\\" + oProv.mScope.Path.Server + @"\root\ccm\policy\machine\RequestedConfig:CCM_SoftwareDistribution")); } } #endregion #region Delete local Policy foreach (ManagementObject MO in SWDistColl) { MO.Delete(); } foreach (ManagementObject MO in SchedColl) { MO.Delete(); } #endregion #region Delete remote Policy oProv.mScope.Path.NamespacePath = @"root\ccm\Policy"; oProv.DeleteQueryResults("SELECT * FROM CCM_Scheduler_ScheduledMessage WHERE PolicyID='" + PolicyID + "'"); oProv.DeleteQueryResults("SELECT * FROM CCM_SoftwareDistribution WHERE PolicyID='" + PolicyID + "'"); #endregion #region delete local .MOF File System.IO.File.Delete(Environment.ExpandEnvironmentVariables(@"%TEMP%\") + PolicyID + ".mof"); #endregion } catch { throw; } return SWDist; }
/// <summary> /// Remove local persistent SoftwareDistribution policies /// </summary> /// <remarks>Wait or enforce a PolicyEvaluation cycle to enforce the process</remarks> public void CleanupPersistentPolicies() { WMI.Provider oProv = new WMI.Provider(oWMIProvider.mScope.Clone()); oProv.mScope.Path.NamespacePath = @"root\CCM\Policy\Machine\RequestedConfig"; oProv.DeleteQueryResults("SELECT * FROM CCM_SoftwareDistribution WHERE PolicySource ='LOCAL'"); oProv.DeleteQueryResults("SELECT * FROM CCM_Scheduler_ScheduledMessage WHERE PolicySource ='LOCAL'"); }