I'm trying to query a database, get a value, and then use that value with a string with Export-Csv
. However, while I can see that my variable is being populated when I'm trying to concat with strings it changes to System.Data.DataRow
Example Code:
$Server = "TestServer"
$Database = "msdb"
$ID = 1
while ([int]$ID -le 4) {
$DatabaseNameQuery = "select Name from Sys.Databases Where Database_ID = $ID"
$DatabaseName = Invoke-Sqlcmd -Query $DatabaseNameQuery -Database $Database -ServerInstance $Server
$FilePath = "C:\$DatabaseName.csv"
$DatabaseName
$FilePath
#Invoke-Sqlcmd -Query $ResultsQuery -Database $Database -ServerInstance $Server | Export-Csv $FilePath
$ID++
}
My actual code will be doing other stuff, getting data back and writing it to the files, I'm just struggling in dynamically naming the files.