You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量脚本:打印机队列从旧服务器到新服务器自动重映射求助

Hey Adam, let's break down a scalable, automated solution to remap those print queues from your old servers to the new ones—since you’ve got thousands of clients with non-uniform deployments and user-managed printers, we need something that’s low-impact and handles both existing and future user-added queues.

Core Approach

Since your new servers already mirror the old ones' queue names exactly, the problem boils down to replacing references to \\OldServer\QueueName with \\NewServer\QueueName across all client devices. We’ll use PowerShell for the heavy lifting (it’s built into all modern Windows clients) and pair it with deployment tools to scale to thousands of machines.

Step-by-Step Implementation

1. Build the Remapping Script

This script will detect printers pointing to your old servers, delete the old connections, add the new ones, and preserve default printer settings. Save it as RemapPrinters.ps1:

# Define your old and new server pairs
$oldServers = @("OldPrintServer01", "OldPrintServer02")
$newServerMap = @{
    "OldPrintServer01" = "NewPrintServer01"
    "OldPrintServer02" = "NewPrintServer02"
}

# Set up a log file to track changes (creates directory if missing)
$logPath = "C:\Temp\PrinterRemapLog.txt"
if (-not (Test-Path (Split-Path $logPath))) {
    New-Item -Path (Split-Path $logPath) -ItemType Directory | Out-Null
}

# Get all network printers linked to old servers
$printers = Get-WmiObject -Class Win32_Printer | Where-Object {
    $_.Network -eq $true -and $_.Name -match "^\\\\($($oldServers -join '|'))\\.*"
}

foreach ($printer in $printers) {
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    try {
        # Parse server and queue name from the printer path
        $pathParts = $printer.Name -split "\\", 4
        $oldServer = $pathParts[2]
        $queueName = $pathParts[3]
        $newServer = $newServerMap[$oldServer]

        if ($newServer) {
            $newPrinterPath = "\\$newServer\$queueName"
            # Delete the old printer connection
            $printer.Delete()
            # Add the new printer connection
            Add-Printer -ConnectionName $newPrinterPath
            # Preserve default printer status if needed
            if ($printer.Default -eq $true) {
                Set-Printer -Name $newPrinterPath -Default $true
            }
            # Log success
            "$timestamp - SUCCESS: Remapped $($printer.Name) to $newPrinterPath" | Out-File $logPath -Append
        } else {
            "$timestamp - WARNING: No new server mapped for old server $oldServer" | Out-File $logPath -Append
        }
    } catch {
        "$timestamp - ERROR: Failed to remap $($printer.Name) - $_" | Out-File $logPath -Append
    }
}

2. Deploy the Script at Scale

Choose the deployment method that fits your environment:

Option A: Domain Group Policy (User Login Script)

  • Place the script in a domain-accessible shared folder (e.g., \\DomainController\Scripts\) and set permissions so all users can read it.
  • Open Group Policy Management, create a new GPO, go to User Configuration > Policies > Windows Settings > Scripts (Logon/Logoff).
  • Add RemapPrinters.ps1 as a logon script. This ensures the script runs every time a user logs in, automatically handling any new old-server queues they add later.

Option B: SCCM/Intune Deployment

  • Package the script as a "PowerShell Script" deployment in SCCM or Intune.
  • Set the deployment to run on a recurring schedule (e.g., weekly) to catch offline clients and new user-added printers.
  • Target all client devices in your environment.

Option C: Remote Execution via WinRM

If you have WinRM enabled on clients, you can run the script remotely in bulk:

# List of client computers (from a text file)
$clientList = Get-Content "C:\Temp\ClientComputers.txt"
# Run the script on all clients
Invoke-Command -ComputerName $clientList -FilePath "C:\Scripts\RemapPrinters.ps1" -Credential (Get-Credential)

3. Validation & Rollback

  • Logging: Each client will log changes to C:\Temp\PrinterRemapLog.txt—you can collect these logs centrally if needed to verify success rates.
  • Backup: Add a backup step to the script to preserve existing printer configurations before making changes:
    # Backup current printers to XML before remapping
    Get-Printer | Export-Clixml "C:\Temp\PrinterBackup_$(Get-Date -Format 'yyyyMMdd').xml"
    
  • Rollback Script: If you need to revert to old servers, modify the original script to swap $oldServers and $newServerMap values and re-run.
Key Considerations
  • User Permissions: Since users already have rights to add/modify printers, running the script in the user’s context (via logon script) will work without elevated privileges.
  • Offline Clients: Logon scripts and SCCM/Intune deployments will automatically run the script the next time the client comes online.
  • Non-Standard Queues: If any queue names don’t exactly match between old and new servers, update the script to include a queue-specific mapping (e.g., add a $queueMap hashtable to handle exceptions).
  • Testing: Always test the script on a small subset of clients first to catch edge cases (e.g., printers with special characters in names).

Let me know if you need help adapting this to your specific environment—whether it’s handling non-domain clients, adding more error handling, or integrating with your existing management tools!

内容的提问来源于stack exchange,提问作者Adam_231

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:12:48