{"id":17761,"date":"2010-07-14T00:01:00","date_gmt":"2010-07-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/14\/hey-scripting-guy-can-i-use-group-policy-cmdlets-to-test-active-directory-replication\/"},"modified":"2010-07-14T00:01:00","modified_gmt":"2010-07-14T00:01:00","slug":"hey-scripting-guy-can-i-use-group-policy-cmdlets-to-test-active-directory-replication","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-group-policy-cmdlets-to-test-active-directory-replication\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Group Policy Cmdlets to Test Active Directory Replication?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p>Hey, Scripting Guy! I need to use the Microsoft <a href=\"http:\/\/technet.microsoft.com\/en-us\/windowsserver\/bb310732.aspx\">Group Policy<\/a> cmdlets and <a href=\"http:\/\/support.microsoft.com\/kb\/968929\">Windows PowerShell 2.0<\/a> to test <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/site\/search?f%5B0%5D.Type=RootCategory&amp;f%5B0%5D.Value=activedirectory&amp;f%5B0%5D.Text=Active%20Directory\">Active Directory<\/a> replication. Know how?<\/p>\n<p>&#8212; RG<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p>Hello RG, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I had an Office Communicator conversation last night with my friend Brett in Sydney, Australia. He is my <a href=\"http:\/\/bit.ly\/39Dieh\">Tim Tam<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Anzac_biscuit\">ANZAC biscuit<\/a> connection, and he has a great personality. He was working with a customer that had a problem with a VBScript script, and I was providing assistance. He happened to mention that it was cold; it turned out it was 57 degrees Fahrenheit outside. It was 107 in Charlotte, <a href=\"http:\/\/en.wikipedia.org\/wiki\/North_Carolina\">North Carolina<\/a> (I use my <a href=\"http:\/\/blogs.technet.com\/search\/SearchResults.aspx?q=conversion%20module&amp;sections=7618\">Windows PowerShell conversion module<\/a> to perform the calculations). That is one thing I enjoy about the Australian winters: They happen to be at the same time as the height of summer in Charlotte. Oh well. <\/p>\n<p>RG, the Test-GPOReplication.ps1 script creates a Group Policy object (GPO) on a specific server, and then connects to another server to watch for replication to occur. When replication of the GPO completes, a message to that effect is displayed. The complete Test-GPOReplication.ps1 script is shown here. <\/p>\n<p><strong>Test-GPOReplication.ps1<\/strong><\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;hyperv.nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;dc1.nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;testGPO&#8221;&nbsp; <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-MyModule<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Param([string]<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ListAvailable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">})<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">then<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">already<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">loaded<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">get-MyModule<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$gpoName<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">new-gpo<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">while(!<\/span><span style=\"color: #2b91af\">$replGPO<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$replGPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-EA<\/span><span style=\"color: #808080\"> &#8220;SilentlyContinue&#8221;&nbsp; <br \/><\/span><span style=\"color: #0000ff\">start-sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Seconds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">while<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Get-Date<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Entry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Point<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Script<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Get-MyModule<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\"> &#8220;GroupPolicy&#8221;<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$dte<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-gpoName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gpoName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">-replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Replication on $replPartner completed at $($dte)&#8221;&nbsp; <br \/><\/span><span style=\"color: #000000\">Remove-GPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span> <\/div>\n<p>The Test-GPOReplication.ps1 script begins by creating several command-line parameters and assigning default values for those parameters. You will want to change my defaults to something that makes sense on your network. The command-line parameters are shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;hyperv.nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;dc1.nwtraders.com&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;testGPO&#8221; <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<p><span style=\"background-color: #f0f0f0\"><\/span><\/p>\n<p>The Test-GPOReplication.ps1 script incorporates the <strong>Get-MyModule<\/strong> function from <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/07\/11\/hey-scripting-guy-weekend-scripter-checking-for-module-dependencies-in-windows-powershell.aspx\">Sunday&rsquo;s Weekend Scripter post<\/a>. The <strong>Get-MyModule<\/strong> function looks in the current Windows PowerShell environment to determine if the <strong>GroupPolicy<\/strong> module is loaded. If the <strong>GroupPolicy<\/strong> module is not loaded, it will load the module. If the module does not exist, an error to that effect is displayed. <\/p>\n<p>Keep in mind that because the <strong>Get-MyModule<\/strong> function accepts an input parameter for the name of the module, the function can check for required modules in other scripts. The complete <strong>Get-MyModule<\/strong> function is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-MyModule<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Param([string]<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(Get-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ListAvailable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">})<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Import-Module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$name<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">then<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">import<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">available<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">module<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">already<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">loaded<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">get-MyModule<\/span> <\/div>\n<p>The <strong>Test-Replication<\/strong> function begins by creating a number of parameters: <\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$gpoName<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><\/div>\n<p>Next, a new GPO is created on a specific server in a specific domain. The values for the name, domain, and server come from the command line when the script is called. The parameters for the function have the same signature as the parameters for the script. The New-GPO cmdlet returns an instance of a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd537085(v=VS.85).aspx\">Microsoft.Grouppolicy.Gpo .NET Framework class<\/a>. This clutters the Windows PowerShell console as the script runs, and the instance is stored in the <strong>$null<\/strong> variable (which effectively discards it). The code shown here creates the new GPO:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">new-gpo<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span> <\/div>\n<p>A <strong>While<\/strong> statement checks the target server for the newly created GPO to appear. The <strong>While<\/strong> statement loops through the script block every second due to the <strong>Start-Sleep<\/strong> cmdlet. This looping continues as long as no GPO object is stored in the <strong>$replGPO<\/strong> variable. The <strong>Get-GPO<\/strong> cmdlet retrieves the newly created GPO from the targeted server. If the GPO does not exist, an error is generated. The <strong>silentlycontinue<\/strong> error action prevents the error from displaying. The complete <strong>while<\/strong> statement is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">while(!<\/span><span style=\"color: #2b91af\">$replGPO<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #2b91af\">$replGPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-GPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-EA<\/span><span style=\"color: #808080\"> &#8220;SilentlyContinue&#8221; <\/p>\n<p><\/span><span style=\"color: #0000ff\">start-sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Seconds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">while<\/span> <\/div>\n<p><span style=\"background-color: #f0f0f0\"><\/span><\/p>\n<p>After the GPO is retrieved from the target server, the current date and time are sent to the calling code, and the function closes. The <strong>Get-Date<\/strong> cmdlet retrieves the current timestamp. This section of the script is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #0000ff\">Get-Date<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span> <\/div>\n<p><span style=\"background-color: #f0f0f0\"><\/span><\/p>\n<p>The entry point to the script calls the <strong>Get-MyModule<\/strong> function to determine if the <strong>GroupPolicy<\/strong> module is present on the system and can be loaded. If the module is not loaded in the current Windows PowerShell session, it will be loaded. If the <strong>GroupPolicy<\/strong> module cannot be loaded, the script exits. This section of the script is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Get-MyModule<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-name<\/span><span style=\"color: #808080\"> &#8220;GroupPolicy&#8221;<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<p>Next, the <strong>Test-Replication<\/strong> function is called to create the new GPO and to wait for replication to occur. The values <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee198910.aspx\">for each<\/a> of the four parameters come from the command-line parameters defined at the beginning of the script. The <strong>Test-Replication<\/strong> function returns a <strong>datetime<\/strong> object that is stored in the <strong>$dte<\/strong> variable. This portion of the script is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #2b91af\">$dte<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Replication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-gpoName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gpoName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">-replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span> <\/div>\n<p><span style=\"background-color: #f0f0f0\"><\/span><\/p>\n<p>After the <strong>Test-Replication<\/strong> function has completed and has returned the <strong>datetime<\/strong> object, a message is displayed on the Windows PowerShell console that indicates replication has occurred. This is shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Replication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">on<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$replPartner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">completed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">at<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$dte<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&#8220;<\/span> <\/div>\n<p>The last thing to do is to remove the <strong>testGPO<\/strong> that was created in the <strong>Test-Replication<\/strong> function. To remove a GPO, use the <strong>Remove-GPO<\/strong> cmdlet as shown here:<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code\"><span style=\"color: #000000\">Remove-GPO<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$gponame<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$domain<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Server<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$server<\/span> <\/div>\n<p>When the Test-GPOReplication.ps1 script runs, the output shown in the following image appears in the Windows PowerShell ISE.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3034.clip_image002_119A4970.jpg\"><\/a><\/p>\n<p><img decoding=\"async\" style=\"max-width: 600px;border: 0px\" alt=\"Image of script output in Windows PowerShell ISE\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/july\/hey0714\/hsg-07-14-10-01.jpg\" \/>&nbsp;<\/p>\n<p>RG, that is all there is to using Group Policy cmdlets and Windows PowerShell 2.0 to test Active Directory replication. Group Policy Week will continue tomorrow when we will talk about reporting Group Policy configuration settings. <\/p>\n<p>We would love for you follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I need to use the Microsoft Group Policy cmdlets and Windows PowerShell 2.0 to test Active Directory replication. Know how? &#8212; RG &nbsp; Hello RG, Microsoft Scripting Guy Ed Wilson here. I had an Office Communicator conversation last night with my friend Brett in Sydney, Australia. He is my Tim Tam [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[152,3,45],"class_list":["post-17761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-group-policy","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I need to use the Microsoft Group Policy cmdlets and Windows PowerShell 2.0 to test Active Directory replication. Know how? &#8212; RG &nbsp; Hello RG, Microsoft Scripting Guy Ed Wilson here. I had an Office Communicator conversation last night with my friend Brett in Sydney, Australia. He is my Tim Tam [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17761"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17761\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}