Select Page

Why a PowerShell Advanced Function

This is a bit of a stream of conscious follow-up to a previous post related to managing Deployed Printers with the Group Policy Automation Engine. I decided today, while working with someone, that porting this to an Advanced Function was the right direction. I’m not done here, but wanted to quickly share the experience. Taking the time to think more broadly and long term about the impact of our work can be helpful. What I mean is, if you spend 30 minutes figuring out the syntax for a relatively complex task that you will repeat n times, why not spend 60 minutes and make it truly reusable. Additionally the additional work is just good at developing your PowerShell muscles. Also, if you are like me, you are super productive when you are procrastinating! I have a lot of other things to do that are higher priority right now… but this is fun!

The Story

You have a series of OUs. Each OU has 0 – n GPOs linked. Each of those GPOs has 0 – n printers configured in either ‘Computer Configuration’ or ‘User Configuration’. You get a new Print server and you want to iterate through each OU, find GPOs, update deployed printers to point to the new server. Pretty straight forward task and one that most of us have encountered. Have you ever gone 1 by 1 through each GPO and made the same change? Well that is one big reason for the Group Policy Automation Engine. This function addresses that using a few different techniques. I’ll paste the function at the end.

Take a look at the video below as I walk through how it works. If you like it and already have our Group Policy Automation Engine great, if not, you may learn something about advanced functions or our freeware cmdlets. Please let us know if you have any thoughts/suggestions.

The Function

This is not complete, but is a good start. You can see how you can use this to build up your own automation around Group Policy and more. You can copy the text and save as a .ps1 file.

—–The Function ↓—-
<#
.Synopsis
Move-Printer will update printers in GPOs with new target Server.
.DESCRIPTION
Move-Printer will, for a specific scope (OU), itterate through each Deployed Printer in
both the ‘Computer Configuration’ and ‘User Configuration’ portions of the GPOs which are
linked to the referenced OU. It will update those printers with the provider ‘ServerName’.There are two primary dependencies on this function. SDM Software’s Group Policy Automation
Engine and the SDM GPMC cmdlets.
.EXAMPLE
Example of how to use this cmdlet
#>
function Move-Printer
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
# The ‘ServerName’ parameter of this function is the name of the
# destination server. The GPO will update to the new server. Provide
# the ServerName as a UNC name e.g. ‘\\servername’
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$ServerName,
        # Make sure to use the LDAP path for the OU you are targeting The
# fucntion will go through each GPO linked to this OU and update each
# printer.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$Scope
        # todo – add a switch parameter to choose User or Computer Printers.
# Default should be both
)
    Begin
{
}
Process
{
# Grab all GPOs linked to the OU referenced in the ‘Scope’
# parameter
$links = Get-SDMgplink -Scope “$Scope”
        foreach ($link in $links)
{
$gpo = Get-GPAEGPO $link.name$compprinters = ($gpo.GetObject(“Computer Configuration/Windows Settings/Deployed Printers”).settings.name)
foreach ($Printer in $compprinters)
{
$GPO | Set-GPDeployedPrinter -GPONode Computer -Name “$printer” -ServerName “$ServerName”
}
            $usrprinters = ($gpo.GetObject(“User Configuration/Windows Settings/Deployed Printers”).settings.name)
foreach ($Printer in $usrprinters)
{
$GPO | Set-GPDeployedPrinter -GPONode User -Name “$printer” -ServerName “$ServerName”
}
}
}
End
{
}
}