title: Powershell array correlation to SQL table dataset from powershell
body: <p>First off, I'm new to stack.  I have referenced stack many times in the past, but recently I have been stuck on this issue for quite sometime.  So here goes.</p>
  
  <p>My goal:
        I am attempting to correlate an array output from VMware that matches a custom value on each VM machine. ( an asset ID )  to a value ( ID Key ) on a microsoft SQL 2000 server.</p>
  
  <p>As such, since this server is pre 2005  I am unable to use the invoke-sqlcmd powershell command.  I have to utilize the full SQL connection string and command structure to return a value out of this database.      This sql statement and script works fine on its own.  Meaning that the sql portion of this script, functioning on its own will pull results out of the database with a manual tag number put in place of my variable "$etag".     I'm fairly new to powershell, and sql use from powershell.</p>
  
  <p>So here is my script with names of the protected taken out.</p>
  
  <pre><code>#========================================================================
  # Created on:   12/4/2013 2:01 PM
  # Created by:   Shaun Belcher
  # Filename:     
  #========================================================================
  
  
  function get-inventory
  {
  
  Add-PSSnapin VMware.VimAutomation.Core
  $date=get-date
  $vcenterserver = @("srv-1","srv-2","srv-3")
  
  Connect-VIServer -server $vcenterserver
  $toAddr="user@domain.com"
  $fromAddr="user@domain.com"
  $smtpsrv="mail.domain.com"
  #Variables
  $mdesks=@()
  $sqlServer = "serverdb"
  $sqlDBNAME = "instance"
  $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $sqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $DataSet = New-Object System.Data.DataSet
  $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.connection = $sqlConnection
  $sqlAdapter.SelectCommand = $sqlCmd
  
  
  #db Connection
  
  $sqlConnection.ConnectionString = "Server = $sqlServer; Database = $sqlDBname;   Integrated Security=True;"
  $SqlCmd.connection = $SqlConnection
  $SqlCmd.commandtext = $sqlQuery
  $sqlAdapter.SelectCommand = $sqlCmd
  $sqlQuery += "SELECT INVHARDW_PropTag as proptag, invhardw_clientID as ClientID,     invhardw_notes as Notes FROM INV_Hardware where invhardw_proptag = '$etag';"
  $SqlCmd.commandtext = $sqlQuery
  $sqlAdapter.SelectCommand = $sqlCmd
  
  $sqlAdapter.Fill($DataSet)
  $DataSet.Tables[0]
  $sqlConnection.Close()
  
  
  
  
  
  $mdesks = @($DataSet.Tables[0] | select propTag, ClientID, Notes)
  
  $virtuals= @(Get-VM | select Name,vmhost,memoryMB,@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}})
  $etags = @(Get-vm | Get-Annotation |select value,@{N="mDeskNote";E={[string]::Join(',',($mdesk | Where-Object {$mdesks.propTag = $_;}))}},@{N="mDeskClientID";E={[string]::Join(',',($mdesk | Where-Object {$mdesks.propTag = $_;}))}})
  
  
  
  
  
  if($virtuals -ne $null){
  $body = @("
      &lt;center&gt;&lt;table border=1 width=50 % cellspacing=0 cellpadding=8 bgcolor=Black cols=3&gt;
      &lt;tr bgcolor=White&gt;&lt;td&gt;Virtual Machine&lt;/td&gt;&lt;td&gt;Host Machine&lt;/td&gt;&lt;td&gt;Memory Allocated&lt;/td&gt;&lt;td&gt;DatastoreList&lt;/td&gt;&lt;td&gt;Asset Tag&lt;/td&gt;&lt;td&gt;App Note&lt;/td&gt;&lt;td&gt;App Client ID&lt;/td&gt;&lt;/tr&gt;")
  
  
  $i = 0
  do {
  
          #if($i % 2){$body += "&lt;tr bgcolor=#D2CFCF&gt;&lt;td&gt;$($virtuals[$i].Name)&lt;/td&gt;&lt;/tr&gt;";$i++}
          #else {$body += "&lt;tr bgcolor=#EFEFEF&gt;&lt;td&gt;$($virtuals[$i].Name)&lt;/td&gt;&lt;/tr&gt;";$i++}
          if($i % 2){$body += "&lt;tr bgcolor=#D2CFCF&gt;&lt;td&gt;$($virtuals[$i].Name)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].VMHost)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].MemorymB)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].datastore)&lt;/td&gt;&lt;td&gt;$($etags[$i].value)&lt;/td&gt;&lt;td&gt;$mdesks[$i].notes&lt;/td&gt;&lt;td&gt;$mdesks[$i].ClientID&lt;/td&gt;&lt;/tr&gt;";$i++}
          else {$body += "&lt;tr bgcolor=#EFEFEF&gt;&lt;td&gt;$($virtuals[$i].Name)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].VMHost)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].memorymb)&lt;/td&gt;&lt;td&gt;$($virtuals[$i].datastore)&lt;/td&gt;&lt;td&gt;$($etags[$i].value)&lt;/td&gt;&lt;td&gt;$mdesks[$i].notes&lt;/td&gt;&lt;td&gt;$mdesks[$i].ClientID&lt;/td&gt;&lt;/tr&gt;";$i++}
  }
  
  while ($virtuals[$i] -ne $null)
  
  $body += "&lt;/table&gt;&lt;/center&gt;"
  # Send email.
  if($attachmentPref){
      $virtuals | Export-CSV "Inventory $($date.month)-$($date.day)-$($date.year).csv"
      Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "$vcenterserver Inventory = $countvms" -Body "$body" -Attachments "Inventory $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
      Remove-Item "Inventory $($date.month)-$($date.day)-$($date.year).csv"
  }
  Else{
      Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "Inventory $vcenterserver = $countvms" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
  }
  }
  
  Disconnect-VIServer -Server $vcenterserver -Confirm:$false exit
  
  
  
  get-inventory
  </code></pre>
  
  <p>This returns the information and sends it in an email with columns and rows of the information.  Again, these are two working scripts that just do not return the result that is sought after.  </p>