Scenario: You have a large dataset that you want to use a Do-While Loop with a Paging system to separate the data into smaller data sets. In this example, I will use a Do-While loop to set a roleassignmentpolicy against may mailboxes:
Solution: Run the following commands
Collect the Mailboxes
$mbx = get-mailbox -resultsize unlimited | where RoleAssignmentPolicy -ne “PolicyWithNoEmailForward”
Create the Page Variables
$total_Count = $mbx.count
$Sleep = 60 #in seconds
$page = 10 #the amount of records you want to go through per set
$page_Count = $page #The amount the page will increase
Buffer for DoWhile Loop
$mbx | Select -first $page | %{
$N = $_.name
“Setting RoleAssignmentPolicy on Mbx: $N”
Set-mailbox $n -roleassignmentPolicy “PolicyWithNoEmailForward”
}
Run the DoWhile Loop
Do{
#Run through the first set of Mailboxes
$mbx | Select -first $page -skip $page | %{
$N = $_.name
"Setting RoleAssignmentPolicy on Mbx: $N"
Set-mailbox $n -roleassignmentPolicy "PolicyWithNoEmailForward"
}
#Display a separator between each set
"
Next"
#Increase the Page and SKip Count
$Page = $page + $page_Count
#Sleep Duration
Sleep $Sleep
} While($Page -lt $Total_Count)