Announcement

Collapse
No announcement yet.

VBScript Check - Help needed, network printers

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

  • VBScript Check - Help needed, network printers

    Thanks to these forums and the work done at the scripting library at microsoft.com/technet/ and www.tek-tips.com and computerperformance.co.uk I was able to do a number of things with scripts for our school this summer. This is the first year I've done scripting, and am a complete newb at it.

    I want to now install network printers with a script based on group membership of computers. I could do this via OU too. Since I'm dealing with teachers, I rename items, so that it doesn't confuse them too much.

    A note about how we're set up. Student accounts cannot install printers. I want to install default printers on a computer so that every user who sits down will have access to it as the default.

    Code:
    'Draft Login Script
    'September 10, 2008
    'CassiusAugusta
    'Code taken from http://forums.petri.com/ , http://www.tek-tips.com/faqs.cfm?fid=5798 and computerperformance.co.uk
    'Thanks Brian873 and markdmac
    '
    '==========================================================================
    '
    ' NAME: LogonScript.vbs
    '
    ' AUTHOR:  Mark D. MacLachlan, The Spider's Parlor
    ' URL   : http://www.thespidersparlor.com
    ' DATE  : 4/10/2003
    '
    ' COMMENT: Enumerates current users' group memberships in given domain.
    '
    '    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    '    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    '    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    '    PARTICULAR PURPOSE.
    '
    '    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
    '    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
    '    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
    '    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
    '    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
    '    OF THIS CODE OR INFORMATION.
    '
    '==========================================================================
    ON ERROR RESUME NEXT
    
    Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path
    
    
    Set WSHShell = CreateObject("WScript.Shell")
    Set WSHNetwork = CreateObject("WScript.Network")
    'Automatically grab the user's domain name
    DomainString = Wshnetwork.UserDomain
    'Find the Windows Directory
    WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
    
    'Grab the user name
    UserString = WSHNetwork.UserName
    'Bind to the user object to get user name and check for group memberships later
    Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
    
    'Grab the computer name for use in add-on code later
    strComputer = WSHNetwork.ComputerName
    
    Set ComputerObj = GetObject("WinNT://" & DomainString & "/" & strComputer & ",computer")
    
    For Each GroupObj In ComputerObj.Groups
        Select Case GroupObj.Name
        'Check for group memberships and take needed action
        Case "HSCorA"
            WSHNetwork.SetDefaultPrinter "\\SERVER\B&W - HS Lab 107"
                label = "B&W - HS Lab 107"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
                   WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Color - HS Lab 107"
                label = "Color - HS Lab 107"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
            Case "HSCorE"
            WSHNetwork.SetDefaultPrinter "\\SERVER\B&W - HS Lab 112"
                label = "B&W - HS Lab 112"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
                   WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Color - HS Lab 112"
                label = "Color - HS Lab 112"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
            Case "HSCorF"
            WSHNetwork.SetDefaultPrinter "\\SERVER\B&W - HS Lab 124"
                label = "B&W - HS Lab 124"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
                   WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Color - HS Lab 1124"
                label = "Color - HS Lab 124"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
    '        Case "HSCorH"
    '        WSHNetwork.SetDefaultPrinter "\\SERVER\B&W - HS Lab 107"
    '            label = "B&W - HS Lab 107"
    '        
    '            Set shellApp = CreateObject("Shell.Application")
    '            shellApp.NameSpace.Self.Name = label
    '              WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Color - HS Lab 107"
    '            label = "Color - HS Lab 107"
    '        
    '            Set shellApp = CreateObject("Shell.Application")
    '            shellApp.NameSpace.Self.Name = label
            Case "HSCorI"
            WSHNetwork.SetDefaultPrinter "\\SERVER\HS Lounge"        
                label = "HS Lounge"
            
                Set shellApp = CreateObject("Shell.Application")
                shellApp.NameSpace.Self.Name = label
        End Select
    Next
    '=====================================
    Dim tempiepath
    tempiepath = "HKCU\Software\Microsoft\Windows\"
    
    WSHShell.RegWrite tempiepath & "ShellNoRoam\MUICache\@inetcplc.dll,-4750","Empty Temporary Internet Files folder when browser is closed","REG_SZ"
    
    WSHShell.RegWrite tempiepath & "CurrentVersion\Internet Settings\Cache\Persistent","0","REG_DWORD"
    Set tempiepath = nothing
    
    'Clean Up Memory We Used
    set UserObj = Nothing
    set GroupObj = Nothing
    set WSHNetwork = Nothing
    set DomainString = Nothing
    set WSHSHell = Nothing
    Set WSHPrinters = Nothing
      
    'Quit the Script
    wscript.quit
    Currently, it's not working. I'm not getting any errors, just the script doesn't seem to actually install printers. I've had success using a similar script to install our network copier, but that was based on USER group membership, not computer group membership.

    Any help would be greatly appreciated!
    Last edited by CassiusAugusta; 10th September 2008, 17:40.

  • #2
    Re: VBScript Check - Help needed, network printers

    you won't add printers with this line. It's only setting the default printer.
    Code:
    WSHNetwork.SetDefaultPrinter "\\SERVER\B&W - HS Lab 124"
    You can add the line above the setdefaultprinter.
    Code:
    WSHNetwork.AddWindowsPrinterConnection  "\\SERVER\B&W - HS Lab 124"
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: VBScript Check - Help needed, network printers

      Originally posted by CassiusAugusta View Post
      I want to now install network printers with a script based on group membership of computers
      You like to, set an already installed printer as the default for the currently logged-on user, based on the computer's membership of a group. If the printer is not installed yet, use the extra code from Dumber's post to install the printer first.

      The .Groups Method (e.g. Set colGroups = objUser.Groups), returns the Groups collection for the user. Groups Method does not work for computer objects.

      For your script you can use one of the excellent samples by MVP Richard L. Mueller.
      - Functions to test for group membership.
      e.g. you can use the function (#5) just as it is like this:
      Code:
      DIM objGroupList
      
      Set objSysInfo = CreateObject("ADSystemInfo")
      strComputerDN = objSysInfo.computername
      Set objComputer = GetObject("LDAP://" & strComputerDN)
      
      Select Case TRUE
          Case IsMember(objComputer, "Domain Computers")
                wsh.echo "matching groupname for the computer"
      End Select
      
      'functions & subroutines here
      I edited one of the samples (IsMember Function # 5) a little bit,
      Code:
      'Option Explicit
      
      With CreateObject("ADSystemInfo")
        Dim objrootDSE, objUser, objComputer
        Set objrootDSE = GetObject("LDAP://" & .DomainDNSName & "/rootDSE")
        Set objUser = GetObject("LDAP://" & .UserName)
            objUser.GetInfoEx Array("sAMAccountName","tokenGroups"), 0
        Set objComputer = GetObject("LDAP://" & .Computername)
            objComputer.GetInfoEx Array("sAMAccountName","tokenGroups"), 0
      End With
      
      'Global Scope,
      DIM objGroupList, strGroup, strUser, strComputer
      strUser = objUser.Get("sAMAccountName")
      strComputer = objComputer.Get("sAMAccountName")
      
      
      'takes about 3-4 seconds to populate the groupslist for the two ADObject
      AppendToDictionary strUser, objUser.Get("tokenGroups")
      AppendToDictionary strComputer, objComputer.Get("tokenGroups")
      
      
      Select Case TRUE
      
        Case IsMember(strUser, "fakegroup1")
      
             wscript.echo strGroup
      
        Case IsMember(strUser, "fakegroup2") OR IsMember(strUser, "DoMAIn users")
      
             wscript.echo strGroup
      
      End Select
      
      Select Case TRUE
      
          Case IsMember(strComputer, "fakegroup")
      
             wscript.echo strGroup
      
          Case IsMember(strComputer, "doMAIn computers")
      
             wscript.echo strGroup
      
      End Select
      
      wscript.quit
      
      
      Function IsMember (ByVal sAMAccName, ByVal Groupname)
          ' Adapted Function #5 from http://www.rlmueller.net/freecode1.htm
          ' The function returns True if the user or computer is a member of the group.
          ' strGroup is the NT name (sAMAccountName) of the group to test.
          ' strGroup and objGroupList have global scope.
          strGroup = Groupname
          IsMember = objGroupList.Exists (sAMAccName & "\" & strGroup)
      End Function
      
      Sub AppendToDictionary (ByVal sAMAccName, ByVal arrbytGroups)
          ' Adapted from http://www.rlmueller.net/freecode1.htm
          ' Subroutine to populate dictionary object with group memberships.
          ' An efficient way to test group membership for multiple
          ' objects (users/computers) using the LDAP provider and the "TokenGroups"
          ' attribute. The routine also reveals membership in nested groups and
          ' the primary group. Will not reveal cross-domain group membership.
          ' objGroupList is the dictionary object, with global scope.
          If (IsEmpty(objGroupList) = True) Then
              Set objGroupList = CreateObject("Scripting.Dictionary")
              objGroupList.CompareMode = vbTextCompare
          ElseIf (objGroupList.Exists(sAMAccName & "\") = True) Then
                exit Sub
          End If
          objGroupList.Add sAMAccName & "\", True
          Dim arrstrGroupSids(), objGroup, j
          If (TypeName(arrbytGroups) = "Byte()") Then
              ReDim arrstrGroupSids(0)
              arrstrGroupSids(0) = OctetToHexStr(arrbytGroups)
              Set objGroup = GetObject("LDAP://" _
                  & "<SID=" & arrstrGroupSids(0) & ">")
              objGroupList.Add sAMAccName & "\" _
                  & objGroup.sAMAccountName, True
              Set objGroup = Nothing
              Exit Sub
          ElseIf (UBound(arrbytGroups) = -1) Then
              Exit Sub
          End If
          ReDim arrstrGroupSids(UBound(arrbytGroups))
          For j = 0 To UBound(arrbytGroups)
              arrstrGroupSids(j) = OctetToHexStr(arrbytGroups(j))
              Set objGroup = GetObject("LDAP://" _
                  & "<SID=" & arrstrGroupSids(j) & ">")
              objGroupList.Add sAMAccName & "\" _
                  & objGroup.sAMAccountName, True
          Next
          Set objGroup = Nothing
       '------------------------------------------------------------
       ' The advantage of passing an argument "ByVal" is that
       ' it protects a variable from being changed by the procedure.
       ' The advantage of passing an argument "ByRef" is that the
       ' procedure can return a value to the calling code through
       ' that argument.
       ' Although the passing mechanism can also affect the
       ' performance of your code, the difference is usually
       ' insignificant. One exception to this is a value type
       ' passed ByVal. In this case, Visual Basic copies the entire
       ' data contents of the argument. Therefore, for a large value
       ' type such as a structure, it is more efficient to
       ' pass it ByRef.
       '------------------------------------------------------------
      End Sub
      
      Function OctetToHexStr (ByVal arrbytOctet)
          ' http://www.rlmueller.net/freecode1.htm
          ' Function to convert OctetString (byte array) to Hex string.
          Dim k
          OctetToHexStr = ""
          For k = 1 To Lenb(arrbytOctet)
              OctetToHexStr = OctetToHexStr _
                  & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
          Next
      End Function
      There is not much difference between the two, the latter is running just slightly faster on my computer (if you would query both the objuser and objComputer with both scriptsamples). Querying the groups for just computerobjects runs fast, querying the groups for a userobject takes a few seconds.
      The groups found for all the queried userobjects and computerobjects, are loaded into the same dictionary. The script see to it that filling the dictionary will only procesess once for each account. After that you can search the list for as many groupnames you like to check on this account, this runs very fast.


      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: VBScript Check - Help needed, network printers

        Thanks for the quick replies. I'll take the suggestions and get working on the script today. Since I have no programming background, it's a step at a time.

        Comment

        Working...
        X