In a Powershell script I have the following code. I am trying to enumerate a list of DB tables from SQL and then use that list to execute subsequent commands.
$q = "SELECT name FROM master..sysdatabases
WHERE name NOT IN ('tempdb', 'model', 'msdb')"
$ListOfDatabases = Invoke-Sqlcmd -ServerInstance $s.ServerName
-Username $s.Username
-Password $s.Pass
-Query $q
foreach ($db in $ListOfDatabases) {
#If I just print $db Like so...
$db
# yields
# name
# -------
# table1
# table2
#But If I Try to use that value I get an error
$Q = "EXECUTE SOME_PROC($db)"
$Q
# yields
# EXECUTE SOME_PROC([System.Data.DataRow])
# EXECUTE SOME_PROC([System.Data.DataRow])
# EXECUTE SOME_PROC([System.Data.DataRow])
# etc... for each table
So where am I going wrong here? I'm trying to extract the string value table1 and table 2, etc for all the tables and use it in another SQL command. And because the SO editor is yelling at me telling me that my post is mostly code, I am adding this additional sentence to help 'convince' it to let me submit this question. -Given that most of my "explaining" is done in the comments of the code.
EXECUTE SOME_PROC($(db.name))"? It says right there that the property is called "name". ;)$Q = "EXECUTE SOME_PROC($($db.Name))"if you were to try that. Accessing Properties inside a string needs to be done in a subexpression.