Home Forums Programming Powershell Query to variable - not wanting table style result RE: Query to variable - not wanting table style result

  • Hi Jack, try using Invoke-SqlCmd in place of SQLCMD - it gives the results in a data row format, whereas I think SQLCMD just gives you a string that you won't be able to loop through.

    You can pipe the results straight into a foreach loop, or use the variable like so:

    $SQL = "SELECT DBName, SFTPFolder FROM db.Export.ExportDBs"

    Invoke-SqlCmd -Serverinstance "server" -Username "User" -Password "password" -Query $sql | `

    foreach {code block - use $_.DBName and $_.SFTPFolder to access the values}

    or

    $SQL = "SELECT DBName, SFTPFolder FROM db.Export.ExportDBs"

    $Database = Invoke-SqlCmd -Serverinstance "server" -Username "User" -Password "password" -Query $sql

    foreach ($d in $Database)

    {

    code block - use $d.DBName and $d.SFTPFolder to acces the values

    }

    Hope that helps.

    Cheers

    Gaz