Recommended Posts

I need to edit an existing vBScript that currently maps users to certain directories. One mapping is to their personal folder a share. However this mapping only takes place if the folder exists. I need to edit the script to check for the folder and then create it if it doesn't exist. Permissions should be so that administrators and said user has access to this folder. I know I can do this on the users account in AD but I would like to do it within the login script if it is at all possible.

Thanks beforehand.

Link to comment
https://www.neowin.net/forum/topic/630628-create-home-directory-with-vbscript/
Share on other sites

Can I ask why you aren't or don't want to complete the home directory field in the user's AD account? :) Seems so much easier...

You could script it (with a user logon script), but it's not going to work unless you grant your users the Create Folders / Append Data permission (and possibly some others - I haven't tested this) on the home directory folder (i.e., \\server\home$\) since the logon script would run with their permissions. Wouldn't say this best practice, but I guess it should technically work. Don't forget to apply the appropriate permissions on their home folder. You can use the cacls.exe command line program for this.

Alternatively, when you create your user, why don't you just create this folder and apply the appropriate permissions on it for them? If you're scripting user creation, this could easily be incorporated (or if you're not, that that's only a few more clicks/1 minute job, but obviously error prone).

It's a bit hard to tell you what to change in your vbscript as it depends on the individual script.

To check for a directory, here is code that might work - link here

To create a directory, here is some code example - link here

How you use / implement the code will depend on your script, so you may want to google for some other examples.

Good example is,

This checks for folder,

<%
Function CheckFolderExists(sFolderName)

Dim FileSystemObject

Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

If (FileSystemObject.FolderExists(sFolderName)) Then
CheckFolderExists = True
Else
CheckFolderExists = False
End If

Set FileSystemObject = Nothing

End Function
%>

Then an example of using that function,

<%
If CheckFolderExists("D:\ProductionReports\July2002") Then
Response.Write("Theuction reports for July 2002 are available")
Else
Response.Write("Theuction reports for July 2002 are not available")
End If
%>

I got this from

http://www.microsoft.com/technet/scriptcen...ss0607.mspx#E4C

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = _

objWMIService.ExecQuery("Select * From Win32_Directory Where Name = 'C:\\Scripts'")

If colFolders.Count < 1 Then

Wscript.Echo "Folder does not exist"

Else

Wscript.Echo "Folder does exist"

End If

You'd replace the . with %COMPUTERNAME% (I think)

and the Scripts part in C:\\Scripts with the variable name of the home directory.

Sorry if I'd made any mistakes as I'm pretty new to scripting myself but I remembered seeing this when reading through the MS scripting website!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.