Advertisement
Guest User

asd

a guest
Oct 8th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $wx = $Users.Clone()
  2.     $counter = [PSCustomObject]@{Value=0}
  3.     $groups = $Users | Group-Object -Property {[math]::Floor($counter.Value++ / [math]::Ceiling($Users.count / $Config.ThreadCount))}
  4.  
  5.     foreach ($g in $groups) {
  6.         Start-Job -Name "graph.msal:$($g.Name)" {
  7.             . (Join-Path -Path $using:LocalPath -ChildPath "Get-MSALAuthMethods.ps1")
  8.             $Out = @()
  9.  
  10.             foreach ($UPN in $using:g.Group) {
  11.                 $UPN.AuthMethods = (Get-MSALAuthMethods -UPN $UPN.UserPrincipalName -myClientId $using:Config.ClientID -myTenantId $using:Config.TenantID -myCertificate (Get-Item "Cert:\CurrentUser\My\$($using:Config.CertificateThumbprint)")
  12.                     | Select-Object @{l="AuthMethods";e={@($_.value).'@odata.type'.replace("#microsoft.graph.", "") | Select-Object -Unique | Where-Object {$_ -ne "passwordAuthenticationMethod"} }}).AuthMethods
  13.                
  14.                 $Out += $UPN
  15.             }
  16.             return $Out
  17.         }
  18.     }
  19.  
  20.     Get-Job | Where-Object {$_.name -match "graph.msal:*"} | Wait-Job | Out-Null
  21.  
  22.  
  23.     $SyncAdUserCount = $Users.Count
  24. $w2 = $Users.Clone()
  25.  
  26.     $Users = @()
  27.     foreach ($job in (Get-Job | Where-Object {$_.name -match "graph.msal:*"})) {
  28.         $Users += Receive-Job $job | Select-Object -ExcludeProperty RunspaceId
  29.     }
  30.  
  31.     Get-Job -State Completed | Where-Object {$_.name -match "graph.msal:*"} | Remove-Job
  32. $w1 = $Users.Clone()
  33.     # Учетные записи без MFA и с SamAccountName
  34.     $withoutAuthMethods = $Users | Where-Object {$null -ne $_.SamAccountName -and $null -eq $_.AuthMethods}
  35.  
  36. write-host "All: $($Users.count)"
  37. write-host "withoutAuthMethods: $($withoutAuthMethods.count)"
  38.     # Search SAN by UserPrincipalName
  39.     $withoutSAN = $Users | Where-Object {$null -eq $_.SamAccountName}
  40. $w3 = $withoutSAN.Clone()
  41. write-host "withoutSAN: $($withoutSAN.count)"
  42.     foreach ($u in $withoutSAN) {
  43.         $u.SamAccountName = (Get-ADUser -Filter ('UserPrincipalName -eq "{0}"' -f $u.UserPrincipalName)).SamAccountName
  44.     }
  45.  
  46.     # Add members to group
  47.     $Users = $Users | Where-Object {$null -ne $_.SamAccountName -and $null -ne $_.AuthMethods}
  48. write-host "Users: $($Users.count)"
  49.     # Add update by UserPrincipalName users
  50.     $withoutAuthMethods += $withoutSAN | Where-Object {$null -ne $_.SamAccountName -and $null -eq $_.AuthMethods}
  51. write-host "withoutAuthMethods: $($withoutAuthMethods.count)"
  52.     $Users += $withoutSAN | Where-Object {$null -ne $_.SamAccountName -and $null -ne $_.AuthMethods}
  53. write-host "Users: $($Users.count)"
  54. $w4 = $withoutSAN.Clone()
  55.     $withoutSAN = $withoutSAN | Where-Object {$null -eq $_.SamAccountName}
  56. write-host "withoutSAN: $($withoutSAN.count)"
  57.  
  58.  
  59.     if ($Users) {
  60.         if ($null -eq (Get-ADGroupMember $Config.ADGroup)) {
  61.             Add-ADGroupMember -Identity $Config.ADGroup -Members $Users.SamAccountName
  62.         } else {
  63.             Add-ADGroupMember -Identity $Config.ADGroup -Members (Compare-Object (Get-ADGroupMember $Config.ADGroup) $Users | Where-Object {$_.SideIndicator -eq '=>'}).inputobject.SamAccountName
  64.         }
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement