henrydenhengst

user creation

Aug 2nd, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $numUsers = "25"
  2. $FormatLength = "1"
  3. $userName = "LoginVSI"
  4. $passWord = "Password!"
  5. $userDomain = "sph.lan"
  6. $baseOU = "DC=sph,DC=lan"
  7.  
  8. for ($i=11; $i -le $numUsers; $i++)
  9. {$fn = "{0:D$FormatLength}" -f $i
  10. $finalUser = $userName + $fn
  11. New-ADUser -name "$finalUser" -SamAccountName "$finalUser" -UserPrincipalName "$finalUser@$userDomain" -GivenName "$finalUser" -Surname "$finalUser" -DisplayName "$finalUser" -path "OU=Target,OU=Users,OU=LoginVSI,$baseOU" -AccountPassword (ConvertTo-SecureString -AsPlainText "$passWord" -Force) -ChangePasswordAtLogon $false -PasswordNeverExpires $true -ScriptPath "V4-VSI_Logon.cmd" -HomePage "http://www.loginvsi.com" -Enabled $true
  12. Add-ADGroupMember -Identity "CN=LoginVSI,OU=LoginVSI,$baseOU" -Members $finalUser
  13.  
  14. homefolder($finalUser)
  15.  
  16. profilefolder($finalUser)
  17.        
  18.    
  19. }
  20.  
  21.  
  22. function homefolder($Finaluser) {
  23.    
  24.     #**** home folder settings ****
  25.     write-host $finaluser
  26.     $username = "SPH\"+$finaluser
  27.  
  28.     $homeDIR = "E:\data\homefolders\"+ $finaluser
  29.     $homeshare = "\\dc\home$\"+ $finaluser
  30.    
  31.     #when folder doesn't exist, create it for the user
  32.     if(!(Test-Path -Path $homedir )){
  33.         New-Item -ItemType directory -Path $homedir
  34.         write-host "folder $homedir not found!"
  35.     }
  36.  
  37.     Set-ADUser -identity $finaluser -HomeDirectory "$Homeshare" -HomeDrive "H:"
  38.    
  39.  
  40.     $ACL = Get-Acl "$Homedir"
  41.     $ACL.SetAccessRuleProtection($true, $false)
  42.     $ACL.Access | ForEach { [Void]$ACL.RemoveAccessRule($_) }
  43.     $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("$NTDomain\Domain Admins","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")))
  44.     $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("$username","Modify", "ContainerInherit, ObjectInherit", "None", "Allow")))
  45.     Set-Acl "$Homedir" $ACL
  46.  
  47.     }
  48.  
  49.  
  50. function profilefolder($finalUser) {
  51.     #**** userprofile settings ****
  52.     $profDIR = "E:\data\profiles\"+$finaluser
  53.     $profshare = "\\dc\profiles$\"+$finaluser
  54.    
  55.     #when folder doesn't exist, create it for the user
  56.     if(!(Test-Path -Path "$profdir.v2" )){
  57.         New-Item -ItemType directory -Path "$profdir.v2"
  58.         write-host "folder $profdir.v2 not found!"
  59.     }
  60.  
  61.     #set profile properties
  62.     set-aduser $finaluser -ProfilePath $profshare
  63.    
  64.     $username = "SPH\"+$finaluser
  65.  
  66.  
  67.     $ACL = Get-Acl "$profdir.v2"
  68.     $ACL.SetAccessRuleProtection($true, $false)
  69.     $ACL.Access | ForEach { [Void]$ACL.RemoveAccessRule($_) }
  70.     $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("SPH\Domain Admins","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")))
  71.     $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("SYSTEM","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")))
  72.     $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("$username","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")))
  73.     Set-Acl "$profDIR.v2" $ACL
  74.  
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment