In my script I am pulling accounts from many OU structures, and so I have an array for each. how can I eliminate all these different arrays I am trying to combine down to one for the whole things. Sometimes no accounts are returned in the array and I have issues in past that would blank out the main array I was sticking accounts into.
`#Get a domain controller for the dva domain.
$dc = Get-ADDomainController -DomainName dva -Discover -NextClosestSite
Add the Domains via the following
Create and Array and then add the array to the filter with the domain name and then add the filter to main array collections if it finds information
$dvamailboxes = Get-ADOrganizationalUnit -LDAPFilter "(Name=Shared Mailboxes)" -Server dva | Select-Object -Expand DistinguishedName
After getting the mailbox OU then Create andother array for the accounts and select accounts per the date.
roll through dva looking for accounts
$dvamailboxAccounts = foreach ($box in $($dvamailboxes))
{
Get-ADUser -Server $dc -SearchBase $box -filter * -Property whenCreated | Where {$_.whenCreated -gt $dte}
}
Select the objects from the Get-ADuser and put in an array for use later
$dvaacctArray = $dvamailboxAccounts | select @{Name="userName";Expression={$_.SamAccountName.Trim()}}, @{Name="address";Expression={"dva"}}, @{Name="safeName";Expression={'VA-WIN-MAILBOX-ACCTS'}},
@{Name="platformId";Expression={'VA-WinLocalDisableAcct-Prod-Admin-26-1yr'}}, @{Name="secret";Expression={'12345678'}}
Append the array with the DVA accounts
if($dvaacctArray -gt $null)
{
$mainemailaccounts += $dvaacctArray
}
`
Then I do the same with the next domain
#Get the DC for the AAC domain
$dc = Get-ADDomainController -DomainName aac -Discover -NextClosestSite
#Add the Domains via the following
#Create and Array and then add the array to the filter with the domain name and then add the filter to main array collections if it finds information
$aacmailboxes = Get-ADOrganizationalUnit -LDAPFilter "(Name=Shared Mailbox)" -Server aac | Select-Object -Expand DistinguishedName
#After getting the mailbox OU then Create andother array for the accounts and select accounts per the date.
#roll through dva looking for accounts
$aacmailboxAccounts = foreach ($box in $($aacmailboxes))
{
Get-ADUser -Server $dc -SearchBase $box -filter * -Property whenCreated | Where {$_.whenCreated -gt $dte}
}
#Select the objects from the Get-ADuser and put in an array for use later
$aacacctArray = $aacmailboxAccounts | select @{Name="userName";Expression={$_.SamAccountName.Trim()}}, @{Name="address";Expression={"aac"}}, @{Name="safeName";Expression={'VA-WIN-MAILBOX-ACCTS'}},
@{Name="platformId";Expression={'VA-WinLocalDisableAcct-Prod-Admin-26-1yr'}}, @{Name="secret";Expression={'12345678'}}
***But this is where the code will start getting large when trying the meld all the arrays into one as I keep adding more. How can I reduce this to one arrya otherwise I will end up with about 30 statements like below adding up the arrays.
#Append the array with the AAC accounts
if($aacacctArray -gt $null -and $mainemailaccounts -gt $null)
{
$mainemailaccounts += $aacacctArray
}
elseif ($aacacctArray -gt $null -and $mainemailaccounts -eq $null)
{
$mainemailaccounts += $aacacctArray
}
else
{
Write-Output "No accounts exist to onboard"
exit 1001
}