Goal: Move 1,800 home shares to two different servers


My goal was to separate the students from faculty with each having their own server, currently all home drives resided on the PDC. I did it in the following manner:



This script is how I access Active Directory and create the csv. Details below:

My students are grouped so they are in groups matching their graduation year. The query below is gathering info about the 2018 graduation year and redirecting it into a file called 2018

ldapsearch -x -L -E pr=200/noprompt -h 10.14.20.20 -b 'ou=2018,ou=students,ou=accounts,DC=example,DC=com' -D 'administrator@example.com' -w mypasswd "*" > 2018

This line calls a script that takes the ldap output and modifies it for the creation of the csv called 2018.csv:

/bin/awk -F ': ' -f /opt/students/script-2-csv.awk < /opt/students/2018 > /opt/students/2018.csv

Here's the script, I want to give credit to the originator of this script: YoLinux LDAP Tutorial: Support scripts and software tools for OpenLDAP directories

# File: ldif2csv-StoogesDumpAll.awk
# Create csv dump for whole database
#
BEGIN {
last = ""
first = ""
name = ""
user = ""
home = ""
# printf(" last,first,full name,\n");
}
/^sn: / {last=$2}
/^givenName: / {first=$2}
/^cn: / {name=$2}
/^mail: / {mail=$2}
/^sAMAccountName: / {user=$2}
/^homeDirectory: / {home=$2}
/^dn/ {
if(last != "" && first != "" && last != "StoogeAdmin") printf("%s,%s,%s,%s,%s\n",name,user,first,last,home)
/^mail: / {mail=$2}
last = ""
first = ""
name = ""
mail = ""
}
# Capture last dn
END { if(last != "" && first != "" && last != "StoogeAdmin") printf("%s,%s,%s,%s,%s\n",name,user,first,last,home)}

In case your creating a csv using some other method, the data needs to be laid out in the following format for the vbs script to correctly process the csv and create the folders:

abigail johnson,abigailjohnson,abigail,johnson,\\sun\students\2018\abigailjohnson

Here's the vb script, I want to give credit to the originator of this script:

Please note that the base folder all of the user folders are created in must exit prior to running the script.

' CaclsExcel.vbs
' Example VBScript to set Cacls
' Version 2.4 - September 2005
' ---------------------------------------------------------'
Option Explicit
Dim intRow, objExcel, objSheet, strPathExcel
Dim strHomeFolder, strHome, strUser
Dim objFSO, objShell, intRunError

'############# Note you will have to amend the following variables

strHome = "\\student1\students\2018\"
strPathExcel = "c:\students\2018.xls"
intRow = 1 ' Row 1 contains headings (set to 2 if headers used)


' Open the Excel spreadsheet
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)

' Create a shell for cmd and CACLS
Set objShell = CreateObject("Wscript.Shell")


' Here is the loop that cycles through the cells
Do Until (objExcel.Cells(intRow,2).Value) = ""
strUser = objExcel.Cells(intRow, 2).Value
call HomeDir ' I decided to use a subroutine
intRow = intRow + 1
Loop
objExcel.Quit ' Clears up Excel


Sub HomeDir()
strHomeFolder = strHome & strUser
If strHomeFolder <> "" Then
If Not objFSO.FolderExists(strHomeFolder) Then
On Error Resume Next
objFSO.CreateFolder strHomeFolder
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Cannot create: " & strHomeFolder
End If
On Error GoTo 0
End If
If objFSO.FolderExists(strHomeFolder) Then
' Assign user permission to home folder.
intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
& strHomeFolder & " /t /c /g Administrators:f "_
& strUser & ":F", 2, True)
If intRunError <> 0 Then
Wscript.Echo "Error assigning permissions for user " _
& strUser & " to home folder " & strHomeFolder
End If
End If
End If
End Sub
objExcel.Quit

WScript.Quit

' End of Cacls example VBScript


ADModify is easy enough to use, the thing you need to remember is the syntax for changing the folders %username% will fail:

\\student1\students\2018\%'sAMAccountName'%