[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

AD VBasic Login Script (Long)

Okay, here's the whole script.  I'm trying to map drives and printers based on AD site or membership in groups and OU's.  I think this suffers from bloat.  

I get the printers removed just fine.
The existing network drives enter the state of being disconnected, but do not disappear.  Opening one in this state immediately reconnects it.
I frequently (but not always) get an error 80070057, The parameter is incorrect on line 33
                       oNet.RemoveNetworkDrive oDrives.Item(i)
       This is usually fixed ( for a while) by changing the 0 from the line above to a 1 or 2, saving, then changing it back.


I get the 'common drives', and EVERYONE gets the privalaged drives. but no new printer mapping based on site occurs

Thanks for your help



' the ucase command returns the input in all caps

' +++++++++++++++++++++++++++++++++THIS IS THE BASE PRINTER AND DRIVE MAPPING SCRIPT ++++++++++++++++++++++++++++++++++++++++++++++
' +++purpose is to remove any existing drive and printer mappings, then create new/apprpirate ones based on AD group membership.+++




'start of script
    Option Explicit
    'On Error Resume Next





'++++++++++++++++++++++++++++++because Symantec s***s, we must sleep++++++++++++++++++++++++
    'WScript.Sleep 3000 'miliseconds




'++++++++++++++++++++++++NEXT remove existing mappings+++++++++++++++++++++++++++++++
   Dim oNet, oDrives, nDrive, i

    Set oNet = CreateObject("Wscript.Network")

    Set oDrives = oNet.EnumNetworkDrives

    For i = 0 To oDrives.Count-1 Step 2
        oNet.RemoveNetworkDrive oDrives.Item(i)
    WScript.echo i
    Next
' +++++++++++++++++++++++ "end drive removal"++++++++++++++++++++++++++++++++++++++++






'+++++++++++++++++++++BULK PRINTER REMOVAL BEGIN++++++++++++++++++++++
 WScript.echo  "Begin by removing all mapped printers To unconfuse users"

   Dim WSHNet, wshNetwork
   Dim clprinters, n
   Set WSHNet= WScript.CreateObject("Wscript.Network")
   Set clPrinters = WshNet.EnumPrinterConnections
   On Error Resume Next
   For n = 0 To clPrinters.Count - 1 Step 2
     WSHNet.RemovePrinterConnection clPrinters.Item(n+1), True
   Next
'++++++++++++++++END BULK PRINTER REMOVAL++++++++++++++++++++





'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' ++++++++++++Okay, now that we have had our dose of fiber and are all clean, let's start adding objects+++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




'+++++++++++++Some of my drives get mapped no matter who you are++++++++++++++++++++++
                    Dim objNetwork
                    Set objNetwork = CreateObject("WScript.Network")

              WScript.echo "common drives"
              objNetwork.MapNetworkDrive "S:", "\\R105002\shared"
              objNetwork.MapNetworkDrive "I:", "\\R105002\intercept"



'++++++++++++Only domain admins get the N Drive****************************************
           
Dim strGroup
' Dim currentuser
' Dim objectUser
' Dim net
' Dim objprinter
WScript.echo "privalaged drives"
 ' here we creat pegs for the AD groups
 Const DomAd     = "Domain Admins"
 
 

 WScript.echo "Now we see which group the member belongs To"
 Set objectUser = CreateObject("ADSystemInfo")
WScript.echo "next line"
Set currentuser = GetObject("LDAP://" & objectUser.UserName)
WScript.echo "active directoroy group based drive mapping"
WScript.echo  objuser & currentuser
 If UBound(currentuser.MemberOf)>0 Then
 strGroup = LCase(Join(currentuser.MemberOf))
 Else strGroup=currentuser.MemberOf(0)
 WScript.echo strGroup
 End If






' +++++++++++++++++++BEGIN DRIVE MAPPING PER GROUP MEMBERSHIP+++++++++++++++++++

  If InStr(strGroup, DomAd) Then
'      WScript.Echo "You are logged into the IT Group "
    WScript.echo strGroup

          objNetwork.MapNetworkDrive "N:", "\\R105002\install"
          objNetwork.MapNetworkDrive "H:", "\\R105002\home"
          objNetwork.MapNetworkDrive "S:", "\\R052002\home"
          objNetwork.MapNetworkDrive "P:", "\\R227002\home"
          objNetwork.MapNetworkDrive "M:", "\\R221002\home"

  End If      








'+++++++++++++++++++++++++Some Drives are on site server, so they are mapped by AD site+++++++++++++++++++++++++++++++
   
Dim objSYSInfo, objNetW
Set objSysInfo = WScript.CreateObject("ADSystemInfo")
    'a good example of how ADSystemInfo works is
    ' Set objAD = CreateObject("ADSystemInfo")  
    ' Wscript.Echo "Your Computer Name is " & objAD.ComputerName  
    ' WScript.Echo "Your Username is " & objAD.UserName

Set objNetW = WScript.CreateObject("WScript.Network")

Dim objADSUser
'holds our user's name
Set objADSUser = GetObject("LDAP://" & objSYSInfo.username)
' This simply forces LDAP to perform as the dirctory lookup protocol

Dim objADSOU
Set objADSOU = GetObject(objADSUser.parent)
WScript.echo "here we set the site" & objADUser.parent
 ' hmm so the ADSysteminfo.username.parent method returns the OU of the user?

'Dim objWinntUser
'Set objWinntUser = GetObject("WinNT://" & objNetW.UserDomain & "/" & objNetW.UserName & ",user")
'This forces WinNT/NeRIOS to act as the directory

'******* end of objects declarations

Dim strOU, StrGrP
'strOU = "_davidstestou"
'StrGrP = "Domain Admins"
' This is just feeding in known false data to prove the comparison below

'uncomment for testing
'MsgBox objNetW.username & " is directly inside the " & strOU & " OU = " & IsDirectMemberOfOU(strOU)
'MsgBox objNetW.username & " is directly or indirectly inside the " & strOU & " OU = " & IsIndirectMemberOfOU(strOU)
'MsgBox objNetW.username & " is a member of the " & StrGrP & " group = " & IsMemberOfGroup(StrGrP)
'IsDirectMemberOfOU is just a bucket that hold a true or false value, see the 1st function.

MsgBox "note the difference in paths for the different protocols:" & vbCrLf & _
    "User ADSPath(LDAP) = " & objADSUser.adspath
    ' "User ADSPath(Winnt) = " & objWinntUser.adspath & vbCrLf & _
     


'***** End of main script, All functions and subs below *****

'Function IsDirectMemberOfOU(OUName) 'the user is contained directly within the OU
'IsDirectMemberOfOU = False

'If UCase(objADSOU.OU) = UCase(OUName) Then
'     IsDirectMemberOfOU = True
'End If

'End Function

'Function IsIndirectMemberOfOU(OUName) 'the user is somewher under the OU specified
'IsIndirectMemberOfOU = False

'If InStr(UCase(objADSOU.adspath),UCase(OUName)) Then
'     IsIndirectMemberOfOU = True
'End If

'End Function

'Function IsMemberOfGroup(GroupName) 'the user is a member of a specified group
'IsMemberOfGroup = False

'Dim objGroup
'Set objGroup = GetObject("WinNT://" & objNetW.UserDomain & "/" & GroupName & ",group")

'IsMemberOfGroup = objGroup.IsMember(objWinntUser.ADsPath)

'End Function
WScript.exho objADSOU.OU

If UCase(objADSOU.OU) = UCase("OU_MtP") Then objNetW.MapNetworkDrive "z:", "\\R105002\home\" & objADSUser

If UCase(objADSOU.OU) = UCase("OU_Operations") then objNetW.MapNetworkDrive "z:", "\\R105002\home\" & objADSUser

If UCase(objADSOU.OU) = UCase("OU_Pwest") Then objNetW.MapNetworkDrive "z:", "\\R105002\home\" & objADSUser

If UCase(objADSOU.OU) = UCase("OU_serT") Then objNetW.MapNetworkDrive "z:", "\\R052002\home\" & objADSUser

If UCase(objADSOU.OU) = UCase("OU_MBs") Then objNetW.MapNetworkDrive "z:", "\\R221002\home\" & objADSUser



'uncomment after move
'If IsIndirectMemberOfOU("OU_Pwest") Then objNetW.MapNetworkDrive "z:", "\\R227002\home" & objADSUser


0
whoam
Asked:
whoam
  • 11
  • 7
1 Solution
 
whoamAuthor Commented:
forgot else if & end if
0
 
whoamAuthor Commented:
Okay, I need ot use Else if and put the pharse after then on anohter line.  
I didn't quite get how to define wheather the user was in the immediate OU or in a child OU.
Thanks
0
 
ddepastinoCommented:
I remember "scraps" scripts.......

Okay a few things of note:

Keep your scripts consistent in structure:

Declarations
Main Script
Function/Subs

A note on Subs and Functions.  Subs just "do something", Functions "return as something".  You can set a function to return true/false, a string or even an object.  You make subs and functions for repetative tasks so that you don't have to type the same code over and over.  They are also handy in that you can name them based on what they do, this makes a script easier to read.

The RemoveNetworkPrinters sub will get errors when it tries to remove non-network printers (it CAN'T remove them and gets an error).  This is normal and you can expect the script to fail if you comment out the "on error resume next".  When troubleshooting it may be easier to just comment out the sections you know you have working if you comment out error suppresion.  Instead of commenting out an entire sub or function just comment out the call to it:

'RemoveNetworkDrives
'RemoveNetworkPrinters

You have the S: being mapped to 2 different places for a Domain Admin, only the first one will be mapped.
Test for Domain Admin and assign the drive if the test returns FALSE

Change:
WSHNet.MapNetworkDrive "I:", "\\R105002\intercept"
WSHNet.MapNetworkDrive "S:", "\\R105002\shared"

To:
WSHNet.MapNetworkDrive "I:", "\\R105002\intercept"
If UserIsMemberOfGroup("Domain Admins") = FALSE then
   WSHNet.MapNetworkDrive "S:", "\\R105002\shared"
End If

I don't think you need to remove any network drives if this is a logon script.  The way you are adding drives is not static, they would disappear as soon as the user logs off.

Look up some of these on Google, it will give you a good idea of what you are really working with and some of the standard properties/methods associated.  The Microsoft documentation on these isn't too bad.  Try having the script echo a few of the properties you find so that you can get used to what you have to work with.

iadsUser
iadsOU
IADsADSystemInfo
WshNetwork Object
WshShell Object

Here's a trimmed down version of what I think you were trying to accomplish:

Option Explicit
On Error Resume Next
'WScript.Sleep 3000 'miliseconds

Dim EchoStuff
'Make this False to stop all messages...
EchoStuff = True

Dim WSHNet
Set WSHNet = CreateObject("Wscript.Network")

Dim SYS
Set SYS = CreateObject("ADSystemInfo")

Dim ADSUser
Set ADSUser = GetObject("LDAP://" & SYS.UserName)
If EchoStuff then msgbox "ADSPath for ADSUser:" & vbcrlf & ADSUser.adspath

Dim ADSUserOU
Set ADSUserOU = GetObject(ADSUser.parent)
If EchoStuff then msgbox "ADSPath for User OU:" & vbcrlf & ADSUserOU.adspath

Dim WinntUser
Set WinntUser = GetObject("WinNT://" & WSHNet.UserDomain & "/" & WSHNet.UserName & ",user")
If EchoStuff then msgbox "ADSPath for WinntUser:" & vbcrlf & WinntUser.adspath

Dim ADSComputer
Set ADSComputer = GetObject("LDAP://" & SYS.ComputerName)
If EchoStuff then msgbox "ADSPath for ADSComputer:" & vbcrlf & ADSComputer.adspath

Dim ADSComputerOU
Set ADSComputerOU = GetObject(ADSComputer.parent)
If EchoStuff then msgbox "ADSPath for Computer OU:" & vbcrlf & ADSComputerOU.adspath

'****************End of Declarations, beginning of Main Script****************

RemoveNetworkDrives
RemoveNetworkPrinters

If EchoStuff then msgbox "Adding Common Drives"
WSHNet.MapNetworkDrive "I:", "\\R105002\intercept"
WSHNet.MapNetworkDrive "S:", "\\R105002\shared"

If UserIsMemberOfGroup("Domain Admins") then
   If EchoStuff then msgbox "Adding IT Department Drives"
   objNetwork.MapNetworkDrive "N:", "\\R105002\install"
   objNetwork.MapNetworkDrive "H:", "\\R105002\home"
   objNetwork.MapNetworkDrive "S:", "\\R052002\home"
   objNetwork.MapNetworkDrive "P:", "\\R227002\home"
   objNetwork.MapNetworkDrive "M:", "\\R221002\home"
end if

Select Case Ucase(ADSUserOU.OU) 'All case statements below should be in capitals

   Case "OU_MTP"
      WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName

   Case "OU_OPERATIONS"
      WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName

   Case "OU_PWEST"
      WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName
   
   Case "OU_SERT"
      WSHNet.MapNetworkDrive "Z:", "\\R052002\home\" & ADSUser.SamAccountName

   Case "OU_MBS"
      WSHNet.MapNetworkDrive "Z:", "\\R221002\home\" & ADSUser.SamAccountName
   
   Case "OU_PWEST"
      WSHNet.MapNetworkDrive "Z:", "\\R227002\home" & ADSUser.SamAccountName

   Case Else
      If EchoStuff then msgbox "The " & ADSUserOU.OU & " Organizational Unit isn't listed for drive mappings"

End Select
 
'******End of Main Script, beginning of Subs & Functions*********************

'**********************
Sub RemoveNetworkDrives
   Dim oDrives, d
   Set oDrives = WSHNet.EnumNetworkDrives

   For d = 0 To oDrives.Count-1 Step 2
      If EchoStuff then msgbox "Removing " & oDrives.Item(d) & " (" &  oDrives.Item(d + 1) & ")"
      WSHNet.RemoveNetworkDrive oDrives.Item(d), True, True
   Next
End Sub
'**********************
Sub RemoveNetworkPrinters

   Dim oPrinters, p
   Set oPrinters = WshNet.EnumPrinterConnections

   For p = 0 To oPrinters.Count - 1 Step 2
      If EchoStuff then msgbox "Removing Printer:" & vbcrlf & oPrinters.Item(p)
      WSHNet.RemovePrinterConnection oPrinters.Item(p+1), True
   Next

End Sub
'**********************
Function UserIsMemberOfGroup(GroupName)
   UserIsMemberOfGroup = False

   Dim objGroup
   Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")

   UserIsMemberOfGroup = objGroup.IsMember(WinntUser.ADsPath)

End Function
'**********************
'End of Script


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
whoamAuthor Commented:
Looks good, very good.  Yet again, I'm not where I can test this.   (I'm selling a house, buying another, moving, closings, utilities, etc).  The reason I need the old mapped drives removed is that they have persistant drives via a batch file they have been using for loging on.  

I should have an answer for you by COB Wednesday.

In the sub's you place a True statement after the
WSHNet.RemovePrinterConnection oPrinters.Item(p+1),
and 2 True statements after
WSHNet.RemoveNetworkDrive oDrives.Item(d),
What do they do?

Also, in the printer mappings, will a user recieve printers if s/he is in a child OU of the OU listed in the Sub?

Finally, I'm going to need to call an executable that's hung out on a shared drive via a UNC.  How do I do this?

Wscript.shell.run "\\server\share\prog.exe",0                   ?

I don't need or want user interaction, so will the 0 hide the program for the users?

0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.



By the way, how much do you charge for a full scripting class? ;)

