Advertisement
parabola949

Run SCCM 2012 Policy via WMI

Mar 20th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. public enum Policy
  2.     {
  3.         [Description("{00000000-0000-0000-0000-000000000001}")]
  4.         HardwareInventory,
  5.         [Description("{00000000-0000-0000-0000-000000000002}")]
  6.         SoftwareInventory,
  7.         [Description("{00000000-0000-0000-0000-000000000003}")]
  8.         DiscoveryInventory,
  9.         [Description("{00000000-0000-0000-0000-000000000022}")]
  10.         EvaluateMachinePolicies
  11.     }
  12.  
  13.  
  14. public static bool RunPolicy(string computer, Policy p)
  15.         {
  16.             try
  17.             {
  18.                 using (var im = new Impersonator.Impersonator())
  19.                 {
  20.                     ManagementScope sccm;
  21.                     sccm =
  22.                         String.Compare(computer, Environment.MachineName, StringComparison.InvariantCultureIgnoreCase) >
  23.                         0
  24.                             ? new ManagementScope("\\\\" + computer + "\\root\\ccm")
  25.                             : new ManagementScope("\\\\localhost\\root\\ccm");
  26.                     sccm.Connect();
  27.                     var client = new ManagementClass(sccm, new ManagementPath("SMS_Client"), null);
  28.                     ManagementBaseObject inParams = client.GetMethodParameters("TriggerSchedule");
  29.                     inParams["sScheduleID"] = GetEnumDescription(p);
  30.                     client.InvokeMethod("TriggerSchedule", inParams, null);
  31.                     return true;
  32.                 }
  33.             }
  34.             catch
  35.             {
  36.                 return false;
  37.             }
  38.  
  39.         }
  40.  
  41.         public static string GetEnumDescription(Enum value)
  42.         {
  43.             var fi = value.GetType().GetField(value.ToString());
  44.             var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  45.  
  46.             if (attributes != null && attributes.Length > 0)
  47.                 return attributes[0].Description;
  48.             return value.ToString();
  49.         }
  50.  
  51.  
  52.  
  53. //XAML Binding With Caliburn.Micro:
  54. xmlns:chLib="clr-namespace:CHLib;assembly=CHLib"
  55. //Window / Page / UserControl Resources:
  56. <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type chLib:Policy}" x:Key="PolicyOptions">
  57.             <ObjectDataProvider.MethodParameters>
  58.                 <x:Type TypeName="chLib:Policy" />
  59.             </ObjectDataProvider.MethodParameters>
  60.         </ObjectDataProvider>
  61.  
  62. //In Grid (or whatever you are using):
  63. <ComboBox Name="SelectPolicy" ItemsSource="{Binding Source={StaticResource PolicyOptions}}" SelectedItem ="{Binding SelectPolicy}" HorizontalAlignment="Center" VerticalAlignment="Center" />
  64.  
  65. //In ViewModel:
  66. private Policy _selectPolicy = Policy.EvaluateMachinePolicies;
  67.  
  68.         public Policy SelectPolicy
  69.         {
  70.             get { return _selectPolicy; }
  71.             set { _selectPolicy = value; NotifyOfPropertyChange(() => SelectPolicy); }
  72.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement