Printserver Migration Part #4: Import Printer and Settings

Ich stelle hier ein paar Skripte zur Verfügung, welche einem die Migration eines Printservers etwas vereinfachten. Selbstverständlich kann man das auch mir Printmig oder anderen Tools durchführen. Ich wollte meinen neuen Printserver from scratch neu installieren, daher der etwas andere Weg. Die Migration wird in vier Teile erfolgen:

Part #1: Export TCP- und LPR-Ports
Part #2: Export Drucker mit allen Settings in eine XML-Datei
Part #3: Import TCP- und LPR-Ports
Part #4: Import aller Drucker mit allen Settings

WICHTIG: Vor dem nächsten schritt müssen erst alle nötigen Druckertreiber installiert werden. Sollte sich der Name oder die Version der Druckertreiber ändern, dann müssen diese vorher in der exportierten CSV-Datei angepasst werden.
Nachdem die Druckertreiber installiert wurden, kann man mit einem Get-PrinterDriver dir Druckertreiber anzeigen lassen. Hier findet man unter dem Punkt "Name" den Namen, welcher in der CSV entsprechend ausgetauscht werden muss. Sollte der Name nicht übereinstimmen, dann erhält man einen Fehler bei der Erstellung des Druckers, dass der Treiber nicht gefunden wurde. Nachdem dir Treiber installiert sind und die CSV ggf. angepasst wurde, können dir Drucker nun per Powershell erstellt werden (inkl. Konfiguration aus der XML).
Hinweis: Es hat sich gezeigt, dass Etikettendrucker nochmals gesondert geprüft werden müssen, da manche Informationen nicht im Windows-Treiber gespeichert wird (Druckkopf-Konfiguration).
Außerdem benötigt die Erstellung von Druckern, welche offline sind, ca. 1 Minute (TCP-Timeout).

Create-Printers_from_CSV.ps1
<# 
.NAME
    Create-Printers_from_CSV.ps1

.AUTHOR
    Ralf Entner

.SYNOPSIS
    Script creates all printers from a csv file. Config will be restored by import xml file.

.DESCRIPTION 
    Script creates all printers with all settings from a csv and xml file.
    With the testmode $true you can run the script without any changes. It will only show whatif
    A log file will be created
 
.NOTES 
    You have to install the printer drivers on the system before you run the script.
    printer driver names must be changed in the csv file if it differs from the original names.
    If the pritner is offline the creation will take approximal 1 mintue.

.COMPONENT 
    No powershell modules needed

.LINK 
    No Links
 
.Parameter ParameterName 
    $CSVPath - Define export path of the csv file
    $Testmode - Defines testmode: $true = test | $false = live
    $Logpath - Path for log file
#>

#Testmode ($true = active | $false = inactive)
$Testmode = $false

# CSV Import path
$CSVPath = "C:\tgswinv\Printmig\Printers.csv"

# XML import path
$XMLPath = "C:\tgswinv\Printmig\"

# Log File
$Logpath = "C:\tgswinv\Printmig\CreatePrinter.log"

#Start transciption 
Start-Transcript -Path $Logpath -Append

# Import Printers
$Printers = Import-Csv -Path $CSVPath -Delimiter ";"



foreach($Printer in $Printers){

    if(!(Get-Printer -Name $Printer.Name -ErrorAction SilentlyContinue))
    {
        Write-Host "Generating new pritner" $Printer.Name -ForegroundColor Green
        # If port not exists create a new one with parameters from csv import
        Add-Printer -Name $Printer.Name -PortName $Printer.Portname -DriverName $Printer.Drivername -Location $Printer.Location -Comment $Printer.Comment -WhatIf:$Testmode
        Start-Sleep -Seconds 2

        Write-Host "Import printer configuration from xml" $Printer.Name -ForegroundColor Green
        # If port not exists create a new one with parameters from csv import
        # Generate printer configuration path to xml file
        $XMLConfigFile = $XMLPath + $Printer.Name + ".xml"
        # Import printer configuration from xml file
        $XMLConfig = Get-Content $XMLConfigFile | Out-String
        # Set printer configuration from xml file
        Set-PrintConfiguration -PrinterName $Printer.Name -PrintTicketXml $XMLConfig -WhatIf:$Testmode
    }
}

Stop-Transcript


Printserver Migration Part #3: Import TCP- und LPR-Ports

Ich stelle hier ein paar Skripte zur Verfügung, welche einem die Migration eines Printservers etwas vereinfachten. Selbstverständlich kann man das auch mir Printmig oder anderen Tools durchführen. Ich wollte meinen neuen Printserver from scratch neu installieren, daher der etwas andere Weg. Die Migration wird in vier Teile erfolgen:

Part #1: Export TCP- und LPR-Ports
Part #2: Export Drucker mit allen Settings in eine XML-Datei
Part #3: Import TCP- und LPR-Ports
Part #4: Import aller Drucker mit allen Settings

Part #3: Import TCP- und LPR-Ports
Jetzt können wir die TCP- und LPR-Ports auf dem neuen Printserver erstellen lassen.
Auch hier gibt es wieder zwei Skripte, da TCP-Ports anders erzeugt werden als LPD-Ports.
ACHTUNG: Vor der Erstellung der LPR-Ports muss der LPD-Service erst über den Servermanager installiert werden, damit der LPR-Porttype zur Verfügung steht.
TIPP: Sollten die Namen oder die IPs der Ports geändert werden, dann bitte vorher in der CSV anpassen!

Create-Printerports_TCP_from_CSV.ps1
<# 
.NAME
    Create-Printerports_TCP_from_CSV.ps1

.AUTHOR
    Ralf Entner

.SYNOPSIS
    Script creates all TCP Printer Ports from a csv file.

.DESCRIPTION 
    Script creates all TCP Printer Ports with all settings from a csv file. In the csv file there are address, port number and smtp settings.
 
.NOTES 
    With the testmode $true you can run the script without any changes. It will only show whatif

.COMPONENT 
    No powershell modules needed

.LINK 
    No Links
 
.Parameter ParameterName 
    $CSVPath - Define import path of the csv file
    $Testmode - Defines testmode: $true = test | $false = live
#>

$CSVPath = "C:\tgswinv\Printmig\PortsTCP.csv"
$Testmode = $false

# Import CSV with Port Informations
$PrinterPorts = Import-CSV -Path $CSVPath -Delimiter ";"

# Loop through 
Foreach($Port in $PrinterPorts){

Write-Host "Creating Printerport $Name"

# Check if Printerport already exists
if(!(Get-PrinterPort -Name $Port.Name -ErrorAction SilentlyContinue))
    {
        Add-PrinterPort -Name $Port.Name -PrinterHostAddress $Port.PrinterHostAddress -PortNumber $Port.Portnumber -SNMPCommunity $Port.SNMPCommunity -SNMP $true -WhatIf:$Testmode
    }
}

Create-Printerports_LPR_from_CSV.ps1
<# 
.NAME
    Create-Printerports_LPR_from_CSV.ps1

.AUTHOR
    Ralf Entner

.SYNOPSIS
    Script creates all LPR Printer Ports from a csv file.

.DESCRIPTION 
    Script creates all LPR Printer Ports with all settings from a csv file.
    In the csv file there are name, protocol, port nubmer and printer host address.
 
.NOTES 
    With the testmode $true you can run the script without any changes. It will only show whatif

.COMPONENT 
    ATTENTION: You have to install the LPR printer monitor before you add the ports!
    No powershell modules needed

.LINK 
    No Links
 
.Parameter ParameterName 
    $CSVPath - Define import path of the csv file
    $Testmode - Defines testmode: $true = test | $false = live
#>

$CSVPath = "C:\tgswinv\Printmig\PortsLPR.csv"
$Testmode = $false

# Import CSV with Port Informations
$PrinterPorts = Import-CSV -Path $CSVPath -Delimiter ";"

# Loop through 
Foreach($Port in $PrinterPorts){

Write-Host "Creating Printerport "$Port.Name

# Check if Printerport already exists
if(!(Get-PrinterPort -Name $Port.Name -ErrorAction SilentlyContinue))
    {
        #Add-PrinterPort -Name $Port.PrinterName -PrinterHostAddress $Port.hostname -WhatIf:$Testmode
        Add-PrinterPort -PrinterName $Port.Printername -HostName $port.Hostname   -WhatIf:$Testmode
    }
}



“Das einzig sichere System müsste ausgeschaltet, in einem versiegelten und von Stahlbeton ummantelten Raum und von bewaffneten Schutztruppen umstellt sein.”
Gene Spafford (Sicherheitsexperte)