none
Remote Credentials Using AddWindowsPrinterConnection Powershell Script on Windows 2008R2 RRS feed

  • Question

  • I am writing a fairly basic powershell script adding printer shares to a Windows 2008 R2 server. If I log onto the remote print server before I run the script it works fine. If I log in with a different profile and run the script, I receive the following error: "Logon failure: unknown user name or bad password." Here is where it get complicated, the server I am running the script on is not in a domain and the printers are on a server in a domain with a special account that allows access to them.  I need to know how in the powershell script to pass these logon credentials through.  Here is the script: 

    (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\print_server\share_name1")
    (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\print_server\share_name2")
    (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\print_server\share_name3")

    Thank you in advance for any help you can give me.

    --HG


    Heather Gavin

    Monday, January 26, 2015 5:42 PM

Answers

  • You have not defined specifically what "works inconsistently" means.

    As jrv pointed out, it doesn't matter whether you use VBScript or PowerShell. You are using the exact same COM object in either case (WshNetwork object).

    If the script works in some circumstances, then you need to work out under exactly which circumstances it works and also exactly under which circumstances it does not work.

    I also recommend removing the line of code On Error Resume Next. Do not use that line of code unless you understand exactly what it does and how it works. It will hide all error messages and you will not know what is going wrong.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 27, 2015 2:25 PM
    Moderator

All replies

  • Not a scripting issue.  Ask your administrator to grant permission on the printer to your account.


    ¯\_(ツ)_/¯

    Monday, January 26, 2015 5:47 PM
  • I have permissions with a domain account.  I need to give the script the account as I am on a non-domain machine.

    Heather Gavin

    Monday, January 26, 2015 6:18 PM
  • Also on the printer share the "Everyone" group has rights to print to the printer, but again the server with the printers is in a domain and the server I need to print from in not in the domain.

    --HG


    Heather Gavin

    Monday, January 26, 2015 6:20 PM
  • You may be able to establish a session with the remote server using the net use command first, then attempt to connect to the printer.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 26, 2015 6:40 PM
    Moderator
  • nwk.AddPrinterConnection(strLocalName, strRemoteName,bUpdateProfile,strUser,strPassword)

    https://msdn.microsoft.com/en-us/library/kxsdca3c(v=vs.84).aspx


    ¯\_(ツ)_/¯

    Monday, January 26, 2015 6:47 PM
  • I have a vbs script that does the same thing but it does not work consistently.  Which is why I want to use PowerShell, not windows scripting.  I am hoping the issues I am having with the vbs script would be solved if I used PowerShell.

    Heather Gavin

    Tuesday, January 27, 2015 12:32 PM
  • It is the exact same code and object in both.

    $nwk=New-Object -ComObject WScript.Network
    $nwk.AddPrinterConnection($strLocalName, $strRemoteName,$true,$strUser,$strPassword) 


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 27, 2015 12:35 PM
    Tuesday, January 27, 2015 12:35 PM
  • Here is the other script that works inconsistently:

    Option Explicit
    On Error Resume Next
    DIM WSHNetwork

    Set wshNetwork = CreateObject("WScript.Network")

    wshNetwork.MapNetworkDrive "", "\\print_serverIP",FALSE,"domain\UserName","password"
    WScript.Sleep(2000)
    WSHNetwork.AddWindowsPrinterConnection "\\print_serverIP\ShareName1"
    WSHNetwork.AddWindowsPrinterConnection "\\print_serverIP\ShareName2"
    wshNetwork.AddWindowsPrinterConnection "\\print_serverIP\ShareName3"
    wshNetwork.AddWindowsPrinterConnection "\\print_serverIP\ShareName4"


    Heather Gavin

    Tuesday, January 27, 2015 12:35 PM
  • You have not defined specifically what "works inconsistently" means.

    As jrv pointed out, it doesn't matter whether you use VBScript or PowerShell. You are using the exact same COM object in either case (WshNetwork object).

    If the script works in some circumstances, then you need to work out under exactly which circumstances it works and also exactly under which circumstances it does not work.

    I also recommend removing the line of code On Error Resume Next. Do not use that line of code unless you understand exactly what it does and how it works. It will hide all error messages and you will not know what is going wrong.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 27, 2015 2:25 PM
    Moderator