简体   繁体   中英

Printer Migration - Powershell script

I have found some great examples on foreach loops in Powershell here but I just can't wrap my head around foreach loops for what I am doing.

I found great scripts that deal with migrating printer when migrating from one Windows print server to another however my challenge is that I am migrating from an Novell iPrint server to a Windows server.

The struggle is that the printer name or share name (or any printer property) for iPrint printer is not the hostname so I have to come up with some translation table with iPrint name and Printer hostname.

Initially, I wanted to just have column 2 of my translation table have it execute my powershell command to install a network printer which would make things easier.

I am in the process of trying to create a logon script to query printers that are installed on computer and have it do a 'foreach' loop against a CSV with iPrint names and hostnames.

csv 1

installediprintprintername1
installediprintprintername2
installediprintprintername3

printtranslationtable.csv

column 1               column 2
iprintprintername1     hostnameprinter1
iprintprintername2     hostnameprinter2
iprintprintername3     hostnameprinter3
iprintprintername4     hostnameprinter4

This is what I got so far but not able to get it to work. Any help would be appreciated!

$printers = @(Get-wmiobject win32_printer)

$path = "\\networkdrive\printtranslationtable.csv"
$printertranslation = Import-Csv -path $path

foreach ($iprintprinter in $printtranslationtable) {
    foreach ($name in $csv1) {
        if ($name -eq $printtranslationtable.column1) {
            Write-Host $newPrinter = $printtranslationtable.column2
        }
    }
}

Update So I was able to tweak the script @TheMadTechnician suggested and able to get this PS script to work in my environment. What I am trying to do is to check if new printers are installed and if they are then just exit script. This is what I have but can't get it to exit or break. I was also trying to write the new printers into text file but not necessary, I would like for it to stop executing script.

if (($printers.name -like "\\winprint*") -eq $true) { $printers.name -like "\\winprint\\" | out-file -FilePath "C:\\windowsprinters.txt" -Append {break} {exit} }

When you read the file with Import-Csv , PowerShell creates an array of custom objects with property names from the header line. On the other hand Get-Content produces simple array of string values. I came up with this one liner, which goes thru the translation table and checks if the printer list contains one. This is not optimal if you have billions of printers, but keeps things clear:

printers.txt:

iprinter2
iprinter3

printertable.csv:

"Column1";"Column2"
"iprinter1";"hostname1"
"iprinter2";"hostname2"
"iprinter3";"hostname3"
"iprinter4";"hostname4"

PowerShell:

$printers = Get-Content .\printers.txt
$prtable = Import-Csv -Delimiter ";" .\printertable.csv
$prtable | ?{ $printers -contains $_.Column1 } | %{Write-Host "Install $($_.Column2)"}

Ok, so you query what printers are installed, and you have a translation table loaded from a CSV, now you just need to look at that translation table and cross reference which entries have a listing in the local computer's printer listings.

$printers = @(Get-wmiobject win32_printer)

$path = "\\networkdrive\printtranslationtable.csv"
$printertranslation = Import-Csv -path $path

$printertranslation | Where{$_.Column1 -in $printers.ShareName} | ForEach{ Add-Printer $_.Column2 }

I don't know what property of the win32_printer object aligns best for you, but I would suggest ShareName or DeviceId. Those should be something like:

ShareName: XeroxColor02
DeviceId:  \\printserver\XeroxColor02

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM