Commits

Anonymous committed 5bb9bda

Extend AD support.

  • Participants
  • Parent commits 89aacbe

Comments (0)

Files changed (3)

LocalPolicy/ActiveDirectoryGroupPolicyObject.cs

         /// If the path specifies a domain controller, the GPO is created on that DC. 
         /// Otherwise, the system will select a DC on the caller's behalf.
         /// </param>
-        public ActiveDirectoryGroupPolicyObject(string activeDirectoryPath, GroupPolicyObjectSettings settings)
+        public ActiveDirectoryGroupPolicyObject(string activeDirectoryPath, GroupPolicyObjectSettings settings = null)
         {
+            settings = new GroupPolicyObjectSettings();
             trycatch(() => instance.OpenDSGPO(activeDirectoryPath, settings.Flag),
                 "Unable to open GPO at ActiveDirectory path '{0}'", activeDirectoryPath);
         }
+        private ActiveDirectoryGroupPolicyObject(COM.IGroupPolicyObject instance)
+        {
+            this.instance = instance;
+        }
+
+        /// <summary>
+        /// Creates a new GPO in the Active Directory with the specified display name and opens
+        /// it with the given settings.
+        /// </summary>
+        /// <param name="activeDirectoryPath">
+        /// Specifies the Active Directory path of the object to create. 
+        /// If the path specifies a domain controller, the GPO is created on that DC. 
+        /// Otherwise, the system will select a DC on the caller's behalf.
+        /// </param>
+        /// <param name="displayName">Specifies the display name of the object to create.</param>
+        public static ActiveDirectoryGroupPolicyObject Create(string activeDirectoryPath, string displayName,
+            GroupPolicyObjectSettings settings = null)
+        {
+            settings = new GroupPolicyObjectSettings();
+            var instance = getInstance();
+            trycatch(() => instance.New(activeDirectoryPath, displayName, settings.Flag),
+                "Unable to create new GPO instance with path '{0}' and display name '{1}'", 
+                activeDirectoryPath, displayName);
+            return new ActiveDirectoryGroupPolicyObject(instance);
+        }
 
         public Guid GuidName
         {

LocalPolicy/GroupPolicyObject.cs

 
         public abstract string GetPathTo(GroupPolicySection section);
 
-        private LocalPolicy.COM.IGroupPolicyObject getInstance()
+        protected static LocalPolicy.COM.IGroupPolicyObject getInstance()
         {
             var concrete = new LocalPolicy.COM.GPClass();
             return (LocalPolicy.COM.IGroupPolicyObject)concrete;
         }
-        protected void trycatch(Func<uint> operation, string messageTemplate, params object[] messageArgs)
+        protected static void trycatch(Func<uint> operation, string messageTemplate, params object[] messageArgs)
         {
             uint result = operation();
             if (result != S_OK)

LocalPolicy/LocalPolicy.csproj

     <FileAlignment>512</FileAlignment>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <PlatformTarget>x86</PlatformTarget>
+    <PlatformTarget>AnyCPU</PlatformTarget>
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>