VBS Script To Create AD User "Home Drive"

Home Forums Scripting General Scripting VBS Script To Create AD User "Home Drive"

This topic contains 1 reply, has 2 voices, and was last updated by  CaTBuS 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts

  • jmurphy1919
    Member
    #167412

    Evening,

    I’m after a VBS script ( which i will run as a GPO/Logon ) that will create a folder on our fileserver in a certain directory for all active directory users. This will be then mapped to Z drive and the permissions locked to that user & Domain Admins

    Currently im having to create the folder manually and then use GPO to map that drive i just want a script to create the folder for me automatically for all ( or now new ) users and set permissions

    Folder names need to be the logon name (%Username%) ??
    directory is a network share on the file server
    //fileserver/users/

    Permissions needs to be this user only to have full permissions and Domain Admins

    folders currently in this directory need to be left alone and it only work for new users going forward.

    anyone help/?


    CaTBuS
    Member
    #309115

    This should do everything you need. I’ve tested it and it works a treat. I’ve not added code to add/modify permissions for “Domain Admins” as in most environments this is already forced. If your environment is not set this way, just create another variable/command set for the AD Group. Let me know if you have any questions!

    Code:
    Option Explicit

    Dim objShell, objFSO, objNetwork, objDomain
    Dim strDomain, strUser

    Set objShell = CreateObject(“WScript.Shell”)
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objNetwork = CreateObject(“WScript.Network”)
    Set objDomain = getObject(“LDAP://rootDse”)

    ‘ DECLARE VARIABLE STRINGS
    strDomain = objDomain.Get(“dnsHostName”)
    strUser = objNetwork.UserName
    strFolder = “\FileServerUsers” & strUser

    ‘ IF NETWORK FOLDER DOES NOT EXIST, CREATE FOLDER AND SET PERMISSIONS
    If Not (objFSO.FolderExists(strFolder)) Then
    Dim strICACLS, intRunError
    Set strICACLS = “%comspec% /c echo Y|icacls ” & strFolder & ” /grant ” & strUser & “:(OI)(CI)F /inheritance:r”

    objFSO.CreateFolder(strFolder)
    intRunError = objShell.Run(strICACLS, 2, True)
    End If

    ‘ IF MAPPED DRIVE DOES NOT EXIST, CREATE MAPPED DRIVE
    If Not (objFSO.DriveExists(“Z:”)) Then
    objNetwork.MapNetworkDrive “Z:”, strFolder
    End If

    WScript.Quit

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.