1

我正在尝试通过 PowerShell 运行 sql 查询并以类似表格的格式返回结果。

它将多个结果放在一个字段中。我怀疑“foreach”循环有问题。请问我错过了什么?

在此处输入图像描述

要使用下面的代码,只需将服务器名称从 'server1'/'server2' 更改为您的 sql server 实例。

$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 
 
4

1 回答 1

1

基本上,如果$results -is [array]您想迭代它而不是将其按原样添加到您的$report数组中,那么您所做的与您想做的相反。

另一方面,将元素添加到固定集合 ( +=) 是一个糟糕的主意。

$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
于 2022-02-04T20:38:41.567 回答