Adding a printer connection from an untrusted domain

Home Forums Scripting Windows Script Host Adding a printer connection from an untrusted domain

This topic contains 8 replies, has 4 voices, and was last updated by Avatar robertoD 8 years, 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • Avatar
    Adam_231
    Member
    #155670

    Hi,

    I have an unusual requirement which I simply do not know if its possible to acheive without a manual approach.

    Our company has a sizeable enterprise domain, lets call this Domain1. I manage another domain for our showroom, lets call this Domain2. The two domains are on the same physical network but are untrusted. There is network connectivity between them as you can share files etc so long as you have logon credentials.

    What I hope to be able to do (without having to goto each users laptop and manually add queues) is to create a script which when run from a user on Domain1’s laptop will create printer connections for various print shares on Domain2.

    I have used a VB script with AddPrinterConnection but of course it does not complete because without providing a logon for Domain2 the script cannot complete. As such, is there a way that within a script that I can add the Domain login credentials so the AddPrinterConnection argument will work?

    Or, is there another way to get the same result?

    Thanks in advance!

    Rems
    Rems
    Moderator
    #228030

    Re: Adding a printer connection from an untrusted domain

    maybe something like,

    -[/CODE]
    [CODE]
    Const HIDDEN_WINDOW = 12

    strPrnSrv = “[COLOR=”darkslateblue”]192.168.10.20[/COLOR]”
    strPrinter = “[COLOR=”DarkSlateBlue”]PrinterShareName[/COLOR]”

    strComputer = “.”
    Set objWMIService = GetObject( _
    “winmgmts:{impersonationLevel=Impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ Use alternate credentials for Inter-Process Communication
    Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
    objConfig.ShowWindow = HIDDEN_WINDOW
    errReturn = objWMIService.Get(“win32_process”).Create _
    (“net.exe use \” & strPrnSrv & ” /user:” _
    & “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR] “”[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”” ” _
    & “/persistent:no”, null, objConfig, intProcessID)

    Set objPrinter = objWMIService.Get(“Win32_Printer”)
    errReturn = objPrinter.AddPrinterConnection _
    (“\” & strPrnSrv & “” & strPrinter)

    [/CODE]

    /Rems[CODE]
    -[/CODE]

    Const HIDDEN_WINDOW = 12

    strPrnSrv = “[COLOR=”darkslateblue”]192.168.10.20[/COLOR]”
    strPrinter = “[COLOR=”DarkSlateBlue”]PrinterShareName[/COLOR]”

    strComputer = “.”
    Set objWMIService = GetObject( _
    “winmgmts:{impersonationLevel=Impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ Use alternate credentials for Inter-Process Communication
    Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
    objConfig.ShowWindow = HIDDEN_WINDOW
    errReturn = objWMIService.Get(“win32_process”).Create _
    (“net.exe use \” & strPrnSrv & ” /user:” _
    & “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR] “”[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”” ” _
    & “/persistent:no”, null, objConfig, intProcessID)

    Set objPrinter = objWMIService.Get(“Win32_Printer”)
    errReturn = objPrinter.AddPrinterConnection _
    (“\” & strPrnSrv & “” & strPrinter)

    [/CODE]

    /Rems[CODE]
    Const HIDDEN_WINDOW = 12

    strPrnSrv = “192.168.10.20
    strPrinter = “PrinterShareName

    strComputer = “.”
    Set objWMIService = GetObject( _
    “winmgmts:{impersonationLevel=Impersonate}!\” _
    & strComputer & “rootcimv2”)

    ‘ Use alternate credentials for Inter-Process Communication
    Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
    objConfig.ShowWindow = HIDDEN_WINDOW
    errReturn = objWMIService.Get(“win32_process”).Create _
    (“net.exe use \” & strPrnSrv & ” /user:” _
    & “otherdomainuser “”#[email protected]#“” ” _
    & “/persistent:no”, null, objConfig, intProcessID)

    Set objPrinter = objWMIService.Get(“Win32_Printer”)
    errReturn = objPrinter.AddPrinterConnection _
    (“\” & strPrnSrv & “” & strPrinter)

    [/CODE]

    /Rems

    Avatar
    robertoD
    Member
    #380694

    Re: Adding a printer connection from an untrusted domain

    Thats weird, I tried to reply to this thread but it disappeared….. maybe I’m going mad :S

    Rems, thanks very much this script is great. I wonder if you could help develop it a little futher as I have tried to modify it slightly with no success.

    I would need to be able to add multiple printers from multiple servers, for example I would want the script to be able to add say the following connections;

    \server1printer1
    \server1printer2
    \server2printer3
    \server2printer4
    \server3printer5
    \server4printer6

    Can your script accomodate that?

    Thanks in advance

    Adam

    Rems
    Rems
    Moderator
    #228032

    Re: Adding a printer connection from an untrusted domain

    Adam_231;243282 wrote:
    I would need to be able to add multiple printers from multiple servers, for example I would want the script to be able to add say the following connections;

    \server1printer1
    \server1printer2
    \server2printer3
    \server2printer4
    \server3printer5
    \server4printer6

    Can your script accomodate that?

    Thanks in advance

    Adam

    It would be something like this,
    (in this sample credentials are passed to the subroutine for each server. If you’d use the same credentials for all servers you could also just enter the credentials once in de sub routine)

    Code:
    Const HIDDEN_WINDOW = 12

    strComputer = “.”
    Set objWMIService = GetObject( _
    “winmgmts:{impersonationLevel=Impersonate}!\” _
    & strComputer & “rootcimv2”)

    Set objPrinter = objWMIService.Get(“Win32_Printer”)

    ‘ Use alternate credentials for Inter-Process Communication
    Dim objConfig
    Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
    objConfig.ShowWindow = HIDDEN_WINDOW

    strPrnSrv1 = “[COLOR=”DarkSlateBlue”]192.168.10.20[/COLOR]”
    ConnectTo strPrnSrv1, “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR]”, “[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”

    strPrnSrv2 = “[COLOR=”darkslateblue”]192.168.10.21[/COLOR]”
    ConnectTo strPrnSrv2, “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR]”, “[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”

    strPrnSrv3 = “[COLOR=”darkslateblue”]192.168.10.22[/COLOR]”
    ConnectTo strPrnSrv3, “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR]”, “[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”

    strPrnSrv4 = “[COLOR=”darkslateblue”]192.168.10.23[/COLOR]”
    ConnectTo strPrnSrv4, “[COLOR=”DarkSlateBlue”]otherdomainuser[/COLOR]”, “[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”

    AddPrinter strPrnSrv1, “[COLOR=”DarkSlateBlue”]printer1[/COLOR]”
    AddPrinter strPrnSrv1, “[COLOR=”DarkSlateBlue”]printer2[/COLOR]”

    AddPrinter strPrnSrv2, “[COLOR=”DarkSlateBlue”]printer3[/COLOR]”
    AddPrinter strPrnSrv2, “[COLOR=”DarkSlateBlue”]printer4[/COLOR]”

    AddPrinter strPrnSrv3, “[COLOR=”DarkSlateBlue”]printer5[/COLOR]”

    AddPrinter strPrnSrv4, “[COLOR=”DarkSlateBlue”]printer6[/COLOR]”

    Sub ConnectTo(sHost, sUser, sPass)
    Dim intProcessID, errReturn
    errReturn = objWMIService.Get(“win32_process”).Create _
    (“net.exe use \” & sHost & ” /user:” _
    & sUser & ” “”” & sPass & “”” ” _
    & “/persistent:no”, null, objConfig, intProcessID)
    End Sub

    Sub AddPrinter(sHost, sPrinter)
    Dim errReturn
    errReturn = objPrinter.AddPrinterConnection _
    (“\” & sHost & “” & sPrinter)
    End Sub

    /Rems

    Avatar
    robertoD
    Member
    #380695

    Re: Adding a printer connection from an untrusted domain

    Rems,

    I can’t thank you enough for your help with this. In the case that I use the script you’ve provided below I would create one script per user and distribute for them to use, in other words there would only be one user/password per script.

    Please excuse my ignorance as I am really only ‘trying’ to learn about scripting but I don’t really understand about entering the credentials once in the sub routine.

    Another thought I had is that when I created the user account in the showroom domain I used the same user account name as they use in the enterprise domain. For example, if the user logs onto the enterprise domain as bob.jones in the showroom domain their account is also bob.jones . I wondered if there is some fancy scripting that can be done to pull the username from their currnet windows session so as not to have to write it in the script? As a side note, each users password for the showroom environment is the same, so in other words if the username value can be taken from the windows session then I would in effect only need one version of the script rather than several individual versions.

    Again, thanks very much for all your help

    Adam

    Rems
    Rems
    Moderator
    #228033

    Re: Adding a printer connection from an untrusted domain

    Adam_231;243287 wrote:
    * entering the credentials once in the sub routine.
    * when I created the user account in the showroom domain I used the same user account name as they use in the enterprise domain.

    I wondered if there is some fancy scripting that can be done to pull the username from their currnet windows session so as not to have to write it in the script? As a side note, each users password for the showroom environment is the same, so in other words if the username value can be taken from the windows session then I would in effect only need one version of the script rather than several individual versions.

    Code:
    Const HIDDEN_WINDOW = 12

    DIM objWMIService, objPrinter, strUserName, objConfig

    strComputer = “.”
    Set objWMIService = GetObject( _
    “winmgmts:{impersonationLevel=Impersonate}!\” _
    & strComputer & “rootcimv2”)

    Set objPrinter = objWMIService.Get(“Win32_Printer”)

    Set objNetwork = WScript.CreateObject(“WScript.Network”)
    strUserName = objNetwork.UserName

    ‘ Use alternate credentials for Inter-Process Communication
    Set objConfig = objWMIService.Get(“Win32_ProcessStartup”).SpawnInstance_
    objConfig.ShowWindow = HIDDEN_WINDOW

    strPrnSrv1 = “[COLOR=”DarkSlateBlue”]192.168.10.20[/COLOR]”
    ConnectTo strPrnSrv1

    strPrnSrv2 = “[COLOR=”darkslateblue”]192.168.10.21[/COLOR]”
    ConnectTo strPrnSrv2

    strPrnSrv3 = “[COLOR=”darkslateblue”]192.168.10.22[/COLOR]”
    ConnectTo strPrnSrv3

    strPrnSrv4 = “[COLOR=”darkslateblue”]192.168.10.23[/COLOR]”
    ConnectTo strPrnSrv4

    AddPrinter strPrnSrv1, “[COLOR=”DarkSlateBlue”]printer1[/COLOR]”
    AddPrinter strPrnSrv1, “[COLOR=”DarkSlateBlue”]printer2[/COLOR]”

    AddPrinter strPrnSrv2, “[COLOR=”DarkSlateBlue”]printer3[/COLOR]”
    AddPrinter strPrnSrv2, “[COLOR=”DarkSlateBlue”]printer4[/COLOR]”

    AddPrinter strPrnSrv3, “[COLOR=”DarkSlateBlue”]printer5[/COLOR]”

    AddPrinter strPrnSrv4, “[COLOR=”DarkSlateBlue”]printer6[/COLOR]”

    Sub ConnectTo(sHost)
    Dim intProcessID, errReturn
    errReturn = objWMIService.Get(“win32_process”).Create _
    (“net.exe use \” & sHost & ” /user:[COLOR=”DarkSlateBlue”]otherdomain[/COLOR]” _
    & strUserName & ” “”[COLOR=”darkslateblue”]#[email protected]#[/COLOR]”” ” _
    & “/persistent:no”, null, objConfig, intProcessID)
    End Sub

    Sub AddPrinter(sHost, sPrinter)
    Dim errReturn
    errReturn = objPrinter.AddPrinterConnection _
    (“\” & sHost & “” & sPrinter)
    End Sub

    Enter the actual password and the domain name in the sub routine.

    /Rems

    Avatar
    robertoD
    Member
    #380696

    Re: Adding a printer connection from an untrusted domain

    Way hey!

    I’m so happy with this, honestly thanks so very much!

    Now all I want to do is package it as an .exe to complete, any suggestions on a freeware program for this task?

    Avatar
    robertoD
    Member
    #380697

    Re: Adding a printer connection from an untrusted domain

    Also, how do you leave reputation points?

    Avatar
    Wired
    Moderator
    #274208

    Re: Adding a printer connection from an untrusted domain

    Hit the ying yang symbol above a helpful post to leave reputation points.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.