我创建了一个PowerShell脚本,将Office 365中的一些细节转储到csv中。 Azure AD中有120,000个帐户。为了加快这个过程,我将脚本分成了25个工作。
除了没有返回数据的MFAState和Errors的输出之外,一切都有效。
以前不使用作业的脚本运行完美。有关工作没有返回数据的任何想法吗?
#### Editable Section #####
$jobcount = 25 # Set the maximum jobs
$usersperbatch = 5000 # Users per batch
$username = "user@example.com" # 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 :(得分:0)
首先,您的Connect-MsolService
循环中不需要while
,因为您已经在循环之前已经拥有#### Editable Section #####
$jobcount = 25 # Set the maximum jobs
$usersperbatch = 5000 # Users per batch
$username = "user@example.com" # 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
。
另外,我会在循环之前定义脚本块,并为用户列表参数使用另一个变量名。
{{1}}