Advertisement
Lee_Dailey

Step 1 - VSCode reformat

May 26th, 2018
332
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.     #Creates variables based on the user information
  59.     $OU = "OU=Student,OU=Users,OU=Win7,DC=domain,DC=lan"    
  60.     $title = $user.'Student Ref No'
  61.     $lastname = ($user.'Student Surname'.Substring(0, 1).toupper() + $User.'Student Surname'.Substring(1).tolower())
  62.  
  63.     #Creates username. Removes punctuation, takes the last 3 digits of the student ref number.
  64.     $FThree = ($user.'Student Ref No'.Length - 3)
  65.     $Detailedname = $User.'Student First Forename'.Substring(0, 1).tolower() + $lastname.tolower() + $user.'Student Ref No'.Substring($FThree, 3)
  66.     $Detailedname = $Detailedname -replace "[-]"
  67.     $Detailedname = $Detailedname -replace "[ ]"
  68.     $Detailedname = $Detailedname -replace "[']"
  69.  
  70.     #Create the rest of the AD information
  71.     $UserFirstname = $User.'Student First Forename'  
  72.     $SAM = $Detailedname
  73.     $UPN = $Detailedname + "@domain.ac.uk"
  74.     $Displayname = $Detailedname
  75.     $Dis = "Student"  
  76.     $profilepath = "\\BRCStudent\Profiles$\Mandatory"
  77.     $Password = $Detailedname.ToLower()
  78.     $POBox = $user.'Student Ref No' -replace "[F]"
  79.  
  80.     #Get the Expiry date, turn it into a Date object.
  81.     $ExpiryDate = $User."Expected End Date"
  82.     $ExpiryDate = Get-Date $ExpiryDate
  83.     $ExpiryDate = $ExpiryDate + $AddTime
  84.     $CompareDate = $ExpiryDate.GetDateTimeFormats()[4]
  85.  
  86.    
  87.     #Set the correct storage location and groups
  88.     if ($user.'Extended Network Storage' -eq 1) {
  89.         $homedrive = "\\BRCStudent\VpaHi\" + $Detailedname
  90.         $group = "VpaHiFile", "AllStudents"
  91.     }
  92.     else {
  93.         $homedrive = "\\BRCStudent\VpaMed\" + $Detailedname
  94.         $group = "VpaMedFile", "AllStudents"
  95.     }
  96.    
  97.  
  98.     #Set the correct groups for Gaming students
  99.     if ($user.Gaming -eq 1) {
  100.         $group = "VpaHiFile", "AllStudents", "LS_ComputingStudents"
  101.     }
  102.  
  103.     #Set the correct groups for Media students
  104.     if ($user.Media -eq 1) {
  105.         $group = $group + "2013_Users_Students_Media"
  106.     }    
  107.  
  108.  
  109.     #If the Expiry Date is after today, create the account.
  110.     if ($TodaysDate -lt $CompareDate) {
  111.  
  112.         #If the name will be too long, write it to the Error log. Otherwise, make user.
  113.         if ($SAM.length -gt 20) {
  114.             Write-Host "Username is too long: $SAM."
  115.             add-content $ErrorLog "Username is too long: $SAM"
  116.             add-content $ErrorLog "-------------------------------------------------------------------"
  117.         }
  118.         else {    
  119.  
  120.             #Check if the user exists
  121.             $ValidUser = Get-ADUser -Filter 'SAMAccountName -eq $SAM'
  122.             if ($ValidUser -ne $null) {
  123.  
  124.                 #If so, find their current expiry date. If that fails (due to being null), set it as the ExpiryDate
  125.                 Try {
  126.                     $CurrentUser = Get-ADUser -Identity $SAM -Properties AccountExpirationDate -ErrorAction Stop
  127.                     $CurrentExpire = Get-Date $CurrentUser.AccountExpirationDate  
  128.    
  129.                     #If the new Expiry Date is after the Current one, set it to the new one.
  130.                     If ($Expirydate -gt $CurrentExpire) {
  131.                         Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  132.                         Write-Host -ForegroundColor Magenta "$SAM Amended"
  133.                         add-content $SuccessLog  "User $SAM Expiry amended."
  134.                     }
  135.                     Else {
  136.                         Write-Host -ForegroundColor Red "$SAM Unchanged"
  137.                     }
  138.                 }
  139.                 Catch {
  140.                     Write-Host -ForegroundColor Magenta "$SAM Amended"
  141.                     add-content $SuccessLog  "User $SAM Expiry amended."
  142.                     Add-Content $SuccessLog "-------------------------------------------------------------------"
  143.                     Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  144.                 }
  145.    
  146.  
  147.             }
  148.             Else {    
  149.  
  150.  
  151.                 #Create the User if it doesn't exist      
  152.                 $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  
  153.                 Set-ADUser -Identity $SAM -AccountExpirationDate $ExpiryDate
  154.                 #
  155.  
  156.                 #Make their Home drive. Copy the Resources folder to it.
  157.                 md -Path $homedrive
  158.                 Copy-Item \\server\ProgramStore\UserCreation\Resources\* $homedrive -Recurse
  159.                 "$Detailedname,$homedrive" | Out-File -FilePath $CreatedUsers -Append -Encoding ASCII
  160.  
  161.                 #Populate the Adobe export with their details. Dump the CSV in Adobe Enterprise Manager to add them to Adobe.
  162.                 $useremail = $Detailedname + "@domain.ac.uk"
  163.                 "$UserFirstname,$lastname,$useremail" | Out-File -FilePath $AdobeExport -Append -Encoding ASCII
  164.        
  165.                 $Adobe2Identity = "Federated ID"
  166.                 $Adobe2Username = $UPN
  167.                 $Adobe2Domain = "domain.ac.uk"
  168.                 $Adobe2Email = $UPN
  169.                 $Adobe2FirstName = $UserFirstname
  170.                 $Adobe2LastName = $lastname
  171.                 $Adobe2CountryCode = "GB"
  172.                 $Adobe2Product = "domain All Apps"
  173.                 "$Adobe2Identity,$Adobe2Username,$Adobe2Domain,$Adobe2Email,$Adobe2FirstName,$Adobe2LastName,$Adobe2CountryCode,$Adobe2Product,,,,," | Out-File -FilePath $AdobeExport2 -Append -Encoding ASCII
  174.  
  175.                 Write-Host "AD Account $Detailedname created!"  
  176.        
  177.                 add-content $SuccessLog  "User $SAM created Sucessfully."
  178.  
  179.    
  180.  
  181.                 ## Adding User to Group
  182.                 Add-ADPrincipalGroupMembership -Identity $SAM -MemberOf $group
  183.        
  184.                 Write-Host " Added to Groups Needed"  
  185.        
  186.                 add-content $SuccessLog  "AD User $SAM Added to groups Sucessfully."
  187.                 Write-Host -ForegroundColor Green $SAM
  188.  
  189.  
  190.  
  191.                 Add-Content $SuccessLog "-------------------------------------------------------------------"
  192.  
  193.  
  194.             }        
  195.  
  196.         }
  197.     }
  198.     Else {
  199.         Write-Host "AD User $SAM Already Expired"
  200.     }
  201. }
  202.  
  203. Add-Content $SuccessLog "-------------------------------------------------------------------"
  204. Get-PSSession|Remove-PSSession
  205. Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement