powershell

Auto Mapping Script in Powershell: Filtration Error


$ACTIVE_PRINTERS = Get-Printer  
try {
    if (Get-Variable -Name LM -ErrorAction SilentlyContinue){
        Write-Host "Lexmark Printers"
        foreach ($PrinterName in $LM.Keys) {
            $ip = $LM[$PrinterName]
            if (!$ACTIVE_PRINTERS.Contains($PrinterName)) {
                Add-PrinterPort "$ip"
                Add-Printer -DriverName "Lexmark Universal v2" -Name $PrinterName -PortName "$ip"
                }
            Write-Host "$PrinterName : $ip Are Mapped"
        }
      
        
    } else {
        Write-Host "No Lexmark Printers Located in $sqdrn"
        }
} catch {
    Write-Error "An error occurred: $($_.Exception.Message)"
}

I am attempting to make a script that runs through a list of IP addresses and map the printer and name, the mapping and naming aspect works but the filtration doesn't work and it just tells me the printer:ip pair exists and errors out. Even with the printers removed/unmapped via settings and control panel. Here is an example of the key value pairs located in the LM array.

$LM = @{"ROOM#1"="192.168.x.1";"ROOM#2"="192.168.x.2"}

Below is the error recieved.

Add-PrinterPort : The specified port already exists.
At C:\Users\foobar\Desktop\MappingPrinter.ps1:85 char:17
+                 Add-PrinterPort "$ip"
+                 ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (MSFT_PrinterPortTasks:ROOT/StandardCimv2/MSFT_PrinterPortTasks) [Add-PrinterPort], CimException
    + FullyQualifiedErrorId : HRESULT 0x800700b7,Add-PrinterPort
 
Add-Printer : The specified printer already exists.
At C:\Users\foobar\Desktop\MappingPrinter.ps1:86 char:17
+ ...             Add-Printer -DriverName "Lexmark Universal v2" -Name $Pri ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException
    + FullyQualifiedErrorId : HRESULT 0x8007070a,Add-Printer

I have tried many variations of the if statement for $ACTIVE_PRINTERS and am fairly new to powershell. If anyone needs more information or i didnt give enough please let me know!


Solution

  • I'm not sure where you're getting $ACTIVE_PRINTERS from, as I can't find any reference to that enviroment variable and it doesn't return anything for me.

    However, I think the Get-Printer cmdlet should give you the info you need, specifically :

    foreach ($PrinterName in $LM.Keys) {
        $ip = $LM[$PrinterName]
        if (!($(Get-Printer).Name -like "*$PrinterName*")) {
            Add-PrinterPort "$ip"
            Add-Printer -DriverName "Lexmark Universal v2" -Name $PrinterName -PortName "$ip"
            }
        Write-Host "$PrinterName : $ip Are Mapped"
    }
    

    Update: To answer the follow up question, if you view the output from Get-Printer or for that matter $ACTIVE_PRINTERS, you'll see the various details that are visible.

    So for instance $ACTIVE_PRINTER.name gives you the display name of the printer, while ACTIVE_PRINTER.drivername gives the driver.