src/ImportStudents.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
. ".\src\Users.ps1" . ".\src\Classes.ps1" function Import-SkolniLoginStudents { param ( [Parameter(Mandatory = $true)] [string]$FilePath, [Parameter(Mandatory = $true)] [string]$CurrentYear, [Parameter(Mandatory = $true)] [string]$Domain, [Parameter(Mandatory = $true)] [string]$UserGroup, [Parameter(Mandatory = $true)] [string]$UserOU, [Parameter(Mandatory = $true)] [string]$ClassOU, [Parameter(Mandatory = $true)] [int]$ImportType, [Parameter(Mandatory = $true)] [int]$UsernamePattern, [string]$ExtensionAttributeName = "msDS-cloudExtensionAttribute1", [bool]$CleanGroupMembership = $false, [bool]$CleanGroupMembershipOnlyFromClassOU = $true, [string]$GroupDomain = $Domain, [string[]]$IgnoreGroups ) Write-Debug "Loading CSV..."; $Csv = Import-Csv $FilePath Write-Debug "Testing CSV for valid values..."; Test-SkolniLoginStudentCsv $Csv $Csv = $Csv | Select-Object *,Password,Status,Alias,UserPrincipalName Write-Debug "Loading all AD users..."; $adUsers = Get-ADUser -SearchBase $UserOU -Filter * -ResultSetSize 5000 -Properties MemberOf $adUserGroup = Get-ADGroup $UserGroup if($ImportType -eq 1) { Write-Debug "Running initial import..." foreach($Row in $Csv) { $index = $Csv.IndexOf($Row); $user = New-SkolniLoginUser -User ([ref]$Row) ` -Domain $Domain ` -Pattern $UsernamePattern ` -Path $UserOU ` -ExtensionAttributeName $ExtensionAttributeName if($CleanGroupMembership) { $firstMatch = $adUsers | Where-Object { $_.SamAccountName -eq $user.SamAccountName }; if ($firstMatch) { if($CleanGroupMembershipOnlyFromClassOU) { Write-Debug "Removing user $($firstMatch.UserPrincipalName) only from groups in $ClassOU..."; $firstMatch.MemberOf | ForEach-Object { $adGroup = Get-ADGroup $_ if ($adGroup.DistinguishedName -like "*$ClassOU" -and $IgnoreGroups.IndexOf($_.SamAccountName) -eq -1) { Write-Debug "Removing user $($firstMatch.UserPrincipalName) from group $($adGroup.SamAccountName)"; Remove-ADGroupMember -Identity $adGroup -Members $_.DistinguishedName -Confirm:$false } else { Write-Debug "Skipping user $($firstMatch.UserPrincipalName) for removing from group $($adGroup.SamAccountName)"; } } } else { Write-Debug "Removing user $($firstMatch.UserPrincipalName) from all groups..."; $firstMatch.MemberOf | ForEach-Object { $adGroup = Get-ADGroup $_ if ($IgnoreGroups.IndexOf($_.SamAccountName) -eq -1) { Write-Debug "Removing user $($firstMatch.UserPrincipalName) from group $($adGroup.SamAccountName)"; Remove-ADGroupMember -Identity $adGroup -Members $_.DistinguishedName -Confirm:$false } else { Write-Debug "Skipping user $($firstMatch.UserPrincipalName) for removing from group $($adGroup.SamAccountName)"; } } } } } Write-Debug "Adding user $($user.UserPrincipalName) to $($adUserGroup.SamAccountName) group" Add-ADGroupMember -Identity $adUserGroup -Members $user $adUsers = $adUsers | Where-Object { $_.SamAccountName -ne $user.SamAccountName }; $class = New-SkolniLoginClass -Name $Row.Class ` -CurrentYear $CurrentYear ` -Domain $GroupDomain ` -Path $ClassOU Write-Debug "Adding user $($user.UserPrincipalName) to $($class.SamAccountName) class" Add-ADGroupMember -Identity $class -Members $user $Csv[$index] = $Row; } Write-Debug "Storing CSV..." $Csv | ConvertTo-Csv -NoTypeInformation | Out-File "$($FilePath)_RESULT.csv" Write-Debug "Active Directory users left: $($adUsers.Count)" $adUsers | Remove-ADUSer -Confirm:$true } elseif ($ImportType -eq 2) { Write-Debug "Running update import..." foreach ($Row in $Csv) { $index = $Csv.IndexOf($Row); $user = New-SkolniLoginStudent -User ([ref]$Row) ` -Domain $Domain ` -Pattern $UsernamePattern ` -Path $UserOU Write-Debug "Adding user $($user.UserPrincipalName) to $($adUserGroup.SamAccountName) group" Add-ADGroupMember -Identity $adUserGroup -Members $user $adUsers = $adUsers | Where-Object { $_.SamAccountName -ne $user.SamAccountName }; $class = New-SkolniLoginClass -Name $Row.Class ` -CurrentYear $CurrentYear ` -Domain $GroupDomain ` -Path $ClassOU Write-Debug "Adding user $($user.UserPrincipalName) to $($class.SamAccountName) class" Add-ADGroupMember -Identity $class -Members $user $Csv[$index] = $Row; } Write-Debug "Storing CSV..." $Csv | ConvertTo-Csv -NoTypeInformation | Out-File "$($FilePath)_RESULT.csv" } } function Test-SkolniLoginStudentCsv { param ( $Csv ) $ColumnsExpected = @( 'GivenName', 'Surname', 'Class', 'IDIssuer', 'IDType', 'ID' ) $ColumnsOK = $True $ColumnsCsv = $Csv | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name $ColumnsExpected | ForEach-Object { If ($ColumnsCsv -notcontains $_) { $ColumnsOK = $False "Expected column not found: '$($_)'" | Write-Host -ForegroundColor Red } } If (-not $ColumnsOK) { Throw "The csv format is incorrect!" } ## Verify that the contents are OK: $ContentOK = $True $RowIndex = 0 ForEach ($Row In $Csv) { $RowIndex++ $Column = 'GivenName' if ([string]::IsNullOrEmpty($Row.$Column)) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } $Column = 'Surname' if ([string]::IsNullOrEmpty($Row.$Column)) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } $Column = 'Class' if ([string]::IsNullOrEmpty($Row.$Column)) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } $Column = 'IDIssuer' if (-not ($Row.$Column -eq "CZ" -or $Row.$Column -eq "INT")) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } $Column = 'IDType' if (-not ($Row.$Column -eq "BN" -or $Row.$Column -eq "SIN")) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } $Column = 'ID' if ([string]::IsNullOrEmpty($Row.$Column)) { throw "Invalid value for $Column at line $Row, value: $($Row.$Column)" } } If (-not $ContentOK) { Throw "The csv content is incorrect!" } } |