::------------------------------------------------------------------------------------------------::
:: Script Name - TakeOwnership ::
:: Purpose - Take ownership of several directories using LCACS. ::
:: Version - 1.0 ::
:: Assumptions - That the Machine is windows NT Based to use the windows NT command set ::
:: ::
:: (c) Copyright 2008-2010, Ben Personick, All Rights Reserved ::
::------------------------------------------------------------------------------------------------::
:Begin
ECHO Off
SET ScriptName=TakeOwnership
ECHO ---- Begining %ScriptName%... ----
::------------------------------------------------------------------------------------------------::
:: This section sets up default variable settings and sets the logging directory and file name. ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
CALL :Start-Set-Date
SET Preamble=%ScriptName% --
::--------------------------------------------------------------------------------------------::
:: Sets Relative Paths used to search later. ::
::--------------------------------------------------------------------------------------------::
REM -- Set Source Directory and File
SET RFDir=C:\Admin\%ScriptName%
SET RFFile=Name.csv
REM -- Set Users Base Directory
SET BaseDir=C:\test
REM -- Set Users Domain
SET Domain=ContossoCorp
::--------------------------------------------------------------------------------------------::
:: This Section Sets up logging variables. ::
::--------------------------------------------------------------------------------------------::
SET IDir=C:\Admin\%ScriptName%\Logs
SET ILog=%ScriptName%_%TDate%_%TTime%.Log
SET IDL=%IDir%\%ILog%
::--------------------------------------------------------------------------------------------::
:: This Section Creates Directories for Logging and other files ::
::--------------------------------------------------------------------------------------------::
:Start-Create-Directories
For /F %%D IN ("%IDir%") DO CALL :Start-DIR-Loop %%D
GOTO End-DIR-Loop
:Start-DIR-Loop
SET TDir=%1
ECHO %Preamble% Checking For %TDir%
IF EXIST %TDir% CALL :Start-Echo "%TDir% -- Found!"
IF EXIST %TDir% GOTO :EOF
ECHO %Preamble% Creating %TDir%
MKDir %TDir%
CALL :Start-Echo "Created %TDir%"
GOTO :EOF
:End-DIR-Loop
CALL :Start-Echo "Dir-Loop Completed"
:End-Create-Directories
::--------------------------------------------------------------------------------------------::
:: This Section Sets Default Values for match variables, don't change except for testing. ::
::--------------------------------------------------------------------------------------------::
REM None Needed.
:End-Prep
::------------------------------------------------------------------------------------------------::
:: This begins the meat of the batch file ::
::------------------------------------------------------------------------------------------------::
:Start-Main
CALL :Start-Echo "Begining Main Processing"
FOR /F "tokens=*" %%R IN ('TYPE "%RFDir%\%RFFile%"') DO Call :Start-Change-Owner-Loop "%%R"
Goto End-Change-Owner-Loop
:Start-Change-Owner-Loop
SET TName=%~1
CALL :Start-Echo "Temp Name = %TName%"
CALL :Start-Echo "TAKEOWN /f %BaseDir%\%TName% /r /d y"
TAKEOWN /f "%BaseDir%\%TName%" /r /d y
CALL :Start-Echo "ICACLS %BaseDir%\%TName% /reset /T"
ICACLS "%BaseDir%\%TName%" /reset /T
CALL :Start-Echo "ICACLS %BaseDir%\%TName% /grant:r %domain%\%TName%:(OI)(CI)F"
ICACLS "%BaseDir%\%TName%" /grant:r "%domain%\%TName%":(OI)(CI)F
CALL :Start-Echo "ICACLS %BaseDir%\%TName% /setowner %domain%\%TName% /T"
ICACLS "%BaseDir%\%TName%" /setowner "%domain%\%TName%" /T
GOTO :EOF
:End-Change-Owner-Loop
:End-Main
GOTO :End
GOTO End-Subs
GOTO :EOF
:Start-Subs
GOTO :End-Set-Date
:Start-Set-Date
SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
GOTO :EOF
:End-Set-Date
GOTO End-Echo
:Start-Echo
Set TEcho=%~1
ECHO %Preamble% %TEcho% --
ECHO %Preamble% %TEcho% -- >> %IDL%
GOTO :EOF
:End-Echo
:End-Subs
:End
CALL :Start-Set-Date
CALL :Start-Echo "Batch Completed ON %TDate% AT %TTime%"
GOTO :EOF
|