Last active
February 25, 2021 12:08
-
-
Save zbalkan/3d7c3f04d7a22ed63336dea038f0cd01 to your computer and use it in GitHub Desktop.
A Powershell script that creates a printer GPP. It reads form a csv file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Configuration variables | |
| $PrintServer = "" | |
| $CSVFilePath = "printers.csv" | |
| $XMLFilePath = "printers.xml" | |
| # The code | |
| Import-Module ActiveDirectory | |
| $Domain = (Get-ADDomain).name | |
| $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition | |
| $Now = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") | |
| # Set The Formatting | |
| $XMLSettings = New-Object System.Xml.XmlWriterSettings | |
| $XMLSettings.Indent = $true | |
| $XMLSettings.IndentChars = " " | |
| # Set the File Name Create The Document | |
| $XMLWriter = [System.XML.XmlWriter]::Create($XMLfilePath, $XMLSettings) | |
| # Write the XML Declaration | |
| $XMLWriter.WriteStartDocument() | |
| # Start the Root Element | |
| $XMLWriter.WriteComment('Root of the policy') | |
| $XMLWriter.WriteStartElement('Printers') # Start root | |
| $XMLWriter.WriteAttributeString('clsid', '{1F577D12-3D1B-471e-A1B7-060317597B9C}') | |
| Import-Csv (Join-Path -Path $ScriptPath -ChildPath $CSVFilePath) | ForEach-Object { | |
| $GroupName = $_.Group | |
| [Microsoft.ActiveDirectory.Management.ADGroup]$Group = Get-ADGroup -Filter { name -like $GroupName } | |
| # Start SharedPrinter Element | |
| $XMLWriter.WriteStartElement('SharedPrinter') # Start SharedPrinter | |
| $XMLWriter.WriteAttributeString('clsid', '{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}') | |
| $XMLWriter.WriteAttributeString('name', $_.Printer) | |
| $XMLWriter.WriteAttributeString('status', $_.Printer) | |
| $XMLWriter.WriteAttributeString('image', '1') | |
| $XMLWriter.WriteAttributeString('bypassErrors', '1') | |
| $XMLWriter.WriteAttributeString('changed', $Now) | |
| $XMLWriter.WriteAttributeString('uid', [System.Guid]::NewGuid().ToString()) | |
| # Start Properties Element | |
| $XMLWriter.WriteStartElement('Properties') # Start Properties | |
| $XMLWriter.WriteAttributeString('action', 'R') | |
| $XMLWriter.WriteAttributeString('comment', '') | |
| $XMLWriter.WriteAttributeString('path', "\\$PrintServer\$($_.Printer)") | |
| $XMLWriter.WriteAttributeString('location', '') | |
| $XMLWriter.WriteAttributeString('default', $_.Default) | |
| $XMLWriter.WriteAttributeString('skipLocal', '0') | |
| $XMLWriter.WriteAttributeString('deleteAll', '0') | |
| $XMLWriter.WriteAttributeString('persistent', '0') | |
| $XMLWriter.WriteAttributeString('deleteMaps', '0') | |
| $XMLWriter.WriteAttributeString('port', '') | |
| $XMLWriter.WriteEndElement() # End Properties | |
| # Start Filters Element | |
| $XMLWriter.WriteStartElement('Filters') # Start Filters | |
| # Start FilterGroup Element | |
| $XMLWriter.WriteStartElement('FilterGroup') | |
| $XMLWriter.WriteAttributeString('bool', 'AND') | |
| $XMLWriter.WriteAttributeString('not', '0') | |
| $XMLWriter.WriteAttributeString('name', "$Domain\$($Group.Name)") | |
| $XMLWriter.WriteAttributeString('sid', $Group.SID) | |
| $XMLWriter.WriteAttributeString('userContext', '1') | |
| $XMLWriter.WriteAttributeString('primaryGroup', '0') | |
| $XMLWriter.WriteAttributeString('localGroup', '0') | |
| $XMLWriter.WriteEndElement() # End FilterGroup | |
| $XMLWriter.WriteEndElement() # End Filters | |
| $XMLWriter.WriteEndElement() # End SharedPrinter | |
| } | |
| $XMLWriter.WriteEndElement() # End root | |
| # End, Finalize and close the XML Document | |
| $XMLWriter.WriteEndDocument() | |
| $XMLWriter.Flush() | |
| $XMLWriter.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment