+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Adding a printer by username in Coding and Web Development; I have looked at a few posts already on here about adding a printer script which checks the computer name ...
  1. #1
    tomscaper's Avatar
    Join Date
    Jul 2006
    Posts
    814
    Thank Post
    118
    Thanked 29 Times in 15 Posts
    Rep Power
    22

    Adding a printer by username

    I have looked at a few posts already on here about adding a printer script which checks the computer name and adds the printer if it is in a particular group. For example

    Option explicit
    dim objNetwork, objComputer, ObjPrinter
    dim strDomain, strComputerName, MyVar
    Set objNetwork = WScript.CreateObject("Wscript.Network")
    strDomain = objNetwork.UserDomain
    strComputerName = objNetwork.ComputerName
    Set objComputer = GetObject("WinNT://" & strDomain & "/" & strComputerName & ",computer")
    Set objPrinter = CreateObject("WScript.Network")

    If isComputerMember("groupname") then
    ObjPrinter.AddWindowsPrinterConnection "\\server\printer"
    end If

    Function IsComputerMember(sGroup)
    Dim oGroup
    on error resume next
    Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group")
    IsComputerMember = CBool(oGroup.IsMember(objComputer.ADsPath & "$"))
    Set oGroup = Nothing
    If not Err.Number = 0 Then
    'isComputerMember could not locate group
    end if
    on error goto 0
    End Function
    I am looking to add a printer by the username and the groups they are members of but i am not fully sure how i gould go about doign that. Has anyone already done this.

  2. #2

    Join Date
    Jan 2009
    Location
    England
    Posts
    1,494
    Thank Post
    300
    Thanked 304 Times in 263 Posts
    Rep Power
    82
    I'm using GPP's to do the same thing. Since rolling them out at the beginning of September it's been great. We're able to target a printer based on a users group membership, their OU, the OU of the computer and a million other different options.

    If you're running windows server 2008 or have a vista client you can set the policies up on then your good to go with GPPs (http://www.gpoguy.com/Portals/0/Grou...20Overview.pdf). For XP + 2003 clients you just need to install the client side extensions so they recognise the GPP's and it should all work

  3. #3
    tomscaper's Avatar
    Join Date
    Jul 2006
    Posts
    814
    Thank Post
    118
    Thanked 29 Times in 15 Posts
    Rep Power
    22
    I have never user GPP's, how would i install this either on the server and client, where woudl i get this for. I am using XP and 2003

  4. #4

    Join Date
    Jan 2009
    Location
    England
    Posts
    1,494
    Thank Post
    300
    Thanked 304 Times in 263 Posts
    Rep Power
    82
    Client install is just a microsoft hotfix that needs to be deployed on XP/2003. Unfortunately to manage it you need a vista/2008 machine. Any older then this and the GPMC doesn't have the options to edit the Group Policy Preferences.

    Have a read of the link above for more info

  5. #5
    tomscaper's Avatar
    Join Date
    Jul 2006
    Posts
    814
    Thank Post
    118
    Thanked 29 Times in 15 Posts
    Rep Power
    22
    I dont have a vista or 2008 machine to do this one. There must be a way to check if a user is in a certain group. as the scritp above just checks if a machine is in a particular group

  6. #6
    tomscaper's Avatar
    Join Date
    Jul 2006
    Posts
    814
    Thank Post
    118
    Thanked 29 Times in 15 Posts
    Rep Power
    22
    Found this if anyone is interested.

    Option explicit
    dim objNetwork, objUser , ObjPrinter
    dim strDomain, strUserName

    Set objNetwork = WScript.CreateObject("Wscript.Network")
    strDomain = objNetwork.UserDomain
    strUserName = objNetwork.UserName
    Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserName & ",user")
    Set objPrinter = CreateObject("WScript.Network")

    If IsMember(group) then
    ObjPrinter.AddWindowsPrinterConnection "\\server\printer"
    End If

    Function IsMember(sGroup)
    Dim oDict, oUser, oGroup
    If IsEmpty(oDict) Then
    Set oDict = CreateObject("Scripting.Dictionary")
    oDict.CompareMode = vbTextCompare
    For Each oGroup In objUser.Groups
    oDict.Add oGroup.Name, "-"
    Next
    Set oUser = Nothing
    End If
    IsMember = CBool(oDict.Exists(sGroup))
    End Function

  7. #7

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    115
    Thanked 528 Times in 451 Posts
    Blog Entries
    2
    Rep Power
    124
    Looks good.

    Just a quick point - if you have nested groups then this won't work (eg if you want to map a printer for everyone in year7 but the group year7 is actually made up from groups year7a, year7b, year7c etc)

    If this is unimportant to you then ignore me completely (most people do :-)) but for anyone else, you could either do things like:

    If IsMember("year7a") or ismember("year7b") or ismember("year7c") then

    or you can get into nasty recursive stuff where you take the top level group, enumerate its members and for any groups you find, enumerate them as well. You then have a list of every user in your group and you see if your user is there.

  8. #8
    tomscaper's Avatar
    Join Date
    Jul 2006
    Posts
    814
    Thank Post
    118
    Thanked 29 Times in 15 Posts
    Rep Power
    22
    Quote Originally Posted by srochford View Post
    Looks good.

    Just a quick point - if you have nested groups then this won't work (eg if you want to map a printer for everyone in year7 but the group year7 is actually made up from groups year7a, year7b, year7c etc)

    If this is unimportant to you then ignore me completely (most people do :-)) but for anyone else, you could either do things like:

    If IsMember("year7a") or ismember("year7b") or ismember("year7c") then

    or you can get into nasty recursive stuff where you take the top level group, enumerate its members and for any groups you find, enumerate them as well. You then have a list of every user in your group and you see if your user is there.
    I am not really sure how it works i just found it on the net and pulled it together till it worked for me. Do you know of a better way to do this. one which would incorporate nested groups.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 4
    Last Post: 30th December 2009, 09:57 AM
  2. adding windows 2000+ username
    By Domino in forum Wireless Networks
    Replies: 13
    Last Post: 6th March 2009, 02:50 PM
  3. Script not deleting/adding printer correctly
    By dave.81 in forum Scripts
    Replies: 2
    Last Post: 5th June 2008, 01:17 PM
  4. Adding a printer
    By Neville in forum Windows
    Replies: 0
    Last Post: 15th May 2008, 11:09 AM
  5. Adding x32 printer drivers to x64 server
    By Dos_Box in forum Windows
    Replies: 7
    Last Post: 2nd May 2008, 10:35 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •