0

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.

5
  • 1
    Why don't you use EXECUTE SOME_PROC($(db.name))"? It says right there that the property is called "name". ;) Commented Mar 29, 2015 at 21:50
  • It would have to be $Q = "EXECUTE SOME_PROC($($db.Name))" if you were to try that. Accessing Properties inside a string needs to be done in a subexpression. Commented Mar 29, 2015 at 21:51
  • @Frode F. Thank you even more :) Commented Mar 29, 2015 at 21:52
  • Do either of you want credit? Post an answer.. I'll mark it solved Commented Mar 29, 2015 at 21:52
  • 1
    @FrodeF. Go ahead. I'm off for today. Commented Mar 29, 2015 at 21:54

2 Answers 2

3

$db is a datarow-object with a name property. Try a subexpression to extract the value of the name-property:

$Q = "EXECUTE SOME_PROC($($db.Name))"
Sign up to request clarification or add additional context in comments.

Comments

0

What you can do is expand the variable to get just the name instead of an object with a name property and call the stored proc with only the value.

$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

    # So we need to delve into each name separately like so
    ForEach ($dbName in $db.Name)
    {
        # This will call the proc with each name in $db
        $Q = "EXECUTE SOME_PROC($dbName)"
        $Q   
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.