0
 
ddepastinoCommented:
You might not need the double trues, I was using them because I was testing the script while logged on and the drives didn't go away because I had files open on them.

WSHNet.RemoveNetworkDrive Device, Force, UpdateProfile
-The Device parameters specifies the network drive device to disconnect. For devices mapped to a local drive letter, this parameter should be the drive letter (x:), for other devices this should be the network path.
-The optional Force parameter is a boolean value used to specify whether the device should be removed even if still in use.
-The optional UpdateProfile is a boolean used to specify whether the mapping should be permanently removed from the users profile as well as the current session.

For the program run you'll need these:

in the declarations:

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.shell")

In the main script:

WSHShell.Run CommandLine, WindowState, WaitOnReturn

So yes, 0 = invisible
WSHShell.Run "\\ServerName\ShareName\InvisibleApp.exe", 0

Another cool part of that is you can specify the script to pause until it is finished running if you need to.  The following would pause the script until the program ended (by default this is FALSE):
WSHShell.Run "\\ServerName\ShareName\InvisibleApp.exe", 0, TRUE

I just looked up and noticed you have no printer mappings in any of the stuff here.  Did you mean to map printers in the "Select Case" section?  Either way that wouldn't work if the OU was a child of the OU specified.  We accomplished that with the IsIndirectMemberOfOU function.  Careful with that one because if the OUs are nested you could end up assigning the wrong drive/printer, IsIndirectMemberOfOU would test for the OU name ANYWHERE in the path.  For example:

