Escolar Documentos
Profissional Documentos
Cultura Documentos
At a glance:
What is Windows
PowerShell?
How GPOs were managed
previously
Migrating scripts to
Windows PowerShell
and Computers. Moreover, there was a brand commands. In addition, Windows PowerShell
new API that let administrators use COM- has functions that work much like subrou
based languages such as VBScript to perform tines in VBScript, and that can be created in
Group Policy administration of tasks, such real time at the Windows PowerShell com
as backing up and restoring Group Policy mand prompt.
Objects (GPOs), migrating GPOs between Even better, Windows PowerShell is built
on the Microsoft .NET Framework, while
VBScript relies on older COM technology.
The vast amount of .NET This means that the vast amount of .NET
code being produced today can be used di
code being produced today rectly from within Windows PowerShell.
What it comes down to is that with Win
can be used directly from dows PowerShell, you get full scripting sup
port and interactive mode, all in one package.
within Windows PowerShell The examples I provide here will all be com
mand-line input so you can type as you read;
however, theyll work equally well if you put
domains, configuring security settings on them in a Windows PowerShell script file
GPOs and links, and reporting. and run it.
Unfortunately, GPMC did not provide the
ability to edit the actual configured settings Recreate old scripts using Windows
inside the GPOs. In other words, you could PowerShell
perform operations on the GPO container, The last thing you want to do when you start
such as reading GPO versions, reading mod with a new technology is toss out all your
ify dates, creating brand new GPOs, backing previous work. There are three approach
up and restoring/importing GPOs from dif es you can use to access COM objects from
ferent domains, and so forth, but you couldnt the GPMC API, or basically to reuse any old
programmatically add or change the content VBScript out there. You can select one of
of the GPO, for example adding a new redi these three options:
rected folder or a new software installation. Create a Windows PowerShell cmdlet us
What you generally did instead was create ing a programming language like C# or
the GPO and configure all settings manu managed C++.
ally using the Group Policy Object Editor, Use Windows PowerShell to access the
then back it up and import it into a test ScriptControl in MSScript.ocx to wrap
environment. When everything was verified old scripts.
and working properly, you would import Wrap the COM calls in reusable Windows
it into the live environment. Despite the PowerShell functions or else call the COM
missing feature, the use of scripts instead objects directly.
of manual interaction with the GPMC API
resulted in a tremendous amount of time, Im going to focus mainly on the third op
effort and error saved in day-to-day Group tion, but lets have a quick look at all the op
Policy administration. tions first.
the VBScript function ShowMessage is RestoreAllGpos Restores all GPOs from a backup to a domain
called and executed with a parameter, result
RestoreGpo Restores a single GPO from a backup
ing in a Message Box displayed with the text
Hello World. GetAllBackedUpGpos Retrieves the latest version of GPO backups from
a given path
Now some of you might think, Great!
Ive mastered the art of using COM from CopyGpo Copies the settings in a GPO to another GPO
Windows PowerShell and I can stop reading CreateGpo Creates a new empty GPO
this article and start filling the ScriptControl
DeleteGpo Deletes a GPO
object with my collection of old GPMC
scripts. Unfortunately, thats not the case. FindDisabledGpos Returns all GPOs where both the user and com-
puter part are disabled
This technique quickly becomes very com
plex and tough to debug as soon as the scripts FindUnlinkedGpos Returns all GPOs that have no links
Wrapping COM objects CreateReportForAllGpos Creates a separate XML report for each GPO in a
So the best option is the third: wrapping domain
the COM calls in reusable Windows Power GetGpoByNameOrID Finds a GPO by its display name or GPO ID
Shell functions, which lets you consume the GetBackupByNameOrId Finds a GPO backup by its display name or GPO
COM objects in the GPMC API. The line be ID
low shows how to create a .NET object di GetAllGposInDomain Returns all GPOs in a domain
rectly in Windows PowerShell. In this case,
functions
C:\Boot.ini)
WScript.Echo comFileObject.Size
ous commands, however. In other words,
some of the objects created initially will be
Then run it using Cscript.exe, for example.
used later on, so stay in the same Windows
In Windows PowerShell, you would do it like
PowerShell session. If you close the session,
this (directly from the Windows PowerShell
youll have to start again from the begin
command line if you like):
ning, retyping all the commands.
$comFileObject = $comFileSystemObject.GetFile(
C:\boot.ini) So, lets create a new GPO using Windows
$comFileObject.Size PowerShell. The Group Policy team at Mi
Of course, to convert a VBScript that reads crosoft included a number of fully work
the size of a file, I could have used the Win ing VBScript samples with GPMC that you
dows PowerShell cmdlet that manages ob can take advantage of to speed things up.
jects in drives, but I wanted to show you how Theyre in the %ProgramFiles%\GPMC\
easy it is to access COM from Windows Pow Scripts directory, where youll also find a file
erShell. Note that though I tell Windows called gpmc.chm that contains the GPMC
PowerShell to create a COM object, the ob API documentation. Lets have a look at the
ject that actually gets created, $comFileSys CreateGPO.wsf script and dissect it to see
temObject here, is a .NET object that wraps what makes it tick.
the COM object and exposes its interface. Near the top youll see this line:
In the scope of this article, however, that Dim GPM
Set GPM = CreateObject(GPMgmt.GPM)
doesnt really matter.
This is basically the starting point of any
Windows PowerShell in action Group Policy management session or script
Now that youve seen how to access COM because it instantiates the GPMgmt.GPM
from Windows PowerShell, lets focus on class that allows access to most of the GPMC
Group Policy. The examples here will show functionality. Lets go ahead and do this from
short code snippets to give you an idea of Windows PowerShell instead:
how to use the GPMC APIs from Windows $gpm = New-Object -ComObject GPMgmt.GPM
$copyFlags = 0
if ($copyAcl)
{
$copyFlags = Constants.ProcessSecurity
}
$gpmResult = $gpmSourceGpo.CopyTo($copyFlags, $gpmTargetDomain, $targetGpo)
[void] $gpmResult.OverallStatus
with Group Policy and how you can reuse the equivalent in VBScript. Youll find it at
any COM object or VBScript code that con microsoft.com/technet/scriptcenter/topics/
sumes a COM object. winpsh/convert.
Windows PowerShell will be, just as Group In addition, the GPMC API is fully docu
Policy already is, a natural part of any Win mented; you can download the information
dows management environment. But there from the Group Policy site at microsoft.com/
are millions of lines of VBScript out there grouppolicy.
that will have to be migrated or maintained, Last but not least, if you havent already
and hopefully this tutorial will help. installed Windows PowerShell, what are
There are a number of sources you can you waiting for? Download it from micro
use to enhance your Group Policy adminis soft.com/powershell. Have fun.
tration and other areas where youve previ
ously used VBScript, including the Windows Thorbjrn Sjvold is the CTO and
PowerShell functions in the download, and a founder of Special Operations Software
great VBScript-to-Windows PowerShell con (www.specopssoft.com), a provider of Group
version guide on the TechNet Web site that Policy based Systems Management and
provides hints on how to do common tasks Security extension products. Reach him at
in Windows PowerShell when you know thorbjorn.sjovold@specopssoft.com.