PowerShell迁移Novell iPrint至Windows服务器的foreach循环及映射问题求助
Got it, let's tackle this Novell iPrint to Windows print server migration—you're right that the tricky part is mapping those non-standard iPrint properties to Windows' expected values, and foreach is exactly the tool to automate this at scale. Here's a step-by-step approach tailored to your scenario:
1. First, Pull Your iPrint Printer Inventory
You need a structured list of all iPrint printers and their key attributes (name, location, driver, IP address, etc.). Novell's iPrint has command-line tools you can leverage for this—for example:
# Use iPrint's CLI to export printer details (adjust path to your iprint.exe) $iPrintRawOutput = & "C:\Program Files (x86)\Novell\iPrint Client\iprint.exe" list printers -format csv # Convert the CSV output to PowerShell objects for easy manipulation $iPrintPrinters = $iPrintRawOutput | ConvertFrom-Csv
If the default CSV doesn't include critical fields like IP address, query individual printers to fill in the gaps:
# Add IP address to each iPrint printer object foreach ($printer in $iPrintPrinters) { $ipOutput = & "C:\Program Files (x86)\Novell\iPrint Client\iprint.exe" get printer $printer.Name ip-address $printer | Add-Member -MemberType NoteProperty -Name "IPAddress" -Value ($ipOutput -split ": ")[1].Trim() }
2. Build Your Property Mapping Table
This is the make-or-break piece—since iPrint's names/properties don't align with Windows, create a translation layer. Use a hashtable for small inventories, or a CSV file for large migrations (easier to edit collaboratively):
Option A: Inline Hashtable
$printerMappings = @( @{ iPrintName = "HR_Office_Printer" WindowsPrinterName = "HR Department Laser Printer" WindowsShareName = "HR-Printer-01" WindowsDriver = "HP LaserJet Pro M404dn" WindowsPrintServer = "WIN-PRINTSRV01" }, @{ iPrintName = "Finance_MFP" WindowsPrinterName = "Finance Multi-Function Printer" WindowsShareName = "Finance-MFP-02" WindowsDriver = "Xerox VersaLink C7020" WindowsPrintServer = "WIN-PRINTSRV01" } )
Option B: Import from CSV (Recommended for Large Sets)
Create a PrinterMappings.csv file with columns like:
iPrintName,WindowsPrinterName,WindowsShareName,WindowsDriver,WindowsPrintServer HR_Office_Printer,HR Department Laser Printer,HR-Printer-01,HP LaserJet Pro M404dn,WIN-PRINTSRV01 Finance_MFP,Finance Multi-Function Printer,Finance-MFP-02,Xerox VersaLink C7020,WIN-PRINTSRV01
Then import it into PowerShell:
$printerMappings = Import-Csv -Path "C:\PrinterMappings.csv"
3. Use Foreach to Automate the Migration Loop
Now loop through each iPrint printer, match it to your mapping, and create the corresponding printer on your Windows server. We'll add error handling to avoid breaking the loop if one printer fails:
foreach ($iPrinter in $iPrintPrinters) { # Find the matching mapping for the current iPrint printer $matchingMap = $printerMappings | Where-Object { $_.iPrintName -eq $iPrinter.Name } if ($matchingMap) { try { Write-Host "Starting migration for: $($iPrinter.Name) → $($matchingMap.WindowsPrinterName)" # Create TCP/IP port if it doesn't exist $portParams = @{ ComputerName = $matchingMap.WindowsPrintServer Name = "IP_$($iPrinter.IPAddress)" PrinterHostAddress = $iPrinter.IPAddress PortNumber = 9100 Protocol = "Raw" ErrorAction = Stop } if (-not (Get-PrinterPort -ComputerName $matchingMap.WindowsPrintServer -Name $portParams.Name -ErrorAction SilentlyContinue)) { Add-PrinterPort @portParams Write-Host "Created TCP/IP port: $($portParams.Name)" } # Create the printer on Windows server $printerParams = @{ ComputerName = $matchingMap.WindowsPrintServer Name = $matchingMap.WindowsPrinterName DriverName = $matchingMap.WindowsDriver PortName = $portParams.Name Shared = $true ShareName = $matchingMap.WindowsShareName Location = $iPrinter.Location Comment = $iPrinter.Description ErrorAction = Stop } Add-Printer @printerParams Write-Host "Successfully migrated $($iPrinter.Name)`n" } catch { Write-Error "Failed to migrate $($iPrinter.Name): $_" } } else { Write-Warning "No mapping found for iPrint printer: $($iPrinter.Name) — skipping." } }
Key Notes to Remember
- Preinstall Drivers: Ensure all required printer drivers are already installed on the Windows print server—
Add-Printerwill fail if the driver isn't present. - Test First: Run the script with a small subset of printers first to validate mappings and catch issues early.
- Adjust Fields: Tweak the script to match the actual column names from your iPrint export (some environments might use different labels).
内容的提问来源于stack exchange,提问作者Knallidge

