Create home folders and set permssions

From MyWiki
Jump to: navigation, search
# Read the list of home folders from FS03
$lines = get-content dirlstFS03.txt
$lines | foreach-object { 
 
 
$homefolderpath = $_
$homefoldername =  $_  -Split('\\')
Write-Host $homefoldername[2]               # Filter the account name and print
$username = $homefoldername[2]
 
try {
          if ( get-aduser $username ) 
             ## In this section a valid user has been identified
             { Write-Host "user $username exists"
             $account = get-aduser $username -properties *
             $office = $account.Office
             Write-host " The office is $office"
## We need to do the following steps
###1. Create a home folder if it does not exist
###2. Apply permissions to the folder created 
###3. Apply a quota if none exists
 
## 1.  Creating the home folder if it does not exist
 
Write-Host "home folder path is $homefolderpath"
if ( -Not ( Test-Path -Path $homefolderpath))
       {
        Write-Host "We need to create folder $homefolderpath"
        New-Item -ItemType directory -Path $homefolderpath -force
 
 
 
##2. Apply permissions to the folder created
$permissions = Get-Acl $homefolderpath 
$userpermissions = New-Object System.Security.AccessControl.FilesystemAccessRule("$username","Fullcontrol","ContainerInherit,ObjectInherit","None","Allow")
$permissions.AddAccessrule($userpermissions)
Set-Acl $homefolderpath $permissions
 
###3. Apply a quota ( forcefully )  Two options, one for staff and one for students
 
if ( $office -eq "Student" ) {
                            Write-Host "Setting student quota for user $username"
                            new-fsrmquota $homefolderpath -Template "250 MB Students Report to User"
                           }
if ( $office -eq "Staff" ) {
                          Write-Host "Setting staff quota for user $username"
                          new-fsrmquota $homefolderpath -Template "500 MB Staff Report to User"
                          }
 
 
      }
 
 
 
 
 
 
 
 
 
      }
   }
     catch { Write-Host "Account $username is not valid in this system" }
 
 
 
}