2017-11-17 4 views
1

Office 365の詳細をCSVにダンプするPowerShellスクリプトを作成しました。 Azure ADには12万のアカウントがあります。プロセスをスピードアップするために、スクリプトを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

良い提案。 "他のコマンドレットを呼び出す前にConnect-MsolServiceコマンドレットを呼び出す必要があります。"スクリプトブロック内のConnect-MSOLServiceをコメントアウトしても、それでも機能します。 – NomadTales

+0

残念ながら、まだMFAStateまたはエラーのために何も返されていません。私の頭を傷つける。 – NomadTales

+0

このConnect-MsolServiceプロンプトを表示するのは正常ではありません。一度接続すれば、再接続する必要はありません。また、私はスクリプトをテストし、私のために働いたことを言及したい。 Azure PowerShellモジュールのどのバージョンを実行していますか? – Christophe

関連する問題