Get shared printer configuration from GPO when computer is only Azure AD joined
As a follow up to my last blog post about network drive mapping configuration, I’ve now created another script to get shared printer configuration from GPO.
PowerShell code
function Get-GPO {
param(
[parameter(Mandatory=$false)]
[string]$DisplayName
)
$Searcher = New-Object DirectoryServices.DirectorySearcher -Property @{
Filter = "(objectClass=groupPolicyContainer)"
}
if ($DisplayName) {
$Searcher.Filter = "(&(objectClass=groupPolicyContainer)(displayname=$DisplayName))"
}
$Searcher.SearchRoot = "LDAP://$env:USERDNSDOMAIN"
$Searcher.FindAll() | ForEach-Object {
[PSCUSTOMOBJECT]@{
DisplayName = $_.properties.displayname -join ""
FilePath = $_.properties.gpcfilesyspath -join ""
}
}
}
$GPO = Get-GPO
$PrinterConnectConfig @()
foreach ($Policy in $GPO) {
$GPODisp = $Policy.DisplayName
$PrefPath = "$($Policy.FilePath)\User\Preferences"
$XMLPath = "$PrefPath\Printers\Printers.xml"
if (Test-Path "$XMLPath") {
[xml]$PrintXML = Get-Content "$XMLPath"
foreach ($Printer in $PrintXML.Printers.SharedPrinter) {
$PrinterConnectConfig += [PSCUSTOMOBJECT]@{
GPOName = $GPODisp
PrinterPath = $printer.Properties.Path
PrinterAction = $printer.Properties.action.Replace("U","Update").Replace("C","Create").Replace("D","Delete").Replace("R","Replace")
PrinterDefault = $printer.Properties.default.Replace("0","False").Replace("1","True")
PrinterFilterGroup = $printer.Filters.FilterGroup.Name
}
}
}
}