none
"The Printer Name is invalid" when creating instance of System.Printing.PrintServer RRS feed

  • Question


  • ASP.net application & Powershell 2.0 to manage print queues, when creating an instance of System.Printing.PrintServer it throws an exception when connecting to some Windows 2003 x86 SP2 Servers,

    Making the Connection from Win7, 2008 & 2008 R2 Same issue

    Code:

    Add-Type -AssemblyName System.Printing
    New-Object System.Printing.PrintServer "\\E9411S01SV003.violet.schools.qa"
    New-Object : Exception calling ".ctor" with "1" argument(s): "An exception occurred while creating the PrintServer
    object. Win32 error: The printer name is invalid.
    "
    At line:1 char:1
    + New-Object System.Printing.PrintServer "\\E9411S01SV003.violet.schools.qa"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand


    When wiresharking the traffic, of working vs non working

    the nonworking ones show this error:

    NT Status: STATUS_OBJECT_NAME_NOT_FOUND (0xc0000034)


    anyone have any suggestions on how to resolve this?


    Regards,

    Adam

    Monday, August 26, 2013 1:28 AM

Answers

  • Hello AdanSalfinger,

    Welcome to MSDN Forums.

    The address \\E9411S01SV003.violet.schools.qa was a DNS alias and the PrintServer constructor was unable to deal with it. To get around this issue, I have some code for you to try:

    try
            {
                // Create object to monitor the printer queue
                PrintServer printServer = new PrintServer(serverPath);
                mPrintQueue = printServer.GetPrintQueue(printerName);
            }
            catch (PrintServerException ex)
            {
                // If the problem might be creating the server because the name is an alias
                if (ex.Message.Contains("printer name is invalid."))
                {
                    string actualServerHostname = "\\\\" + Dns.GetHostEntry(serverPath.TrimStart('\\')).HostName;
                    // Create object to monitor the printer queue
                    PrintServer printServer = new PrintServer(actualServerHostname);
                    mPrintQueue = printServer.GetPrintQueue(printerName);
                    // Write to log about the use of a different address
                }
                else
                {
                    throw;
                }
            }

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 27, 2013 4:18 AM
    Moderator