Currently I have a working solution:
#Declare Variables
$PMArray = @()
$SMArray = @()
#Get Input File and put information in 2 Arrays, 1 for Personal Mailboxes
#and 1 for Shared Mailboxes
Import-Csv "C:\inputfile.csv" -Delimiter ';' | foreach {
$InputMBXType = $_."MailboxType"
$InputL = $_."Level"
$InputSMTP = $_."PrimarySMTPAddress"
if ($InputMBXType -eq "Personal Mailbox") {
$PMObject = New-Object psobject
$PMObject | Add-Member NoteProperty 'MailboxType' $InputMBXType
$PMObject | Add-Member NoteProperty 'Level' $InputL
$PMObject | Add-Member NoteProperty 'EmailAddress' $InputSMTP
$PMArray += $PMObject
}
if ($InputMBXType -eq "Shared Mailbox") {
$SMObject = New-Object psobject
$SMObject | Add-Member NoteProperty 'MailboxType' $InputMBXType
$SMObject | Add-Member NoteProperty 'Level' $InputL
$SMObject | Add-Member NoteProperty 'EmailAddress' $InputSMTP
$SMArray += $SMObject
}
}
#Split Arrays by department
#Personal Mailboxes
$ABCpmarray = $PMArray | Where-Object {$_.Level -eq "ABC"}
$DEFpmarray = $PMArray | Where-Object {$_.Level -eq "DEF"}
$GHIpmarray = $PMArray | Where-Object {$_.Level -eq "GHI"}
#Shared Mailboxes
$ABCsmarray = $SMArray | Where-Object {$_.Level -eq "ABC"}
$DEFsmarray = $SMArray | Where-Object {$_.Level -eq "DEF"}
$GHIsmarray = $SMArray | Where-Object {$_.Level -eq "GHI"}
#Split Array in batches of defined number per batch
[int]$splitat = 50
#ABCpmarray
$runcount = [math]::Ceiling($ABCpmarray.count/$splitat)
for ($LN=0; $LN -lt $runcount; $LN++) {
[int]$begin = $($LN * $splitat)
[int]$end = $(($LN +1) * $splitat) -1
$count = "{0:D2}" -f $LN
$sel = $ABCpmarray[$begin..$end]
$sel | select EmailAddress | Export-Csv -Path "C:\ABC-Personal-$count.csv" -NoTypeInformation
}
#ABCsmarray
$runcount = [math]::Ceiling($ABCsmarray.count/$splitat)
for ($LN=0; $LN -lt $runcount; $LN++) {
[int]$begin = $($LN * $splitat)
[int]$end = $(($LN +1) * $splitat) -1
$count = "{0:D2}" -f $LN
$sel = $ABCsmarray[$begin..$end]
$sel | select EmailAddress | Export-Csv -Path "C:\ABC-Shared-$count.csv" -NoTypeInformation
}
#DEFpmarray
$runcount = [math]::Ceiling($DEFpmarray.count/$splitat)
for ($LN=0; $LN -lt $runcount; $LN++) {
[int]$begin = $($LN * $splitat)
[int]$end = $(($LN +1) * $splitat) -1
$count = "{0:D2}" -f $LN
$sel = $DEFpmarray[$begin..$end]
$sel | select EmailAddress | Export-Csv -Path "C:\DEF-Personal-$count.csv" -NoTypeInformation
}
#DEFsmarray
$runcount = [math]::Ceiling($DEFsmarray.count/$splitat)
for ($LN=0; $LN -lt $runcount; $LN++) {
[int]$begin = $($LN * $splitat)
[int]$end = $(($LN +1) * $splitat) -1
$count = "{0:D2}" -f $LN
$sel = $DEFsmarray[$begin..$end]
$sel | select EmailAddress | Export-Csv -Path "C:\DEF-Shared-$count.csv" -NoTypeInformation
}
Unfortunately all the departments are hard-coded in the script.
I would like to use a CSV file with all the departments. Based on the CSV, new arrays must be created and filled with content.
$_.Level -eq "ABC"what you meant by hardcoded department? What is$PMArrayand$SMArrayforloops into a function.