2017-11-17 1 views
1

Office 365의 일부 세부 정보를 CSV로 덤프하는 PowerShell 스크립트를 만들었습니다. Azure 광고에는 120,000 개의 계정이 있습니다. 프로세스 속도를 높이기 위해 스크립트를 25 개의 작업으로 분할했습니다.수신 작업에 데이터가 누락되었습니다.

모든 것은 MFAState의 출력과 데이터를 반환하지 않는 오류를 제외하고는 모두 작동합니다.

이전에 작업을 사용하지 않은 내 스크립트는 완벽하게 작동합니다. 그 데이터가 그 일자리로 돌아 오지 않는 이유는 무엇입니까?

#### Editable Section ##### 
$jobcount = 25     # Set the maximum jobs 
$usersperbatch = 5000   # Users per batch 
$username = "[email protected]" # MSOL Admin account 

#### Start Script ##### 

# Get Credential and connect 
$cred = Get-Credential $username 

"Connecting to MSOnline" 
if (Get-Module MSOnline) {Import-Module MSOnline} 

Connect-MsolService -Credential $cred 

"Getting MSOL Accounts" 
$MSOLUsers = Get-MsolUser -All 
"Total Users: $($MSOLUsers.Count)" 

# Set counts 
$i = 0 
$j = $usersperbatch - 1 
$batch = 1 

while ($i -lt $MSOLUsers.count) 
{ 

    # Pause job creation if jobs equal $jobcount 
    $running = @(Get-Job | Where-Object {$_.State -eq 'Running'}) 
    if ($running.Count -ge $jobcount) {$running | Wait-Job -Any | Out-Null} 

    # Create batch of users 
    $userbatch = $MSOLUsers[$i..$j] 

    # Create Scriptblock for job 
    $sb = { 
     # Import Arguments 
     param ($cred, $MSOLUsers) 

     # Connect to AAD 
     Connect-MsolService -Credential $cred 

     $Results = @() 

     Foreach ($m in $MSOLUsers) 
     { 

      $Result = New-Object PSObject 

      $Result | Add-Member -MemberType "NoteProperty" -Name DisplayName -Value $m.DisplayName 
      $Result | Add-Member -MemberType "NoteProperty" -Name FirstName -Value $m.FirstName 
      $Result | Add-Member -MemberType "NoteProperty" -Name LastName -Value $m.LastName 
      $Result | Add-Member -MemberType "NoteProperty" -Name UserPrincipalName -Value $m.UserPrincipalName 
      $Result | Add-Member -MemberType "NoteProperty" -Name WhenCreated -Value $m.WhenCreated 
      $Result | Add-Member -MemberType "NoteProperty" -Name GUID -Value $m.GUID 
      $Result | Add-Member -MemberType "NoteProperty" -Name LastDirSyncTime -Value $m.LastDirSyncTime 
      $Result | Add-Member -MemberType "NoteProperty" -Name StrongPWord -Value $m.StrongPasswordRequired 
      $Result | Add-Member -MemberType "NoteProperty" -Name Disabled -Value $m.BlockCredential 
      $Result | Add-Member -MemberType "NoteProperty" -Name MFAState -Value $m.StrongAuthenticationRequirements.State 
      $Result | Add-Member -MemberType "NoteProperty" -Name IsLicensed -Value $m.IsLicensed 
      $Result | Add-Member -MemberType "NoteProperty" -Name Valid -Value $m.ValidationStatus 
      $Result | Add-Member -MemberType "NoteProperty" -Name Errors -Value $m.Errors.ErrorDetail.ObjectErrors.ErrorRecord.ErrorDescription 

      $Results += $Result 

     } # End Foreach 

     $Results 

    } # End Scriptblock 

    # Create Job Name based on Range 
    $jobname = "$i-$j" 

    # Start job 
    Start-Job -Name $jobname -ScriptBlock $sb -ArgumentList $cred, $userbatch 

    # update counts 
    $batch += 1 
    $i = $j + 1 
    $j += $usersperbatch 

    if ($i -gt $MSOLUsers.count) {$i = $MSOLUsers.count} 
    if ($j -gt $MSOLUsers.count) {$j = $MSOLUsers.count} 

} # End While 

# Get Results 
$Results = Get-Job | Receive-Job -Wait 

답변

0

당신은 이미 루프 전에이 있기 때문에 당신의 while 루프 Connect-MsolService가 없어도 우선.

또한 루프 앞에 스크립트 블록을 정의하고 사용자 목록 매개 변수에 다른 변수 이름을 사용합니다.

#### Editable Section ##### 
$jobcount = 25     # Set the maximum jobs 
$usersperbatch = 5000   # Users per batch 
$username = "[email protected]" # MSOL Admin account 

#### Start Script ##### 

# Get Credential and connect 
$cred = Get-Credential $username 

"Connecting to MSOnline" 
if (Get-Module MSOnline) {Import-Module MSOnline} 

Connect-MsolService -Credential $cred 

"Getting MSOL Accounts" 
$MSOLUsers = Get-MsolUser -All 
"Total Users: $($MSOLUsers.Count)" 

# Set counts 
$i = 0 
$j = $usersperbatch - 1 
$batch = 1 

# Create Scriptblock for job 
$sb = { 
    # Import Arguments 
    param ($cred, $users) 

    # Connect to AAD 
    #Connect-MsolService -Credential $cred 

    $Results = @() 

    Foreach ($m in $users) 
    { 

     $Result = New-Object PSObject 

     $Result | Add-Member -MemberType "NoteProperty" -Name DisplayName -Value $m.DisplayName 
     $Result | Add-Member -MemberType "NoteProperty" -Name FirstName -Value $m.FirstName 
     $Result | Add-Member -MemberType "NoteProperty" -Name LastName -Value $m.LastName 
     $Result | Add-Member -MemberType "NoteProperty" -Name UserPrincipalName -Value $m.UserPrincipalName 
     $Result | Add-Member -MemberType "NoteProperty" -Name WhenCreated -Value $m.WhenCreated 
     $Result | Add-Member -MemberType "NoteProperty" -Name GUID -Value $m.GUID 
     $Result | Add-Member -MemberType "NoteProperty" -Name LastDirSyncTime -Value $m.LastDirSyncTime 
     $Result | Add-Member -MemberType "NoteProperty" -Name StrongPWord -Value $m.StrongPasswordRequired 
     $Result | Add-Member -MemberType "NoteProperty" -Name Disabled -Value $m.BlockCredential 
     $Result | Add-Member -MemberType "NoteProperty" -Name MFAState -Value $m.StrongAuthenticationRequirements.State 
     $Result | Add-Member -MemberType "NoteProperty" -Name IsLicensed -Value $m.IsLicensed 
     $Result | Add-Member -MemberType "NoteProperty" -Name Valid -Value $m.ValidationStatus 
     $Result | Add-Member -MemberType "NoteProperty" -Name Errors -Value $m.Errors.ErrorDetail.ObjectErrors.ErrorRecord.ErrorDescription 

     $Results += $Result 

    } # End Foreach 

    $Results 

} # End Scriptblock 

while ($i -lt $MSOLUsers.count) 
{ 
    # Pause job creation if jobs equal $jobcount 
    $running = @(Get-Job | Where-Object {$_.State -eq 'Running'}) 
    if ($running.Count -ge $jobcount) {$running | Wait-Job -Any | Out-Null} 

    # Create batch of users 
    $userbatch = $MSOLUsers[$i..$j] 

    # Create Job Name based on Range 
    $jobname = "$i-$j" 

    # Start job 
    Start-Job -Name $jobname -ScriptBlock $sb -ArgumentList $cred, $userbatch 

    # update counts 
    $batch += 1 
    $i = $j + 1 
    $j += $usersperbatch 

    if ($i -gt $MSOLUsers.count) {$i = $MSOLUsers.count} 
    if ($j -gt $MSOLUsers.count) {$j = $MSOLUsers.count} 

} # End While 

# Get Results 
$Results = Get-Job | Receive-Job -Wait 
+0

좋습니다. 하나의 "다른 cmdlet을 호출하기 전에 Connect-MsolService cmdlet을 호출해야합니다." Scriptblock에서 Connect-MSOLService를 주석 처리하지만 여전히 작동합니다. – NomadTales

+0

불행히도 여전히 MFAState 또는 오류에 대해서는 아무것도 반환하지 않습니다. 내 머리를 긁적. – NomadTales

+0

일단 연결되면 다시 연결할 필요가없는이'Connect-MsolService' 프롬프트가있는 것은 정상적인 현상이 아닙니다. 또한 필자는 스크립트를 테스트했음을 언급하고 싶습니다. 실행중인 Azure PowerShell 모듈의 버전은 무엇입니까? – Christophe

관련 문제