Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

vbscript to create home directory if it does not exist

I'm running a login script that will map some network drives for the users when they log in.  I'd like for the script to do a check to see if the users home directory, \\SERVER\Users$\<USER> exists.  If the folder exist great, it can continue on.  If it does not exist then I would like the script to create the folder and assign the user permissions to the folder.  Any ideas on how I can accomplish this goal?



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
 
 
 
'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
 
'Give the PC time to do the disconnect, wait 500 milliseconds
wscript.sleep 500
 
'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
 
 
 
WSHNetwork.MapNetworkDrive "M:", "\\SERVER\users$\" & UserString ,True
WSHNetwork.MapNetworkDrive "I:", "\\SERVER\Information",True

Open in new window

0
lakeview108
Asked:
lakeview108
  • 2
  • 2
2 Solutions
 
Shift-3Commented:
The script below, inserted at line 21, should do it.  This assumes that the user has sufficient permissions to create the folder and change permissions on it.


strHomeDir = "\\SERVER\Users$\" & UserString
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If Not objFSO.FolderExists(strHomeDir) Then
    objFSO.CreateFolder(strHomeDir)
    WSHShell.Exec("%comspec% /c echo Y|cacls " & Chr(34) & strHomeDir & Chr(34) & _
        " /C /G " & UserString & ":F SYSTEM:F")
End If

Open in new window

0
 
lakeview108Author Commented:
I forgot to mention that the users don't have permissions to create the folders at that level.  Is it possible to use the admin account to create the folder?
0
 
Shift-3Commented:
No, or at least not easily.  You don't want to include a password in a script, because users could view it.

Users' home folders should get automatically created when their accounts are set up.  If some are missing then it might be better to use an AD query to mass-create them for all of your users at once.
0
 
lakeview108Author Commented:
Okay thanks for all of the help.  I'll investigate the AD query and see what I can come up with.  Shift-3, you've been a great help.
0

Featured Post

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.

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