You can update or refresh the GPO easily by the command line utility tool gpupdate. By using following commands we can update GPO on local machine.
gpupdate /force -update user and computer configuration
gpupdate /target:computer /force -update only computer configuration
gpupdate /target:user /force -update only user configuration
Remote Group Policy update in C#
You can use the below C# function to update GPO on remote machine.
Note: For local machine don’t use username,password and Impersonation
private static void UpdateGPO(string machinename) { try { ConnectionOptions connectionOptions = new ConnectionOptions(); connectionOptions.Username = @"DomainAdministrator"; connectionOptions.Password = "password"; connectionOptions.Impersonation = ImpersonationLevel.Impersonate; ManagementScope scope = new ManagementScope("\" + machinename + "rootCIMV2", connectionOptions); scope.Connect(); ManagementClass clas = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions()); ManagementBaseObject inparams = clas.GetMethodParameters("Create"); inparams["CommandLine"] = "GPUpdate /force"; ManagementBaseObject outparam = clas.InvokeMethod("Create", inparams, null); } catch (Exception ex) { } }
Local Group Policy update in C#
You can use the below C# function to update GPO on local machine.
private static void UpdateGPO() { try { Process proc = new Process(); ProcessStartInfo procStartInfo = new ProcessStartInfo(@"cmd.exe","/c"+"gpupdate/force"); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; procStartInfo.LoadUserProfile = true; proc.StartInfo = procStartInfo; proc.Start(); proc.WaitForExit(); } catch (Exception ex) { } }
Related Articles:
– Active Directory Attribute mapping with Friendly name – user
– Active Directory Search Filter Examples
– Create new Active Directory User in C#
– How to get list of all domain controllers in C#
– Restore a deleted Active Directory object using C#
– Active Directory search filter by ObjectGuid
Thanks,
Morgan
Software Developer
Advertisement