Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 2007

PowerShell Scripting

Group Policy change management made easy!
RSS
Subscribe to Windows IT Pro | See More Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

We also need to grab the constants from the GPMC object and create a variable that can be passed into our COM functions; the code at callout B accomplishes this task. In addition, we need to use the DNS names from the sample .csv file to obtain the domain object GPMDomain via a call to the GetDomain GPMC API; the code at callout C accomplishes this task.

Next, we need to call the GPMC API to select the appropriate GPO, as the code at callout D does. Notice that this line of code uses a new operand (i.e., $_.) and specifies the column name of the .csv file we created earlier. The $_ operand lets us access sets of data within each column, stored by PowerShell as .NET objects.

The last line in our GPMC API calls, which callout E shows, is the final call to actually do the backup. In this example, I hard-coded the backup location to C:\backup. This location can be passed in or be part of the columns in the passed-in .csv file. The PowerShell command is

$Result = $GPO.Backup ("C:\\backup", $_ 
  .Description) 

Note that when you pass in a directory name to a COM-based function call, you need to use two backslashes because single backslashes are interpreted as escape characters. Make sure the directory C:\backup exists before you call the PowerShell script.

Finally, we add a single line of code at the beginning to name our filter and put the code we've written so far in a block to allow objects to be passed in as a filter. Functions are typically created with parameters that are passed in. In our case, the whole .csv file is passed in and we're dynamically accessing all the objects within the file. Using functions doesn't make sense because we aren't specifying static parameters. Filters just take whatever is passed in—the code inside the filter handles and makes assumptions about the data to use.

If we wanted to get more advanced, we could add error checks to make sure the $GPMResult variable is valid and that no exceptions have been thrown, to determine the script's success or failure. However, I wanted to keep the example simple.

Step 4: execute the Backup Filter
Now we get to see PowerShell's magic and flexibility. First, start PowerShell and change the PowerShell policy to allow execution of scripts. To do so, enter

PS C:\> Set-ExecutionPolicy
  -executionPolicy Unrestricted 

Next, load the PowerShell code from Listing 1 by "dot-sourcing" the PowerShell file. This action essentially loads the filter we've created into the current PowerShell runspace.

As I already explained, passing a .csv file as a parameter only passes the reference to the .csv file in a function. You then have to write code in the function to manually parse the contents of the .csv file. Passing the .csv file in as a filter lets us access all of the file's data elements as .NET objects inside the filter.

Enter

PS C:\> . C:\PSDemo\BackupGPOs.ps1 

Then, import the .csv file and pass it into the Do-GPOBackup filter we wrote. Enter

PS C:\> import-csv C:\PSDemo\GPOList.csv   
  | Do-GPOBackup 

Figure 2 shows the output.

The PowerShell script that we created can take as input any .csv file for performing operations on GPOs. We can also add a parameter check to operate only on GPOs that match a certain domain name. For example, for the domain Americas, enter

PS C:\> import-cvs C:\PSDemo\GPOList.csv 
| {where $_.Domain = "Americas"} | DoGPOBackup

Another option is to take the result and pipe it to a graph to identify the GPOs that were backed up, time them to see which ones are taking the longest to back up, and pinpoint which ones are creating the greatest performance problems. The possibilities are now endless. We could modify the PowerShell script so that instead of performing backups, we could perform periodic restores of critical GPOs by scheduling them via Task Scheduler. In addition, simply changing the spreadsheet contents would dynamically change the list of GPOs being operated on, backed up, or restored.

   Previous  1  [2]  3  Next 


Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

How can I stop and start services from the command line?

...

Where is Microsoft NetMeeting in Windows XP?

...


Related Articles PowerShell 101, Lesson 2

Working with Shortcuts in Windows PowerShell

PowerShell One-Liners for Managing the File System

Dig Out by Digging Into PowerShell

Related Events PowerShell 101 - eLearning Series

Introduction to Identity Lifecycle Manager "2"

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing