Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $wx = $Users.Clone()
- $counter = [PSCustomObject]@{Value=0}
- $groups = $Users | Group-Object -Property {[math]::Floor($counter.Value++ / [math]::Ceiling($Users.count / $Config.ThreadCount))}
- foreach ($g in $groups) {
- Start-Job -Name "graph.msal:$($g.Name)" {
- . (Join-Path -Path $using:LocalPath -ChildPath "Get-MSALAuthMethods.ps1")
- $Out = @()
- foreach ($UPN in $using:g.Group) {
- $UPN.AuthMethods = (Get-MSALAuthMethods -UPN $UPN.UserPrincipalName -myClientId $using:Config.ClientID -myTenantId $using:Config.TenantID -myCertificate (Get-Item "Cert:\CurrentUser\My\$($using:Config.CertificateThumbprint)")
- | Select-Object @{l="AuthMethods";e={@($_.value).'@odata.type'.replace("#microsoft.graph.", "") | Select-Object -Unique | Where-Object {$_ -ne "passwordAuthenticationMethod"} }}).AuthMethods
- $Out += $UPN
- }
- return $Out
- }
- }
- Get-Job | Where-Object {$_.name -match "graph.msal:*"} | Wait-Job | Out-Null
- $SyncAdUserCount = $Users.Count
- $w2 = $Users.Clone()
- $Users = @()
- foreach ($job in (Get-Job | Where-Object {$_.name -match "graph.msal:*"})) {
- $Users += Receive-Job $job | Select-Object -ExcludeProperty RunspaceId
- }
- Get-Job -State Completed | Where-Object {$_.name -match "graph.msal:*"} | Remove-Job
- $w1 = $Users.Clone()
- # Учетные записи без MFA и с SamAccountName
- $withoutAuthMethods = $Users | Where-Object {$null -ne $_.SamAccountName -and $null -eq $_.AuthMethods}
- write-host "All: $($Users.count)"
- write-host "withoutAuthMethods: $($withoutAuthMethods.count)"
- # Search SAN by UserPrincipalName
- $withoutSAN = $Users | Where-Object {$null -eq $_.SamAccountName}
- $w3 = $withoutSAN.Clone()
- write-host "withoutSAN: $($withoutSAN.count)"
- foreach ($u in $withoutSAN) {
- $u.SamAccountName = (Get-ADUser -Filter ('UserPrincipalName -eq "{0}"' -f $u.UserPrincipalName)).SamAccountName
- }
- # Add members to group
- $Users = $Users | Where-Object {$null -ne $_.SamAccountName -and $null -ne $_.AuthMethods}
- write-host "Users: $($Users.count)"
- # Add update by UserPrincipalName users
- $withoutAuthMethods += $withoutSAN | Where-Object {$null -ne $_.SamAccountName -and $null -eq $_.AuthMethods}
- write-host "withoutAuthMethods: $($withoutAuthMethods.count)"
- $Users += $withoutSAN | Where-Object {$null -ne $_.SamAccountName -and $null -ne $_.AuthMethods}
- write-host "Users: $($Users.count)"
- $w4 = $withoutSAN.Clone()
- $withoutSAN = $withoutSAN | Where-Object {$null -eq $_.SamAccountName}
- write-host "withoutSAN: $($withoutSAN.count)"
- if ($Users) {
- if ($null -eq (Get-ADGroupMember $Config.ADGroup)) {
- Add-ADGroupMember -Identity $Config.ADGroup -Members $Users.SamAccountName
- } else {
- Add-ADGroupMember -Identity $Config.ADGroup -Members (Compare-Object (Get-ADGroupMember $Config.ADGroup) $Users | Where-Object {$_.SideIndicator -eq '=>'}).inputobject.SamAccountName
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement