Advertisement
Guest User

Step 1

a guest
May 25th, 2018
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Get Today's Date. Date format 4 is for YYYY-MM-DD, makes folders behave correctly when sorted by name.
  2. $TodaysDate = Get-Date
  3. $TodaysDate = $TodaysDate.GetDateTimeFormats()[4]
  4.  
  5. #Begin Transcript
  6. $TransPath = "\\server\ProgramStore\UserCreation\Transcripts\" + $TodaysDate
  7. md $TransPath
  8. $TransPath = $TransPath + "\Step1.log"
  9. Start-Transcript -Path $TransPath
  10.  
  11. #Set up Log files for output
  12. $LogPath = "\\server\ProgramStore\UserCreation\Logs\" + $TodaysDate
  13. md $LogPath
  14. $ExportPath = "\\server\ProgramStore\UserCreation\Exports\" + $TodaysDate
  15. md $ExportPath
  16. $ErrorLog = $LogPath + "\Errorlog.txt"
  17. $SuccessLog = $LogPath + "\Successlog.txt"
  18. Add-Content $ErrorLog "-----------------------------------------------------------------"
  19. Add-Content $ErrorLog $TodaysDate
  20. Add-Content $ErrorLog "Step 1"
  21. Add-Content $ErrorLog "-----------------------------------------------------------------"
  22. Add-Content $SuccessLog "-------------------------------------------------------------------"
  23. Add-Content $SuccessLog $TodaysDate
  24. Add-Content $SuccessLog "Step 1"
  25. Add-Content $SuccessLog "-------------------------------------------------------------------"
  26.  
  27. ## Create Session with local mail server
  28. $s1=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mail.domain.ac.uk/PowerShell -Authentication Kerberos
  29.  
  30. Import-PSSession -Session $s1
  31.  
  32. ## Add AD Cmdlets
  33. Import-Module ActiveDirectory
  34.  
  35. #Import CSV  
  36. $csv = @()  
  37. $csv = Import-Csv -Delimiter "," -Path "\\fcpsdb\UserCreation\ITAccounts.csv"
  38.  
  39. #Get Domain Base  
  40. $searchbase = Get-ADDomain | ForEach {  $_.DistinguishedName }  
  41.  
  42. #Set up CSVs for Adobe
  43. $CreatedUsers = $ExportPath + "\CreatedUsers.csv"
  44. $AdobeExport = $ExportPath + "\AdobeExport.csv"
  45. $AdobeExport2 = "\\server\ProgramStore\UserCreation\Exports\AdobeExport2.csv"
  46.  
  47. #Create the Output CSV. Will overwrite the file
  48. "Username,Homedrive" | Out-File -FilePath $CreatedUsers -Encoding ASCII
  49. "Firstname,Lastname,email" | Out-File -FilePath $AdobeExport -Encoding ASCII
  50. "Identity Type,Username,Domain,Email,First Name,Last Name,Country Code,Product Configurations,Admin Roles,Product Configurations Administered,User Groups,User Groups Administered,Products Administered" | Out-File -FilePath $AdobeExport2 -Encoding ASCII -NoClobber
  51.  
  52. #Leeway for students - the amount of time after their course is expected to end that they can still log in.
  53. $AddTime = New-TimeSpan -Days 90
  54.  
  55. #Loops for each user in the CSV
  56. ForEach ($user In $csv)  
  57. {  
  58.  
  59.     #Creates variables based on the user information
  60.     $OU = "OU=Student,OU=Users,OU=Win7,DC=domain,DC=lan"    
  61.     $title= $user.'Student Ref No'
  62.     $lastname= ($user.'Student Surname'.Substring(0,1).toupper() + $User.'Student Surname'.Substring(1).tolower())
  63.  
  64.     #Creates username. Removes punctuation, takes the last 3 digits of the student ref number.
  65.     $FThree = ($user.'Student Ref No'.Length - 3)
  66.     $Detailedname = $User.'Student First Forename'.Substring(0,1).tolower() + $lastname.tolower() + $user.'Student Ref No'.Substring($FThree,3)
  67.     $Detailedname = $Detailedname -replace "[-]"
  68.     $Detailedname = $Detailedname -replace "[ ]"
  69.     $Detailedname = $Detailedname -replace "[']"
  70.  
  71.     #Create the rest of the AD information
  72.     $UserFirstname = $User.'Student First Forename'  
  73.     $SAM =  $Detailedname
  74.     $UPN= $Detailedname + "@domain.ac.uk"
  75.     $Displayname= $Detailedname
  76.     $Dis= "Student"  
  77.     $profilepath= "\\BRCStudent\Profiles$\Mandatory"
  78.     $Password = $Detailedname.ToLower()
  79.     $POBox = $user.'Student Ref No' -replace "[F]"
  80.  
  81.     #Get the Expiry date, turn it into a Date object.
  82.     $ExpiryDate = $User."Expected End Date"
  83.     $ExpiryDate = Get-Date $ExpiryDate
  84.     $ExpiryDate = $ExpiryDate + $AddTime
  85.     $CompareDate = $ExpiryDate.GetDateTimeFormats()[4]
  86.  
  87.    
  88.     #Set the correct storage location and groups
  89.     if ($user.'Extended Network Storage'-eq 1)
  90.     {$homedrive = "\\BRCStudent\VpaHi\" + $Detailedname
  91.     $group= "VpaHiFile","AllStudents"}
  92.     else {$homedrive = "\\BRCStudent\VpaMed\" + $Detailedname
  93.     $group= "VpaMedFile","AllStudents"}
  94.    
  95.  
  96.     #Set the correct groups for Gaming students
  97.     if ($user.Gaming -eq 1){
  98.     $group="VpaHiFile","AllStudents","LS_ComputingStudents"}
  99.  
  100.     #Set the correct groups for Media students
  101.     if ($user.Media -eq 1){
  102.     $group=$group + "2013_Users_Students_Media"}    
  103.  
  104.  
  105.     #If the Expiry Date is after today, create the account.
  106. if ($TodaysDate -lt $CompareDate){
  107.  
  108.     #If the name will be too long, write it to the Error log. Otherwise, make user.
  109. if ($SAM.length -gt 20) {
  110.     Write-Host "Username is too long: $SAM."
  111.     add-content $ErrorLog "Username is too long: $SAM"
  112.     add-content $ErrorLog "-------------------------------------------------------------------"}
  113.     else{    
  114.  
  115.     #Check if the user exists
  116. $ValidUser = Get-ADUser -Filter 'SAMAccountName -eq $SAM'
  117. if ($ValidUser -ne $null) {
  118.  
  119.     #If so, find their current expiry date. If that fails (due to being null), set it as the ExpiryDate
  120. Try {
  121.     $CurrentUser = Get-ADUser -Identity $SAM -Properties AccountExpirationDate -ErrorAction Stop
  122.     $CurrentExpire = Get-Date $CurrentUser.AccountExpirationDate  
  123.    
  124.     #If the new Expiry Date is after the Current one, set it to the new one.
  125.     If ($Expirydate -gt $CurrentExpire){
  126.     Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  127.         Write-Host -ForegroundColor Magenta "$SAM Amended"
  128.         add-content $SuccessLog  "User $SAM Expiry amended."
  129.     }
  130.     Else{
  131.         Write-Host -ForegroundColor Red "$SAM Unchanged"
  132.     }
  133.     }
  134. Catch{
  135.     Write-Host -ForegroundColor Magenta "$SAM Amended"
  136.     add-content $SuccessLog  "User $SAM Expiry amended."
  137.     Add-Content $SuccessLog "-------------------------------------------------------------------"
  138.     Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  139.     }
  140.    
  141.  
  142.       }
  143. Else
  144. {    
  145.  
  146.  
  147.       #Create the User if it doesn't exist      
  148.       $create = New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $UPN  -DisplayName $Displayname -GivenName $UserFirstname -Surname $lastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU -Title $title -Description $Dis -profilePath $profilepath -POBox $POBox -HomeDrive H: -HomeDirectory $homedrive -Department "students" -ChangePasswordAtLogon $true  
  149.           Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  150. #
  151.  
  152.        #Make their Home drive. Copy the Resources folder to it.
  153.       md -Path $homedrive
  154.       Copy-Item \\server\ProgramStore\UserCreation\Resources\* $homedrive -Recurse
  155.       "$Detailedname,$homedrive" | Out-File -FilePath $CreatedUsers -Append -Encoding ASCII
  156.  
  157.       #Populate the Adobe export with their details. Dump the CSV in Adobe Enterprise Manager to add them to Adobe.
  158.       $useremail = $Detailedname + "@domain.ac.uk"
  159.       "$UserFirstname,$lastname,$useremail" | Out-File -FilePath $AdobeExport -Append -Encoding ASCII
  160.        
  161.        $Adobe2Identity = "Federated ID"
  162.        $Adobe2Username = $UPN
  163.        $Adobe2Domain = "domain.ac.uk"
  164.        $Adobe2Email = $UPN
  165.        $Adobe2FirstName = $UserFirstname
  166.        $Adobe2LastName = $lastname
  167.        $Adobe2CountryCode = "GB"
  168.        $Adobe2Product = "domain All Apps"
  169.        "$Adobe2Identity,$Adobe2Username,$Adobe2Domain,$Adobe2Email,$Adobe2FirstName,$Adobe2LastName,$Adobe2CountryCode,$Adobe2Product,,,,," | Out-File -FilePath $AdobeExport2 -Append -Encoding ASCII
  170.  
  171.       Write-Host "AD Account $Detailedname created!"  
  172.        
  173.       add-content $SuccessLog  "User $SAM created Sucessfully."
  174.  
  175.    
  176.  
  177.       ## Adding User to Group
  178.       Add-ADPrincipalGroupMembership -Identity $SAM -MemberOf $group
  179.        
  180.       Write-Host " Added to Groups Needed"  
  181.        
  182.       add-content $SuccessLog  "AD User $SAM Added to groups Sucessfully."
  183.       Write-Host -ForegroundColor Green $SAM
  184.  
  185.  
  186.  
  187. Add-Content $SuccessLog "-------------------------------------------------------------------"
  188.  
  189.  
  190. }        
  191.  
  192.  }
  193.  }
  194.  Else{
  195.  Write-Host "AD User $SAM Already Expired"}
  196.  }
  197.  
  198. Add-Content $SuccessLog "-------------------------------------------------------------------"
  199. Get-PSSession|Remove-PSSession
  200. Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement