3 Array PowerShell ForEach Loop

Welcome Forums General PowerShell Q&A 3 Array PowerShell ForEach Loop

Viewing 2 reply threads
  • Author
    • #215256
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      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.



      • This topic was modified 1 month, 3 weeks ago by kvprasoon. Reason: code formatting
    • #215265
      Topics: 4
      Replies: 90
      Points: 404
      Helping Hand
      Rank: Contributor

      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
    • #215304
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      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.



Viewing 2 reply threads
  • You must be logged in to reply to this topic.