I had a similar problem and used a script which was something like this. Forgive any errors as I don't have access to the actual script I used, just a similar one which I have just tweaked for you.
Effectively it takes a folder as a parameter, enumerates the folders within in it and then changes the ownership using icacls. It also logs which folders have been processed and The assumption is that the home folder and the username are the same. You could easily adapt it to reset the file permissions as well just to ensure that all is as it should be.
Code:
' Reset ownership of the sub-folders of the given folder.
Option Explicit
Dim nArgs, objFSO, objFolder, objSubfolder, colSubFolders, strFolder, folderIdx, strSubFolder
Dim strLogDirectory, strProcessedLog
Dim objChildFolder, objSubSubFolder, strSubSubFolder, colChildFolders, strSubFolderName
Dim wshShell, strCmd
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
wscript.echo "Rerun using cscript"
wscript.quit
end if
strLogDirectory = "."
strProcessedLog = "resetFolders.log"
set objFSO = CreateObject("Scripting.FileSystemObject")
nArgs = WScript.Arguments.Count
If nArgs < 1 then
WScript.Echo "usage: cscript Reset_Ownership.vbs Path_to_folders_to_be_reset. Example ""G:\home\pupils\05\"" "
WScript.Quit
end If
strFolder = Wscript.arguments.item(0)
if strFolder = "" then
wscript.echo "No Folder parameter was passed"
wscript.quit
end if
wscript.echo "Top level folder " & strFolder
set objFolder = objFSO.GetFolder(strFolder)
set colSubfolders = objFolder.SubFolders
for each objSubfolder in colSubfolders
strSubFolder = objSubfolder.path
strSubFolderName = objSubfolder.name
'wscript.echo "Resetting permissions in " & strSubFolder
on error resume next
' Reset account permissions
set wshShell = WScript.CreateObject("Wscript.shell")
strCmd = "icacls " & strSubFolder & " /setowner " & strSubFolderName & " /t /c"
wscript.echo strCmd
call wshShell.run (stdCmd, 2, True)
if err.number <> 0 then
WriteAccountLog "Error setting ownership: " & strSubFolder & " Error: " & err.number & " " & err.description, Date, Time, strLogDirectory, strProcessedLog
else
WriteAccountLog "Reset ownership: " & strSubFolder, Date, Time, strLogDirectory, strProcessedLog
end If
on error goto 0
next
wscript.echo "Folder reset completed. Check " & strLogDirectory & strProcessedLog & " for a list folders processed."
wscript.quit
function WriteAccountLog(strSubFolder, strDate, strTime, strLogDirectory, strLog)
Dim objFSO, objFolder, objShell, objLogFile
Dim strLogText
strLogText = strSubFolder & "," & strDate & "," & strTime
' Open account log and account log files for appending.
'Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that strLogDirectory exists
If objFSO.FolderExists(strLogDirectory) then
set objFolder = objFSO.GetFolder(strLogDirectory)
else
set objFolder = objFSO.CreateFolder(strLogDirectory)
end if
if objFSO.FileExists(strLogDirectory & strLog) then
set objFolder = objFSO.GetFolder(strLogDirectory)
else
set objLogFile = objFSO.CreateTextFile(strLogDirectory & strLog)
set objLogFile = objFSO.OpenTextFile (strLogDirectory & strLog, ForAppending, True)
objLogFile.WriteLine("Processed Folder, Update Date,Update Time")
objLogFile.close
end If
' without doing this a permissions error will probably occur
set objFolder = nothing
set objLogFile = nothing
'OpenTextFile method needs a const value
'ForAppending = 8
'ForReading = 1
'ForWriting = 2
Const ForAppending = 8
Set objLogFile = objFSO.OpenTextFile (strLogDirectory & strLog, ForAppending, True)
objLogFile.WriteLine(strLogText)
objLogFile.close
end Function