OUName = Employees
OUPath = LDAP://OU=Employees, OU=BuildingB, OU=SiteA, OU=AllEmployees, DC=Domain, DC=Forest, DC=COM

If Instr(1, OUPath, OUName) then.....

This would return TRUE for both the Employees AND AllEmployees OUs, get it?  You just need to be aware of it when you do AD stuff in the future.

Anyhow, here's how you can put both functions into one:

Function IsMemberOfOU(OUName, OnlyCheckParent)
   IsMemberOfOU = False

   If Ucase(ADSUserOU.OU) = ucase(OUName) then
      IsMemberOfOU = True
   End If

   If OnlyCheckParent then Exit Function

   If InStr(1, UCase(ADSUserOU.adspath), UCase(OUName)) Then
      IsMemberOfOU = True
   End If

End Function

Replace the entire "Select Case......End Select" section with calls to the function.  To check just the parent:

If IsMemberOfOU(ou_MtP, TRUE) Then.....

And to check for parent OR child of:

If IsMemberOfOU(ou_MtP, FALSE) Then.....

I learned all this nonsense with trial and error and looking up junk online.  You don't need no stinking classes!!! :)

0
 
whoamAuthor Commented:
Okay, now we're cooking with gas!  

Below is what I managed to cobble together.  
 Have 6 last questions
1. how do I check to see if a user has a z drive on the server.  i.e.  a user's z drive is a folder names the same as the user's account.  in the case of a new user, they will not have one made for them.

2. with my naming scheme it would be very nice to map printers using wildcard/near matches.  i.e.  a user in mtp_1st_mapping group should get all printers whose names start with mtp_1.

3.  it took some time, but is seems that objects have to be defined within the subs and functions  or they do not work.  i.e.
                set variabl = getobject(wscript.shell)  must appear inside the sub to work there.  is this true?

4.  If I try to put Option Explicit statments anywhere, I get an error that point to the option explicit statment.  any ideas?

5.  the whole subroutine structure evades me.  in Function UserIsMemberOfGroup(GroupName)  you have a variable in parenthases(SP?) what exactly is happening here?

6.  I'm still not clear, how do I make is so that I can map objects based on parent OU memebership?

thanks!

'************************begin script**********************************************

RemoveNetworkDrives
RemoveNetworkPrinters

'Look up some of these on Google, it will give you a good idea of what you are really working with and some of the standard properties/methods associated.  The Microsoft documentation on these isn't too bad.  
'Try having the script echo a few of the properties you find so that you can get used to what you have to work with.

'iadsUser
'iadsOU
'IADsADSystemInfo
'WshNetwork Object
'WshShell Object

