Last active
February 24, 2023 14:48
-
-
Save CraigChamberlain/d70a2804f20abe8ca2a96654d2d587c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules IsamsBatchApi | |
$config = Get-Content ./config.json -Raw | ConvertFrom-Json | |
$clientID = $config.BatchAPI.ClientID | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText | |
$clientSecret = $config.BatchAPI.ClientSecret | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText | |
$isamsInstance = $config.BatchAPI.Host | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText | |
$searchBase = $config.ActiveDirectory.PupilSearchBase | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText | |
Connect-Isams -ClientID $clientID -ClientSecret $clientSecret -IsamsInstance $isamsInstance | |
$iSAMSPupils = Get-IsamsCurrentPupil | |
Disconnect-Isams | |
$runCount = 0 | |
do | |
{ | |
$runCount ++ | |
$rerun = $false | |
$ADPupils = Get-ADUser -Filter * -SearchBase $searchBase -Properties EmployeeID, EmployeeNumber, DisplayName | |
$isamsUsersToCreate = [System.Collections.Generic.List[Isams.BatchApiClient.Core.Model.PupilManager.PupilManagerInitialisedPupil]]::new() | |
$ManagedADUser = @{} | |
$UnManagedADUser = @{} | |
$ADPupils | | |
ForEach-Object { | |
if ([string]::IsNullOrWhitespace($_.EmployeeID)) | |
{ | |
$UnManagedADUser.Add($_.SamAccountName, $_) | |
} | |
else | |
{ | |
#TODO could include a try catch here for adding duplicate IDs, should be no duplicates but no guarantees. | |
$ManagedADUser.Add([int]$_.EmployeeID, $_) | |
} | |
} | |
$iSAMSPupils | | |
ForEach-Object { | |
if ($ManagedADUser.ContainsKey($_.Id)) | |
{ | |
$user = $ManagedADUser[$_.Id] | |
if ([string]::IsNullOrEmpty($user.EmployeeNumber) -or $user.EmployeeNumber -ne $_.SchoolId) | |
{ | |
Write-Warning ("Setting " + $_.FullName + " EmployeeNumber to " + $_.SchoolId + ' was ' + $user.EmployeeNumber) | |
Set-ADUser $user -EmployeeNumber $_.SchoolId | |
} | |
if ([string]::IsNullOrEmpty($user.GivenName) -or $user.GivenName -ne $_.Forename){ | |
Write-Warning ("Setting " + $_.FullName + " GivenName to " + $_.Forename + ' was ' + $user.GivenName) | |
Set-ADUser $user -GivenName $_.Forename | |
} | |
$name = $_.Surname + ", " + $_.Preferredname | |
if ([string]::IsNullOrEmpty($user.DisplayName) -or $user.DisplayName -ne $name){ | |
Write-Warning ("Setting " + $_.FullName + " Display Name to " + $name + ' was ' + $user.DisplayName) | |
Set-ADUser $user -DisplayName $name | |
} | |
if ([string]::IsNullOrEmpty($user.Name) -or $user.Name -ne $name){ | |
Write-Warning ("Setting " + $_.FullName + " Name to " + $name + ' was ' + $user.Name) | |
$user | Rename-ADObject -NewName $name | |
} | |
} | |
elseif ($UnManagedADUser.ContainsKey($_.SchoolCode)) | |
{ | |
$user = $UnManagedADUser[$_.SchoolCode] | |
$msg = "Set ID for - " + $User.Name + "? (y/n)" | |
do | |
{ | |
$answer = (Read-Host $msg).Trim().ToLower() | |
} | |
until ("y" -eq $answer -or "n" -eq $answer ) | |
if ("y" -eq $answer) | |
{ | |
Set-ADUser $user -EmployeeID $_.Id | |
$UnManagedADUser.Remove($_.SchoolCode) | |
$rerun = $true | |
} | |
} | |
else | |
{ | |
Write-Warning "No AD user for $($_.Fullname)" | |
$isamsUsersToCreate.Add($_) | |
} | |
} | |
} | |
until ($runCount -eq 2 -or -not $rerun) | |
$isamsUsersToCreate.Count | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment