3 Array PowerShell ForEach Loop

I have a SQL Server, SQL Database, and Company array that I need to run a forEach Loop on.

I can do it with a single value array but I have NO idea how to do it for multiple arrays needing the information from the other arrays

Each position in the array corresponds to information about the others. i.e. Company1 uses DB01 with database AR_Agency1, Company2 uses DB02 and database AR_Agency2(See array below).


$Company = (“Company1″,”Company2″,”Company3″,”Company4″,”Company5”)
$DatabaseServer = (“DB01”, “DB02”, “DB03”, “DB04”, “DB02”)
$Database= (“AR_Agency1”, “AR_Agency2”, “Agency3”, “Agency4”, “Agency5”)
#SQL Query
$Query = ‘UPDATE tblAsyncJobQueue SET InactiveUntil = NULL’

#Invoke-Sqlcmd -ServerInstance $DatabasesServer -Database $Database -Query $Query

#So what I want to do is

ForEach ($company in $companies) {



End state I need the query run on each Company database.



If company 1 is always going to use AR_1 and DB1, why not structure the data in a nested hashtable? that will even set you up to splat the commands. Note, this I theoretical, I am unable to test this.

$Query = ‘UPDATE tblAsyncJobQueue SET InactiveUntil = NULL’
$HT = @{
Company1 = @{DataBase = 'AR_Agency1';ServerInstance = 'DB01';Query = $Query}
Company2 = @{DataBase = 'AR_Agency2';ServerInstance = 'DB02';Query = $Query}

foreach($key in $ht.Keys)
Invoke-SQLcmd @ht.$key

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.