Return SQL Query as Array in Powershell
arrays, powershell, sql-server, sql-server-2008
Solution
I'd do this:
$DBNameList = @(Invoke-SQLCmd -query "select Name from sysdatabases" -Server DEVSQLSRV) | select-object -expand Name
That will give you an array of names.The option `-contains` should work fine.
Problem
I have a SQL 2008 Ent server with the databases "DBOne", "DBTwo", "DBThree" on the server DEVSQLSRV. Here is my Powershell script: ``` $DBNameList = (Invoke-SQLCmd -query "select Name from sysdatabases" -Server DEVSQLSRV) ``` This produces my desired list of database names as: ``` Name ----- DBOne DBTwo DBThree ``` I has been my assumption that anything that is returned as a list is an Array in Powershell. However, when I then try this in Powershell: ``` $DBNameList -contains 'DBTwo' ``` It comes back has "False" instead of "True" which is leading me to believe that my list is not an actual array. Any idea what I'm missing here? Thanks so much! Emo