1

I'm trying to run a sql query via PowerShell and return the results in a table-like format.

It's putting multiple results in one field. I suspect there's something wrong with the 'foreach' loops. What am I missing, please?

enter image description here

To use the code below, just change the server names from 'server1'/'server2' for your sql server instances.

$query = "
 SELECT   @@SERVERNAME     AS ServerName       
                   , (SELECT DB_NAME ())              AS DBName
                   , s.name                                        AS SchemaName
                   , st.name                                       AS TableName
                   , RIGHT(st.name, 8)                    AS Rgt8
                   , TRY_CONVERT(DATE, RIGHT(st.name, 8), 103) AS Rgt8Date
                   
                   FROM        sys.tables  AS st
                   INNER JOIN  sys.objects AS so ON so.object_id = st.object_id
                   INNER JOIN  sys.schemas AS s ON s.schema_id = st.schema_id
                   "
$instanceNameList = @('server1', 'server2')
$report = @()
 
foreach ($instanceName in $instanceNameList) {
    write-host "Executing query against SERVER/INSTANCE: " $instanceName        
    $dbNames = Invoke-DbaQuery -SqlInstance $InstanceName -Database "master" -Query "select name from sys.databases where database_id > 4 and name <> 'TEST'"          

    foreach ($database in $dbNames.Name ) {
        Write-host -Activity "Current DATABASE $database" -Status "Querying: $database"                               
        $results = Invoke-DbaQuery -SqlInstance $InstanceName -Database $database -Query $query 

        # <#          
        if ($results -is [array]) {
            $CustomObject = [pscustomobject]  @{
                ServerName           = $results.ServerName
                DBName                 = $results.DBName
                SchemaName        = $results.SchemaName
                TableName             = $results.TableName
                Rgt8                         = $results.Rgt8
                Rgt8Date                 = $results.Rgt8Date
                OverOneYearOld   = $results.OverOneYearOld         
                Drop_Table             = $results.Drop_Table                           
            }
        ## ADDING EACH ROW/JOB OBJECT THAT HAS BEEN REPORTED, TO THE REPORT ARRAY       
        $report += $CustomObject      
        }        
    } 
}
          
$report | Select-Object ServerName, DbName, TableName | Out-GridView 
 
2
  • if $results -is [array] then you should loop over it :) you should also be able to use Select-Object Commented Feb 4, 2022 at 20:20
  • 2
    += kills puppies. Commented Feb 4, 2022 at 20:24

1 Answer 1

1

Basically, you're doing the opposite of what you wanted to do, if $results -is [array] you want to iterate over it instead of add it as is to to your $report array.

On the other hand, adding elements to a fixed collection (+=) is a terrible idea.

$dbquery = @'
select name from
sys.databases
where database_id > 4 and name <> 'TEST'"
'@

$result = foreach ($instanceName in $instanceNameList) {
    $params = @{
        SqlInstance = $InstanceName
        Database = "master"
        Query = $dbquery
    }
    $dbNames = Invoke-DbaQuery @params

    foreach ($database in $dbNames.Name) {
        $params.Database = $database
        $params.Query = $query
        Invoke-DbaQuery @params | Select-Object @(
            'ServerName'
            'DBName'
            'SchemaName'
            'TableName'
            'Rgt8'
            'Rgt8Date'
            'OverOneYearOld'
            'Drop_Table'
        )
    }
}

$result | Format-Table
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Santiago Squarzon!
@JM1 my pleasure, happy to help. looking back at my code, if those are the exact properties you're pulling from your DB, the Select-Object statement might not be even needed at all

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.