'Here's a trimmed down version of what I think you were trying to accomplish:

'On Error Resume Next
'Option Explicit
'WScript.Sleep 3000 'miliseconds


Dim EchoStuff
'Make this False to stop all messages...
EchoStuff = True


Dim SYS
Set SYS = CreateObject("ADSystemInfo")

Dim ADSUser
Set ADSUser = GetObject("LDAP://" & SYS.UserName)
If EchoStuff Then MsgBox "ADSPath for ADSUser:" & vbCrLf & ADSUser.adspath

Dim ADSUserOU
Set ADSUserOU = GetObject(ADSUser.parent)
If EchoStuff Then MsgBox "ADSPath for User OU:" & vbCrLf & ADSUserOU.adspath

Dim WinntUser
'Set WinntUser = GetObject("WinNT://" & WSHNet.UserDomain & "/" & WSHNet.UserName & ",user")
'If EchoStuff Then MsgBox "ADSPath for WinntUser:" & vbCrLf & WinntUser.adspath

Dim ADSComputer
Set ADSComputer = GetObject("LDAP://" & SYS.ComputerName)
If EchoStuff Then MsgBox "ADSPath for ADSComputer:" & vbCrLf & ADSComputer.adspath

Dim ADSComputerOU
Set ADSComputerOU = GetObject(ADSComputer.parent)
If EchoStuff Then MsgBox "ADSPath for Computer OU:" & vbCrLf & ADSComputerOU.adspath

Dim WSHNet
Set WSHNet = WScript.CreateObject("WScript.Network")

Dim objSysInfo, ObjNetwork
Set objSysInfo = WScript.CreateObject("ADSystemInfo")
Set objNetwork = WScript.CreateObject("Wscript.Network")


'****************End of Declarations, beginning of Main Script****************
'clean the systems
RemoveNetworkDrives
RemoveNetworkPrinters

'MAP COMMON DRIVES
If EchoStuff Then MsgBox "Adding Common Drives"
WSHNet.MapNetworkDrive "I:", "\\R105002\intercept"
WSHNet.MapNetworkDrive "S:", "\\R105002\shared"

'MAP ADMINISTRATIVE DRIVES
If UserIsMemberOfGroup("DOMAIN ADMINS") Then
   If EchoStuff Then MsgBox "Adding IT Department Drives"
   objNetwork.MapNetworkDrive "N:", "\\R105002\install"
   objNetwork.MapNetworkDrive "H:", "\\R105002\home"
   objNetwork.MapNetworkDrive "V:", "\\R052002\home"
   objNetwork.MapNetworkDrive "P:", "\\R227002\home"
   objNetwork.MapNetworkDrive "M:", "\\R221002\home"
End If

'MAP PRINTERS PER GROUP MEMBERSHIP
    If UserIsMemberofGroup("mtp_1st_mapping") Then
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_MainBranch_HP_2300dtn_PCL6", "MtP_1st_MainBranch_HP_2300dtn_PCL6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_MainBranch_HP_4200dtn_PCL6", "MtP_1st_MainBranch_HP_4200dtn_PCL6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_MainBranch_HP_4250dtn_PCL6", "MtP_1st_MainBranch_HP_4250dtn_PCL6"
         objprinter.SetDefaultPrinter "\\R105009\MtP_1st_MainBranch_HP_4200dtn_PCL6"
    Else If UserIsMemberofGroup("mtp_2nd_mapping")Then
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_2nd_MainBranch_HP_4250_pcl6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_2nd_MainBranch_HP_3700dn_PCL6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_2nd_MainBranch_HP_8150_PCL5"
    Else If UserIsMemberofGroup("opps_mapping") Then
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_ops_hp_2200_pcl6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_ops_hp_4250dtn_pcl6"
         WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_1st_ops_hp_8150n_pcl5"
    End If
       
 End If

If echosStuff Then MsgBox "Add Z: DRIVE per Site"
    Select Case UCase(objSysInfo.SiteName)
         Case "MAIN-OFFICE"  
            WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName
         Case "MB"
             WSHNet.MapNetworkDrive "Z:", "\\R221002\home\" & ADSUser.SamAccountName
            'UNCOMMENT AFTER THE MOVE
        'Case "PW"
          '   WSHNet.MapNetworkDrive "Z:", "\\R227002\home\" & ADSUser.SamAccountName
         Case "SILLE"
             WSHNet.MapNetworkDrive "Z:", "\\R052002\home\" & ADSUser.SamAccountName
        Case Else
            MsgBox "NO Z: DRIVE"
    End Select





'Select Case UCase(ADSUserOU.OU) 'All case statements below should be in capitals

'   Case "OU_MTP"
'      WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName
   
'    Case "ManagedAdministrators"
 '     WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName

'   Case "OU_OPERATIONS"
'      WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName

'   Case "OU_PWEST"
 '     WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName
   
  ' Case "OU_SUMMERVILLE"
   '   WSHNet.MapNetworkDrive "Z:", "\\R052002\home\" & ADSUser.SamAccountName

'   Case "OU_MBEACH"
 '     WSHNet.MapNetworkDrive "Z:", "\\R221002\home\" & ADSUser.SamAccountName
   
  ' Case "OU_PWEST"
   '   WSHNet.MapNetworkDrive "Z:", "\\R227002\home" & ADSUser.SamAccountName

'   Case Else
 '     If EchoStuff Then MsgBox "The " & ADSUserOU.OU & " Organizational Unit isn't listed for drive mappings"

'End Select
 
'******End of Main Script, beginning of Subs & Functions*********************

'**********************
Sub RemoveNetworkDrives
   Dim oDrives, d, WshNet3
   Set WSHNet3 = WScript.CreateObject("WScript.Network")
   Set oDrives = WSHNet3.EnumNetworkDrives

   For d = 0 To oDrives.Count-1 Step 2
      If EchoStuff Then MsgBox "Removing " & oDrives.Item(d) & " (" &  oDrives.Item(d + 1) & ")"
      WSHNet3.RemoveNetworkDrive oDrives.Item(d), True, True
   Next
End Sub
'**********************
Sub RemoveNetworkPrinters

   Dim oPrinters, p, WshNet2
   Set WSHNet2 = WScript.CreateObject("WScript.Network")

   Set oPrinters = WshNet2.EnumPrinterConnections

   For p = 0 To oPrinters.Count - 1 Step 2
      If EchoStuff Then MsgBox "Removing Printer:" & vbCrLf & oPrinters.Item(p)
      WSHNet2.RemovePrinterConnection oPrinters.Item(p+1), True
   Next

End Sub
'**********************
Function UserIsMemberOfGroup(GroupName)
   UserIsMemberOfGroup = False

   Dim objGroup
   Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")
   Set WinntUser = GetObject("WinNT://" & WSHNet.UserDomain & "/" & WSHNet.UserName & ",user")

   UserIsMemberOfGroup = objGroup.IsMember(WinntUser.ADsPath)
   
End Function
'**********************
Function IsMemberOfOU(OUName, OnlyCheckParent)
   IsMemberOfOU = False

   If Ucase(ADSUserOU.OU) = ucase(OUName) then
      IsMemberOfOU = True
   End If

   If OnlyCheckParent then Exit Function

   If InStr(1, UCase(ADSUserOU.adspath), UCase(OUName)) Then
      IsMemberOfOU = True
   End If

End Function
'**********************

End If
0
 
ddepastinoCommented:
Order of a script:
1) Declarations
2) Main Script
3) Subs & Functions

Man, are you getting your points worth or what????  :)

Q3: You are attempting to run subs/functions before declaring the objects/variables that they rely on.  A script is read by the OS from the top down, when a sub or function is called it jumps (up or down) to the sub or function and then returns to where it was reading from, in this case the main script.  So what is happening is you are calling the RemoveNetworkDrives sub BEFORE you declare and initialize the WSHNet object, this is why you needed to declare a new one inside the sub.

Q4: Option Explicit needs to be declared first and applies to the entire script, you cannot use it anywhere else.  It enforces declaring a variable before using it, most often you will find errors because of the misspellings of variables.

Q5:  When you call a Subroutine or function you are stepping outside your main script to do something and then go back to the main script.  This concept took a while for me to understand but here's the skinny.  Take this for example:

WSHNet.MapNetworkDrive "Z:", "\\R105002\home\" & ADSUser.SamAccountName

The WSHNET object is an object that is defined in a dll file on the OS.  MapNetworkDrive is actually a subroutine (EXACTLY like the subs we made up) and the stuff after that is parameters for the sub.

Let's make our own fake sub that imitates it, I am assuming you know the "Net Use" command in CMD/DOS.

Sub MapADrive(Drive, UNCPath)
   Dim WSHShell
   Set WSHShell = WScript.CreateObject("WScript.shell")
   WSHShell.Run "C:\Windows\System32\CMD.exe Net Use " & Drive & " " & UNCPath
End Sub

And of course we would call this sub like this:

MapADrive "Z:", "\\Server\Share\Folder"

So inside the sub what actually gets executed inside the sub above is:
WSHShell.Run C:\Windows\System32\CMD.exe Net Use Z: \\Server\Share\Folder

The parameters for the sub (Drive and UNCPath) are used as variables in the sub, what you are doing here is declaring a variable (called a parameter) that only the sub can use and/or see.
Get it?

And Now for a function; a function is basically a sub that returns a value, let's use objGroup for this one:

if objGroup.IsMember(WinntUser.ADsPath) then

objGroup is an object that is defined in a dll file on the OS. IsMember is actually a function (EXACTLY like the functions we made up) and the item after that is a parameter.  What makes this different from the sub is that it returns a value inside our IF....THEN statement.

What I am doing with the IsMemberOfGroup function is making multiple lines of code into a question.  What if we didn't use a function for this?  The code would look like this for printer mappings:

'*****start code without function******
Dim objGroup, GroupName
GroupName = "FakeGroupA"

Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")
If objGroup.ismember(Winnt.ADSPath) then
         WSHNet.AddWindowsPrinterConnection "\\R105009\printerA"
end if

GroupName = "FakeGroupB"

Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")
If objGroup.ismember(Winnt.ADSPath) then
         WSHNet.AddWindowsPrinterConnection "\\R105009\printerB"
end if

GroupName = "FakeGroupC"

Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")
If objGroup.ismember(Winnt.ADSPath) then
         WSHNet.AddWindowsPrinterConnection "\\R105009\printerC"
end if

*********end code without function*****************
Yikes!!! what a lot of work for 3 printers, see how we had to "Set objGroup" each time when we changed the GroupName?  It only gets worse as you add more printer mappings over time.  Let's try that again with the function:

*************start code with function**************
If UserIsMemberOfGroup("FakeGroupA") then
    WSHNet.AddWindowsPrinterConnection "\\R105009\printerA"
end if

If UserIsMemberOfGroup("FakeGroupB") then
    WSHNet.AddWindowsPrinterConnection "\\R105009\printerB"
end if

If UserIsMemberOfGroup("FakeGroupC") then
     WSHNet.AddWindowsPrinterConnection "\\R105009\printerC"
end if

Function UserIsMemberOfGroup(GroupName)
   UserIsMemberOfGroup = False

   Dim objGroup
   Set objGroup = GetObject("WinNT://" & WSHNet.UserDomain & "/" & GroupName & ",group")
   UserIsMemberOfGroup = objGroup.IsMember(WinntUser.ADsPath)
   
End Function

'*************end of code with function*****************

The function basically encapsulates code into a question for us, most importantly it reads better and makes troubleshooting easier.

You might want to read all that garbage a few times, if you can get that concept it will make scripting ALOT easier.

Q6: So the OU function is actually simple:

Function IsMemberOfOU(OUName, OnlyCheckParent)  'Function(Parameter, Parameter)
   IsMemberOfOU = False 'Set false initially             ***Return False

   If Ucase(ADSUserOU.OU) = ucase(OUName) then ***if ADSUserOU.OU = parameter(OUName) then.....
      IsMemberOfOU = True                                    ***Return True
   End If

   If OnlyCheckParent then Exit Function                   ***If parameter(OnlyCheckParent) = True then exit function

   If InStr(1, UCase(ADSUserOU.adspath), UCase(OUName)) Then    ***If parameter(OUName) is anywhere in the ADSPath then...
      IsMemberOfOU = True                                                           *** Return True
   End If

End Function

So you can call it 2 ways:
IsMemberOfOU("FakeOU", False)  ****Check if the user account is contained within the FakeOU OU
IsMemberOfOU("FakeOU", True)  ****Same as line above -OR- FakeOU is anywhere in the ADSUser.ADSPath

So to map a printer for a user account that is ONLY inside the OU:
If IsMemberOfOU("FakeOU", True) then
   WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_2nd_MainBranch_HP_4250_pcl6"
end if

And to map a printer if the user account is contained in the OU or is a child of the OU:
If IsMemberOfOU("FakeOU", False) then
   WSHNet.AddWindowsPrinterConnection "\\R105009\MtP_2nd_MainBranch_HP_4250_pcl6"
end if

Q2:  Let's wait on that one......

The Select Case....End Select for printers will not work with groups because of the way select statements work.
In the "'MAP PRINTERS PER GROUP MEMBERSHIP" section be aware that an IF....ElseIf....EndIf  statement will take the first thing that is true and exit the statement.  So if a user is a member of both the mtp_1st_mapping and mtp_2nd_mapping groups ONLY the mtp_1st_mapping stuff will happen, after this it would exit.

Homework assignment:  Take the script I posted Monday and start over, then post the edit again.  Your prize will be the answer to Q1.......

Here are the rules:

1) Declarations !!!!
2) Main Script  !!!!
3) Subs & Functions  !!!!



0
 
whoamAuthor Commented:
Home work?  You've been in schools too long.  Okay, *grumble*
0
 
whoamAuthor Commented:
worm/bot search text
map drive printer active directory AD site group user membership assign based on remove delete create
0
 
whoamAuthor Commented:
Weird, it's worked for 3 days, I only whish I had had time to be in editing it, so I have not changed it, but I get a vbscript runtime error 800a005 at line marked with the astrisk.

Sub RemoveNetworkPrinters

   Dim oPrinters, p, WshNet2
   Set WSHNet2 = CreateObject("WScript.Network")

   Set oPrinters = WshNet2.EnumPrinterConnections ***************

   For p = 0 To oPrinters.Count - 1 Step 2
      If EchoStuff Then MsgBox "Removing Printer:" & vbCrLf & oPrinters.Item(p)
      WSHNet2.RemovePrinterConnection oPrinters.Item(p+1), True
   Next

End Sub

Thoughts?
0
 
ddepastinoCommented:
Yep, you have a printer that it's trying to remove that it can't.  This is OK, it happens to me for a PrintToFile printer I have.  Just turn erroring off for that sub as seen below.  Your EchoStuff line should be reporting the printer it's having an issue with just before you err out, ewhich should NOT be a network printer.  I think it's got something to do with the EnumPrintersConnections gathering anything that is not considered a local connection.  You could also put in an error handler but this error would actually happen quite commonly and no-one would want to be bothered with it.

BTW, you don't need to have those additional WSHNet objects (WSHNet2) as long as you don't call the sub/function before you declare and initialize WSHNET in the main script.  What's the hold up man? I wanna see the full script!!!  If you want to close this out and E me direct I have my address listed in my profile, it's becoming obvious that no-one is interested in getting in the middle of this insanity.....

Sub RemoveNetworkPrinters
   On Error Resume Next
   Dim oPrinters, p, WshNet2
   Set WSHNet2 = CreateObject("WScript.Network")

   Set oPrinters = WshNet2.EnumPrinterConnections ***************

   For p = 0 To oPrinters.Count - 1 Step 2
      If EchoStuff Then MsgBox "Removing Printer:" & vbCrLf & oPrinters.Item(p)
      WSHNet2.RemovePrinterConnection oPrinters.Item(p+1), True
   Next
  On Error Goto 0
End Sub
0
 
ddepastinoCommented:
oh, they don't post addresses....

Append my EE name with @ B L M F L D <DOT> O R G
Dropping the spaces of course..
0
 
whoamAuthor Commented:
Ah, of course!!!!  I recalled your warning about the error, but had
on error resume next
in the body of the script.  So, it would seem that subs and functions are very much islands.  What, if anything is carried into a sub from the script.  This is off topic and philosophysical, but I was just curious.  THANKS!
0
 
ddepastinoCommented:
Your "island theory" is on the mark, the code in between "Sub Whatever" and "End Sub" becomes it's own entity.  A sub/function can see all of the objects/variables in the main script; but items declared in a sub/function are only available to the sub/function they are contained in.  More importantly remember that all declarations a sub/function uses should be initialized BEFORE calling the sub/function.  If you set a string value to a variable after calling a sub that needs that info you can be assured something is going to go wrong.

Where's that big FINISHED script?????
0
 
whoamAuthor Commented:
Thanks for the update.  I have not forgotten, just busy.  We're implementing offsite backups, putting in a new server room, changing ISP, adding ghost, and the usual desktop issues plus a few other things, so I'm slammed but still interested.

M
0
 
whoamAuthor Commented:
I am NOT quiting, just want to make sure you get ponts.  I should have time for this in June.
0
 
ddepastinoCommented:
Look up a bit and jot down my e-mail from 5-5, good luck with the chaos....been there done that.
0
 
whoamAuthor Commented:
Got it!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now