Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum Policy
- {
- [Description("{00000000-0000-0000-0000-000000000001}")]
- HardwareInventory,
- [Description("{00000000-0000-0000-0000-000000000002}")]
- SoftwareInventory,
- [Description("{00000000-0000-0000-0000-000000000003}")]
- DiscoveryInventory,
- [Description("{00000000-0000-0000-0000-000000000022}")]
- EvaluateMachinePolicies
- }
- public static bool RunPolicy(string computer, Policy p)
- {
- try
- {
- using (var im = new Impersonator.Impersonator())
- {
- ManagementScope sccm;
- sccm =
- String.Compare(computer, Environment.MachineName, StringComparison.InvariantCultureIgnoreCase) >
- 0
- ? new ManagementScope("\\\\" + computer + "\\root\\ccm")
- : new ManagementScope("\\\\localhost\\root\\ccm");
- sccm.Connect();
- var client = new ManagementClass(sccm, new ManagementPath("SMS_Client"), null);
- ManagementBaseObject inParams = client.GetMethodParameters("TriggerSchedule");
- inParams["sScheduleID"] = GetEnumDescription(p);
- client.InvokeMethod("TriggerSchedule", inParams, null);
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- public static string GetEnumDescription(Enum value)
- {
- var fi = value.GetType().GetField(value.ToString());
- var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (attributes != null && attributes.Length > 0)
- return attributes[0].Description;
- return value.ToString();
- }
- //XAML Binding With Caliburn.Micro:
- xmlns:chLib="clr-namespace:CHLib;assembly=CHLib"
- //Window / Page / UserControl Resources:
- <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type chLib:Policy}" x:Key="PolicyOptions">
- <ObjectDataProvider.MethodParameters>
- <x:Type TypeName="chLib:Policy" />
- </ObjectDataProvider.MethodParameters>
- </ObjectDataProvider>
- //In Grid (or whatever you are using):
- <ComboBox Name="SelectPolicy" ItemsSource="{Binding Source={StaticResource PolicyOptions}}" SelectedItem ="{Binding SelectPolicy}" HorizontalAlignment="Center" VerticalAlignment="Center" />
- //In ViewModel:
- private Policy _selectPolicy = Policy.EvaluateMachinePolicies;
- public Policy SelectPolicy
- {
- get { return _selectPolicy; }
- set { _selectPolicy = value; NotifyOfPropertyChange(() => SelectPolicy); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement