SHARE
TWEET

Untitled

a guest Oct 23rd, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # CSV file being imported.
  2. $CsvFile = "$env:USERPROFILEDownloadsSampleData.csv"
  3.  
  4. # Import the contents of the CSV file.
  5. $Users = Import-Csv -Path "$CsvFile"
  6.  
  7. # Logs will be dumped here.
  8. $LogFolder = "C:Temp"
  9.  
  10. # Setting up an array for holding results.
  11. $UserCreationSuccess = @()
  12. $UserCreationFailure  = @()
  13. $UsersAlreadyExist = @()
  14. #$GroupJoinSuccess = @()
  15. $VerbosePreference = "Continue"
  16.  
  17. # Loop through each line of the CSV, creating variables for each field.
  18. ForEach ($User in $Users) {
  19.     # Creating the basic variables.
  20.     $FirstName = $User.'Student First Name'
  21.     $MiddleInitial = $User.'I'
  22.     $LastName = $User.'Student Last Name'
  23.     $ADUserName = $User.'Stu Access Login'
  24.     $StudentID = $User.'Other ID'
  25.     $GradYear = $User.'Grad Year'
  26.     $CapFInitial = $FirstName.substring(0,1).ToUpper()
  27.     $MInitial = $MiddleInitial.substring(0,1).ToLower()
  28.     $LInitial = $LastName.substring(0,1).ToLower()
  29.     $Password = "$CapFInitial$MInitial$LInitial" + "#" + "$StudentID"
  30.     # The folowing couple variables are created via Switch statements.
  31.     $SchoolCode = Switch ($User.'School')
  32.     {
  33.         20 { "Exeter Township Senior High" }
  34.         30 { "Exeter Township Junior High" }
  35.         40 { "Lorane Elementary School" }
  36.         50 { "Jacksonwald ES" }
  37.         70 { "Reiffton School" }
  38.         90 { "Owatin Creek Elementary School" }
  39.     }
  40.  
  41.     $ADGroups = Switch ($User.'School')
  42.     {
  43.         20 { "Secondary Students", "Students" }
  44.         30 { "Secondary Students", "Students" }
  45.         40 { "K4 Students", "Students" }
  46.         50 { "K4 Students", "Students" }
  47.         70 { "Secondary Students", "Students" }
  48.         90 { "K4 Students", "Students" }
  49.     }
  50.  
  51.     # Headers for the CSV exported later.
  52.     $ExportCsvProperties = @{
  53.         FirstName = $FirstName;
  54.         LastName = $LastName;
  55.         UserName = $ADUserName;    
  56.         Error = $Null;
  57.         Date = (Get-Date)
  58.     }
  59.  
  60.     If (-Not(Get-ADUser -Filter {SamAccountName -eq $ADUserName})) {
  61.         Try {
  62.             # Create user.
  63.             New-ADUser `
  64.                 -Name "$FirstName $LastName" `
  65.                 -SamAccountName "$ADUserName" `
  66.                 -GivenName "$FirstName" `
  67.                 -Initials "$MiddleInitial" `
  68.                 -Surname "$LastName" `
  69.                 -DisplayName "$FirstName $MiddleInitial. $LastName" `
  70.                 -UserPrincipalName "$ADUserName@mydomain.com" `
  71.                 -EmailAddress "$ADUserName@mydomain.com" `
  72.                 -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) `
  73.                 -Enabled $false `
  74.                 -PasswordNeverExpires $true `
  75.                 -CannotChangePassword $true `
  76.                 -Path "OU=$GradYear,OU=Students,OU=$SchoolCode,OU=accounts,DC=academic,DC=mydomain,DC=com"
  77.  
  78.             # If you've gotten this far, the user has been created; output to screen.
  79.             Write-Verbose "[PASS] Created [$($ADUserName)]."
  80.  
  81.             # Add SUCCESS data to the array.
  82.             $UserCreationSuccess += New-Object -TypeName PSCUSTOMOBJECT -Property $ExportCsvProperties
  83.  
  84.             # Add user to group(s).
  85.             ForEach ($ADGroup in $ADGroups) {
  86.                 # Add user to group.
  87.                 Get-ADUser -Identity $ADUserName | Add-ADPrincipalGroupMembership -MemberOf $ADGroup
  88.  
  89.                 # Output to the screen
  90.                 Write-Verbose "[PASS] Added [$($ADUserName)] to [$($ADGroup)]."
  91.  
  92.                 # Add SUCCESS data to the array for successful join.
  93.                 #$GroupJoinSuccess += New-Object -TypeName PSCUSTOMOBJECT -Property $ExportCsvProperties
  94.  
  95.             } # End ForEach
  96.  
  97.         } # End Try
  98.  
  99.         Catch {
  100.             #Write-Error "[ERROR] Can't create user [$($ADUserName)] : $_"
  101.             Write-Error "[ERROR] Can't create user [$($ADUserName)] : $($_.Exception.Message)"
  102.  
  103.             # Collect FAILURE data for the array.
  104.             $ExportCsvProperties.error = $($_.Exception.Message)
  105.  
  106.             # Add FAILURE data to the array.
  107.             $UserCreationFailure += New-Object -TypeName PSCUSTOMOBJECT -Property $ExportCsvProperties
  108.  
  109.         } # End Catch
  110.  
  111.     } # End IF
  112.  
  113.     Else {
  114.         Write-Warning "The account [$($ADUserName)] wasn't created, it already exists."
  115.         $UsersAlreadyExist += New-Object -TypeName PSCUSTOMOBJECT -Property $ExportCsvProperties
  116.  
  117.     } # End Else
  118.  
  119. } # End ForEach
  120.  
  121.  
  122. If (-Not(Test-Path $LogFolder)) {
  123.     Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
  124.     New-Item $LogFolder -Force
  125. }
  126.  
  127. # Export data in the array to CSV.
  128. $UserCreationSuccess | Export-Csv -Path "$LogFolderUserCreationSuccess.csv" -NoTypeInformation -Append
  129. $UserCreationFailure | Export-Csv -Path "$LogFolderUserCreationFailure.csv" -NoTypeInformation -Append
  130. $UsersAlreadyExist | Export-Csv -Path "$LogFolderUsersAlreadyExist.csv" -NoTypeInformation -Append
  131. #$GroupJoinSuccess | Export-Csv -Path "$LogFolderGroupJoinSuccess.csv" -NoTypeInformation -Append
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top