Sign in to follow this  
Followers 0
Registered

Mapping Drives per Active Directory - Problems

1 post in this topic

#1 ·  Posted (edited)

Hi,

I need help.

First off, a warning: I'm a newb, not just with AutoIt, but scripting in general. I'm trying to learn this as I go along, so I can get some work done.

I need an .exe for mapping drives according to a domain user's AD group membership. This is primarily for VPN-only users, on Windows 7, whose drives can't be mapped with a GPO (because GP only maps drives during login, before VPN can be connected). Normally it's not an issue, but when migrating shares to new servers, or when a user changes positions or departments, it complicates things. This .exe would be run on an as-needed basis, after the user has logged-in and connected to the VPN.

I searched here for scripts that would do this, and found one from several years ago by user "jazzyjeff."  I tried to use this, and ran into problems, which I'll get into in a bit. I noticed jazzyjeff was still active on the forum, so I PM'd him, and he was gracious enough to send me a login script he currently uses. This script uses Select instead of If, as his first script did. Well, this one is giving me different problems. I'm sure jazzyjeff is busy and he's been helpful enough, so I'm coming to the rest of you for assistance.

The scripts below are not exactly as he wrote them. I've tried to adapt them correctly to my needs and environment, and left off some extra stuff.

This is the first script, using If. The problem with this is that it seems to try to map what is under If in the function, even when it shouldn't (because of AD group membership), and ignores what is under ElseIf. So, there are two functions for mapping here. It will map what is under If for each of them, even if it shouldn't, and will not map what is under ElseIf, even if it should.

#include <constants.au3>
#include <adfunctions.au3>

; Retrieve the name of the connected DC
Global $strHostServer = $objRootDSE.Get ("dc1.domain.com")

_Userpath ()
_DelDrive ()
_Addrive1 ()
_Addrive2 ()

Func _UserPath ()
    ; Retrieve the value of the environment variable %USERNAME%.
    Global $sUserDrive = EnvGet("USERNAME")
 EndFunc

; Delete Drive Maps to be Used
Func _DelDrive ()
    DriveMapDel ("M:")
    DriveMapDel ("P:")
    DriveMapDel ("S:")
    DriveMapDel ("T:")
    DriveMapDel ("U:")
    DriveMapDel ("W:")
    DriveMapDel ("X:")
EndFunc

; Specialty Drive Maps
Func _Addrive1 ()
    If _ADIsMemberOf ("SG Marketing M Drive", @UserName) Then
        DriveMapAdd ("M:", "\\domain.com\Files\Marketing")
    EndIf
 EndFunc

Func _Addrive2 ()
    ;Drive Maps by Department
    ;Cleveland Office
    If _ADIsMemberOf ("SG Cleveland IT", @UserName) Then
        DriveMapAdd ("S:", "\\CLEserv1\Share") ; This is a Windows 2003 server share.
        DriveMapAdd ("T:", "\\domain.com\Files\IT-Dept") ; This is a Windows 2012 share via DFS shortcut
        DriveMapAdd ("U:", "\\CLEserv6\CTUsers\" & $sUserDrive) ; This is a Server 2008 R2 server share.
        DriveMapAdd ("X:", "\\domain.com\Files\IT") ; This is a Netapp Cifs share via DFS shortcut.
    ElseIf _ADIsMemberOf ("SG Cleveland Finance", @UserName) Then
        DriveMapAdd ("S:", "\\CLEserv1\Share")
        DriveMapAdd ("T:", "\\CLEserv6\Finance$")
        DriveMapAdd ("U:", "\\CLEserv6\CTUsers\" & $sUserDrive)
    ElseIf _ADIsMemberOf ("SG Cleveland Human Resources", @UserName) Then
        DriveMapAdd ("S:", "\\CLEserv1\Share")
        DriveMapAdd ("T:", "\\CLEserv6\HR$")
        DriveMapAdd ("U:", "\\CLEserv6\CTUsers\" & $sUserDrive)
    ;Boston Office
    ElseIf _ADIsMemberOf ("SG Boston Users", @UserName) Then
        DriveMapAdd ("P:", "\\BOSserv6\PSTUsers\%username%")
        DriveMapAdd ("T:", "\\BOSserv6\Boston")
        DriveMapAdd ("U:", "\\BOSserv6\CTUsers\%username%")
    Else
        MsgBox (0, "Group Error", "No department membership found.")
    EndIf
EndFunc

Oh... one other thing I did, which may or may not be related to the issue, or may just be bad in general, was to put _ArrayCreate() back into Array.au3, because I was getting an error about it being undefined when I did a syntax check on this first script. I understand that function was >deprecated, for some reason. Not sure if jamming it back in there is problematic or not, but it seemed to get me past the error.

 

This is the second script, using Select instead of If. The problem with this script is that there are two shares that don't get mapped. One is a Windows 2003 server, and the other is a Netapp cifs share. These shares map just fine with the first script above. At first I thought it might have to do with the included files, but I think I have ruled that out by swapping over just the mapping stuff from the second script into the first. 

#include <AD.au3>


;************************************************************************************
;*                                                                                  *
;*  Stop the script running on a server. This script is intended only for laptops.  *
;*                                                                                  *
;************************************************************************************
If @OSVersion = "WIN_2012R2" Or @OSVersion = "WIN_2012" Or @OSVersion = "WIN_2008R2" Or @OSVersion = "WIN_2008" Or @OSVersion = "WIN_2003" Then
    Exit
 EndIf

;************************************************
;*                                              *
;*  Open connection to Active Directory         *
;*                                              *
;************************************************
_AD_Open()

;****************************************************
;*                                                  *
;*  Remove mappings for the drives we want to use.  *
;*                                                  *
;****************************************************
Func _DelDrives()
    DriveMapDel ("M:")
    DriveMapDel ("P:")
    DriveMapDel ("S:")
    DriveMapDel ("T:")
    DriveMapDel ("U:")
    DriveMapDel ("W:")
    DriveMapDel ("X:")
EndFunc

;************************************************************************
;*                                                                      *
;*  Retrieve the value of the environment variable %USERNAME%.          *
;*                                                                      *
;************************************************************************

Func _UserPath()
    Global $sUserDrive = EnvGet("USERNAME")
 EndFunc

;********************************************
;*                                          *
;*  Configure the list of shares            *
;*                                          *
;********************************************

Func _MapCorporateShare()
    DriveMapAdd("S:", "\\CLEserv1\share\") ; This is a Windows 2003 Server. ***This fails to map.***
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##CLEserv1#share", "_LabelFromReg", "REG_SZ", "Corporate Share")
EndFunc

Func _MapITXShare()
    DriveMapAdd("X:", "\\domain.com\Files\IT\") ; This is a DFS shortcut to a Netapp Cifs Share. ***This fails to map.***
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##domain.com#Files#IT", "_LabelFromReg", "REG_SZ", "IT File Storage")
EndFunc

 Func _MapSGClevelandUsers()
    DriveMapAdd("U:", "\\CLEserv6\CTUsers\" & $sUserDrive) ; This is a Windows 2008 R2 Server.
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##CLEserv6#CTUsers#" & $sUserDrive, "_LabelFromReg", "REG_SZ", $sUserDrive & " Personal Share")
 EndFunc

Func _MapSGClevelandIT()
    DriveMapAdd("T:", "\\domain.com\Files\IT-Dept") ; This is a DFS shortcut to a Windows 2012 Server.
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##domain.com#Files#IT-Dept", "_LabelFromReg", "REG_SZ", "Cleveland IT Share")
EndFunc

Func _MapSGClevelandFinance()
    DriveMapAdd("T:", "\\CLEserv6\Finance$\") ; This is a Windows 2008 R2 Server.
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##CLEserv6#Finance$", "_LabelFromReg", "REG_SZ", "Cleveland Finance Share")
EndFunc

;****************************************
;*                                      *
;*  Map the Network Drives              *
;*                                      *
;****************************************

Select
    ; Cleveland IT Users
    Case _AD_IsMemberOf("SG Cleveland IT", @UserName)
       _UserPath()
       _DelDrives()
       _MapSGClevelandIT()
       _MapSGClevelandUsers()
       _MapCorporateShare()
       _MapITXShare()
    ; Cleveland Finance
    Case _AD_IsMemberOf("SG Cleveland Finance", @UserName)
       _UserPath()
       _DelDrives()
       _MapSGClevelandFinance()
       _MapSGClevelandUsers()
       _MapCorporateShare()
EndSelect

; Close connection to Active Directory.
_AD_Close()

I don't know if Select is preferable to If in this case, or whether that matters. I'd probably want to go with whichever one is more flexible, but mainly I just need a reliable, working script.

I'm sure I've probably made a mess of things, and I apologize in advance for any headaches I cause. There are some other things I'd like to use AutoIt for, once I learn more about it. But I want to get this figured out first. 

Any help is greatly appreciated.

Thank you

 

Edit: I posted the above after spending hours and hours over several days trying to figure out the issue with these scripts. And just a few minutes after posting, I realized that the trailing backslashes in the paths (in the second script) could be causing the issue. I removed them and, lo and behold, now all the drives are mapping correctly when I run that second script. So I think I am very close to having a working mapping script... just need to do more testing to confirm.

Still, because I want to learn, I would appreciate some critiques of the scripts, other suggestions, and would still like to know what is up with the first script.

Edited by Registered

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0