Announcement

Collapse
No announcement yet.

Mapping network printer using user's credentials

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mapping network printer using user's credentials

    Hi, everyone!!
    I'm looking for solution which allows users connect to the network printer using their AD credentials. Here is my situation. We have AD domain member Print Sever, where all site printers are installed. We also have ~100 client computers in the same network. When users are logged into computers with their AD credentials they can easily connect to the network printers using Add Printer Wizard or from the Run menu (\\ServerName\PrinterName). However sometime AD members computer need to run some vendor software which require to run under local administrative account. And time to time our users need to print some documents from computer which running this particular vendor software. So here is the problem. Local admin account can't connect to printers shared in AD. Window provides ability connect to network drives using different user credential, however there is no such as option when you connect to the network printer... Network printer always mapping for currently logged in user... And in my case printer will be mapped with "Access Deny" status. Of course it is possible connect to the printer from the RUN menu ((\\ServerName\PrinterName). In this case username/password prompt will popup and user can connect to the printer after typing his username and password. But I'm looking for some more intelligent way for printer connection...
    I have some "*.hta" script with show username/password prompt and allow users connect to Network drives using their credentials:


    Code:
     
    <HTML>
    <HEAD>
    <TITLE>Network Drive Connection</title>
    <HTA:APPLICATION
    ApplicationName="Map_ND.HTA"
    SingleInstance="Yes"
    WindowsState="Normal"
    Scroll="No"
    Navigable="Yes"
    MaximizeButton="No"
    SysMenu="Yes"
    Caption="Yes"
    ></HEAD>
    <SCRIPT LANGUAGE="VBScript">
    Sub Window_Onload
      '# Size Window
      sHorizontal = 440
      sVertical = 175
      Window.resizeTo sHorizontal, sVertical
      '# Get Monitor Details
      Set objWMIService = GetObject _
        ("winmgmts:root\cimv2")
      intHorizontal = sHorizontal *2
      intVertical = sVertical *2
      Set colItems = objWMIService.ExecQuery( _
        "Select ScreenWidth, ScreenHeight from" _
        & " Win32_DesktopMonitor", , 48)
      For Each objItem In colItems
        sWidth= objItem.ScreenWidth
        sHeight = objItem.ScreenHeight
        If sWidth > sHorizontal _
          then intHorizontal = sWidth
        If sHeight > sVertical _
          then intVertical = sHeight
      Next
      Set objWMIService = Nothing
      '# Center window on the screen
      intLeft = (intHorizontal - sHorizontal) /2
      intTop = (intVertical - sVertical) /2
      Window.moveTo intLeft, intTop
      '# default window content
      window.location.href="#Top"
    End Sub
     
    Sub RunScript
    on Error Resume Next
     
    ' ------------Script Configuration---------------------------------
     
    mDrive = "P:"
    strRemotePath = "\\ServerName\ShareName"
    strDriveAlias = "My Share"
     
    minUSRnamelength = 3
    minPASSwrdlength = 7
     
    strUsr = UsrnameArea.Value
    strPas = PasswordArea.Value
    Set objNetwork = CreateObject("WScript.Network")
    Err.Clear : objNetwork.RemoveNetworkDrive "P:", True, True
    If Err.Number = 0 Then Msgbox "Station P: succesfully disconnected"
    If Len(strUsr) >= minUSRnamelength then
    If Len(strPas) >= minPASSwrdlength then
    Err.Clear
    objNetwork.MapNetworkDrive mDrive, strRemotePath, FALSE, strUsr, strPas
    If Err.Number = 0 Then
    oShell.NameSpace(mDrive).Self.Name = strDriveAlias
    Msgbox chr(34) & strUsr & """ My Share is connected now!"
    End If
    ELSE 
    Msgbox "You typed an incorrect password !"
    Exit Sub
    End If
    ELSE 
    Msgbox chr(34) & strUsr & """ is an incorrect Username !"
    Exit Sub
    End If
    Set objNetwork = Nothing
    Self.Close()
    End Sub
     
    Sub CancelScript
    Set oShell = Nothing
    Set objNetwork = Nothing
    Self.Close()
    End Sub
     
    '---------End of Script Configuration------------------
     
    </SCRIPT>
     
    <BODY STYLE="font:14 pt arial; color:white; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000000', EndColorStr='#0000FF')">
    <a name="Top"></a><CENTER>
    <table border="0" cellpadding="0" cellspacing="0"><font size="2" color="black" face="Arial">
    <tr>
    <td height="30">
    <p align="right">Your Username</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="text" name="UsrnameArea" size="30"></td></tr>
    <tr>
    <td height="30">
    <p align="right">Company Password</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="password" name="PasswordArea" size="30"></td></tr>
    </table><BR>
    <HR color="#0000FF">
    <Input id=runbutton class="button" type="button" value=" Create Drive " name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp; 
    <Input id=runbutton class="button" type="button" value="Cancel" name="cancel_button" onClick="CancelScript">
    </CENTER>
    </BODY>
    </HTML>
    This script working pretty good for me and I've tried to create something similar for mapping network printers:

    Code:
     
    <HTML>
    <HEAD>
    <TITLE>Mapping Network Printer</title>
    <HTA:APPLICATION
    ApplicationName="Connect to Network Printer.HTA"
    SingleInstance="Yes"
    WindowsState="Normal"
    Scroll="No"
    Navigable="Yes"
    MaximizeButton="No"
    SysMenu="Yes"
    Caption="Yes"
    ></HEAD>
    <SCRIPT LANGUAGE="VBScript">
    Sub Window_Onload
      '# Size Window
      sHorizontal = 440
      sVertical = 175
      Window.resizeTo sHorizontal, sVertical
      '# Get Monitor Details
      Set objWMIService = GetObject _
        ("winmgmts:root\cimv2")
      intHorizontal = sHorizontal *2
      intVertical = sVertical *2
      Set colItems = objWMIService.ExecQuery( _
        "Select ScreenWidth, ScreenHeight from" _
        & " Win32_DesktopMonitor", , 48)
      For Each objItem In colItems
        sWidth= objItem.ScreenWidth
        sHeight = objItem.ScreenHeight
        If sWidth > sHorizontal _
          then intHorizontal = sWidth
        If sHeight > sVertical _
          then intVertical = sHeight
      Next
      Set objWMIService = Nothing
      '# Center window on the screen
      intLeft = (intHorizontal - sHorizontal) /2
      intTop = (intVertical - sVertical) /2
      Window.moveTo intLeft, intTop
      '# default window content
      window.location.href="#Top"
    End Sub
     
    Sub RunScript
    on Error Resume Next
     
    ' ------------Script Configuration---------------------------------
     
    strPrinterName = "PrinterShareName" 
    strPrintServer = "ServerName"
    strPrinter = "\\" & strPrintServer & "\" & strPrinterName 
     
    minUSRnamelength = 3
    minPASSwrdlength = 7
    strUsr = UsrnameArea.Value
    strPas = PasswordArea.Value
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    If Len(strUsr) >= minUSRnamelength then
    If Len(strPas) >= minPASSwrdlength then
    Err.Clear
    WshNetwork.AddWindowsPrinterConnection strPrinter, False, strUsr, strPas
    If Err.Number = 0 Then
    Msgbox chr(34) & strUsr & """ Network Printer is connected now!"
    End If
    ELSE 
    Msgbox "You typed an incorrect password !"
    Exit Sub
    End If
    ELSE 
    Msgbox chr(34) & strUsr & """ is an incorrect Username !"
    Exit Sub
    End If
    Set objNetwork = Nothing
    Self.Close()
    End Sub
     
    Sub CancelScript
    Set oShell = Nothing
    Set objNetwork = Nothing
    Self.Close()
    End Sub
     
    '---------End of Script Configuration------------------
     
    </SCRIPT>
     
    <BODY STYLE="font:14 pt arial; color:white; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr='#000000', EndColorStr='#0000FF')">
    <a name="Top"></a><CENTER>
    <table border="0" cellpadding="0" cellspacing="0"><font size="2" color="black" face="Arial">
    <tr>
    <td height="30">
    <p align="right">Your Username</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="text" name="UsrnameArea" size="30"></td></tr>
    <tr>
    <td height="30">
    <p align="right">Company Password</p>
    </td>
    <td height="30">&nbsp;&nbsp; <input type="password" name="PasswordArea" size="30"></td></tr>
    </table><BR>
    <HR color="#0000FF">
    <Input id=runbutton class="button" type="button" value=" Connect Printer " name="run_button" onClick="RunScript">
    &nbsp;&nbsp;&nbsp; 
    <Input id=runbutton class="button" type="button" value="Cancel" name="cancel_button" onClick="CancelScript">
    </CENTER>
    </BODY>
    </HTML>
    I've just replaced MapNetworkDrive value with AddWindowsPrinterConnection but it not works... I've tried also different methods objNetwork and WshNetwork but no success... Any help will be much appreciated!
Working...
X