Printer Migration: Client Side Script to Change Print Servers
Param([parameter(Mandatory=$true,Position=0,
HelpMessage="Old Server")]
$OldServer,
[parameter(Mandatory=$true,Position=1,
HelpMessage="New Server")]
$NewServer,
[parameter(Mandatory=$true,Position=2,
HelpMessage="Workstation Name")]
$ThisPC,
[parameter(Mandatory=$true,Position=3,
HelpMessage="Username")]
$ThisUser,
[parameter(Mandatory=$true,Position=4,
HelpMessage="Path to Log")]
$OutputLog)
# Example command-line: .\Run-PrintMigration.ps1 -ExecutionPolicy Unrestricted -command "& '\\domain.com\scripts\Run_PrintMigration.ps1' '\\oldserver' '\\newserver' '$WkSta' '$UserID' '\\domain.com\logs$\'"
# This procedure adds a new printer
Function InstallNewPrinter{
Param ([string]$currentPrinter, [string]$newPrinter)
$Global:TextLog += "++ Add: " + $newPrinter
#Add new Printer
$global:net.AddWindowsPrinterConnection($newPrinter)
#Add current printer to the TobeDeleted List
$global:PrintersTobeDeleted += $currentPrinter
}
# This procedure sets the respected new printer default
Function SetPrinterDefault{
Param ([string]$defaultPrinter)
#If current printer is default, make the new a default printer
$global:net.SetDefaultPrinter($defaultPrinter)
}
# This procedure deletes all the old printer which were replaced by this script.
Function DeleteCurrentPrinters{
foreach ($printerTodelete in $global:PrintersTobeDeleted)
{
$Global:TextLog += "-- Delete: " + $printerTodelete
$global:net.RemovePrinterConnection($printerTodelete)
}
}
# Set print server name
$Printserver = "."
# Uncomment to use these variables for the new/old print server names
#$newServer = "\\NEWPRINT"
#$oldServer = "\\OLDPRINT"
$Global:TextLog = @()
$Printers = @()
$global:PrintersTobeDeleted = @()
$OutputFile = $OutputLog+$ThisUser+"_"+$ThisPC+"_"+[DateTime]::Now.Year.ToString()+[DateTime]::Now.Month.ToString()+[DateTime]::Now.day.ToString()+[DateTime]::Now.hour.ToString()+[DateTime]::Now.Minute.ToString()+[DateTime]::Now.Second.ToString()+".txt"
$Global:TextLog += "$OldServer $NewServer $ThisPC $ThisUser $OutputLog"
# This section will identify all the installed Network printer under logged in users profile using WMI
$Printers = Get-WMIObject Win32_Printer -computername $Printserver -Filter "Network=True"
if($Printers)
{
# This is a blank array to store the installed Network printer under logged in users profile and pass it on
$PrintersTobeDeleted = @()
$newPrinter = ""
$defaultPrinter = ""
$Global:TextLog += "Log Start"
# Get Network object
$global:net = new-Object -com WScript.Network
foreach ($Printer in $Printers)
{
#Check to see if it is a printer connected to the old server
if ($Printer.SystemName -like "$oldServer*")
{
#A check to see if we have to add a new printer
$AddNewPrinter = $true
#Check to see if the printer also exists in the new server
Foreach ($comparePrinter in $Printers)
{
If (($Printer.shareName -eq $comparePrinter.shareName) -and ($comparePrinter.SystemName -like "$newServer*"))
{
#Remove old printer by Adding current printer to the TobeDeleted List
$global:PrintersTobeDeleted += $Printer.Name
#New Printer has already been created no need to add another one.
$AddNewPrinter = $false
}
}
#If the new printer does not exist, create a new one
if($AddNewPrinter)
{
switch ($Printer.ShareName)
{
"OLDPRN01" {$newPrinter = $newServer + "\NEWPRN01"}
"OLDPRN02" {$newPrinter = $newServer + "\NEWPRN02"}
"OLDPRN03" {$newPrinter = $newServer + "\NEWPRN03"}
"OLDPRN04" {$newPrinter = $newServer + "\NEWPRN04"}
default {$newPrinter = $newServer + "\" + $Printer.Sharename}
}
if ($Printer.Default)
{
$defaultPrinter = $newPrinter
}
InstallNewPrinter $Printer.Name $newPrinter
$newPrinter = ""
}
}
Else
{
$Global:TextLog += ".. NO Change: " + $Printer.Name
}
if ($defaultPrinter -ne "")
{
SetPrinterDefault $defaultPrinter
}
}
DeleteCurrentPrinters
$Global:TextLog += "Log End"
#Write-output $Global:TextLog
#Write-Output $OutputFile
$Global:TextLog | Out-File $OutputFile
}