Hello Experts
I'm relatively new to Powershell scene, and also new to this site!
I'm currently trying to automate as many things as possible. One thing I've been looking at recently is automating the creation of Homedrives. If the directory is already created, the script will set the correct permissions on the folder.
So far I've come up with the following:
$Users = Get-ADuser -SearchBase 'OU of the users' -Filter *
$Path = "\\fileserverpath"
foreach ($user in $Users)
{
Set-ADUser $user -HomeDrive P: -HomeDirectory $Path$($user.SamAccountName)
if (!(Test-Path -Path $path$($user.SamAccountName))) {
$HomeFolderACL=Get-Acl $Path
$HomeFolderACL.SetAccessRuleProtection($false,$true)
New-Item -ItemType directory -Path $Path$($user.SamAccountName)|Out-Null
$ACL=New-Object System.Security.AccessControl.FileSystemAccessRule($($user.SamAccountName), "FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")
$HomeFolderACL.AddAccessRule($ACL)
Set-Acl -Path $Path$($user.SamAccountName) -AclObject $HomeFolderACL
}
Else {
$HomeFolderACL=Get-Acl $Path
$HomeFolderACL.SetAccessRuleProtection($false,$true)
$aclold = Get-Acl $Path$($user.SamAccountName)
$aclold.Access | %{$aclold.RemoveAccessRule($_)} | Out-Null
$ACL=New-Object System.Security.AccessControl.FileSystemAccessRule($($user.SamAccountName), "FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")
$HomeFolderACL.AddAccessRule($ACL)
Set-Acl -Path $Path$($user.SamAccountName) -AclObject $HomeFolderACL
}
}