Automic Workload Automation

 View Only

Powershell to spool to printer

  • 1.  Powershell to spool to printer

    Posted Aug 02, 2022 05:43 PM
    I'm attempting to run a powershell script that will spool pdfs to a printer. The script works in powershell on the Automation Engine server, but through Automation Engine it fails.  I also get the same result trying to run it as a file with the job simply executing:  C:\temp\test_print
    Get-Printer : The spooler service is not reachable.  Ensure the spooler service is running.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:5 char:12
    + $printer = Get-Printer |?{$_.name -eq "\\ucuflowstn01-p\Canon MFD 859 ...
    +            ~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Get-P 
       rinter], CimException
        + FullyQualifiedErrorId : HRESULT 0x800706ba,Get-Printer
     
    New-Object : Cannot find type [System.Drawing.Printing.PrintDocument]: verify that the assembly 
    containing this type is loaded.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:11 char:22
    +     $PrintDocument = New-Object System.Drawing.Printing.PrintDocument
    +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
     
    The property 'PrinterName' cannot be found on this object. Verify that the property exists and 
    can be set.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:12 char:5
    +     $PrintDocument.PrinterSettings.PrinterName = $printer.Name
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     
    The property 'DocumentName' cannot be found on this object. Verify that the property exists and 
    can be set.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:13 char:5
    +     $PrintDocument.DocumentName = "$FileName"
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     
    The property 'PrintFileName' cannot be found on this object. Verify that the property exists and 
    can be set.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:14 char:5
    +     $printDocument.PrinterSettings.PrintFileName = "$FileName"
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     
    You cannot call a method on a null-valued expression.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:15 char:5
    +     $PrintDocument.Print()
    +     ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    New-Object : Cannot find type [System.Drawing.Printing.PrintDocument]: verify that the assembly 
    containing this type is loaded.
    At C:\Automic\AE_12.3\Agents\Windows\temp\JAAEDIVD.TXT.ps1:11 char:22
    +     $PrintDocument = New-Object System.Drawing.Printing.PrintDocument
    +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand



    My script:


    $printer = Get-Printer |?{$_.name -eq "network printer here"}
    $FilePath = "C:\temp\*.pdf"
    Get-ChildItem "$FilePath"|
    ForEach-Object {
    $FileName = $_.FullName
    $PrintDocument = New-Object System.Drawing.Printing.PrintDocument
    $PrintDocument.PrinterSettings.PrinterName = $printer.Name
    $PrintDocument.DocumentName = "$FileName"
    $printDocument.PrinterSettings.PrintFileName = "$FileName"
    $PrintDocument.Print()
    }