SherlockSpreadsheets
SherlockSpreadsheets

Reputation: 2370

Powershell outputs mixed between loops - SQL datasets

I have a PowerShell script that loops through list of 3 servers. A SQL script is run with Invoke-Sqlcmd and the result set is stored to variable $DS. At the end of the loop I return the records with with $DS.Tables.Rows.

But the results sets are getting mixed together. I tried using a Write-Host message to breakup the results. But they are still getting mixed together.

Why are the result getting mixed together in the output?

How can I separate the outputs between each loop?

Thanks

enter image description here

enter image description here

Object type

$DS | gm ............... TypeName: System.Data.DataSet

$DS.Tables | gm ........ TypeName: System.Data.DataTable

$DS.Tables.Rows | gm ... TypeName: System.Data.DataRow

Script

#########################>

# SQL servers
$PCList= @("GCOD139","GCOD039","GCOP039") 
Write-Host ($PCList -join ", ") 

# Query multiple servers
foreach ($PC in $PCList) {

    Write-Host ($PC + "...") -ForegroundColor Yellow

    # SQL parameters
    $Params = @{
        'ServerInstance' = $PC;  
        'Database' = 'master';
    #   'Username' = 'svcBIPOC';
    #   'Password' = 'bipoc2020*';
    #   'InputFile' = "C:\ScriptFolder\TestSqlCmd.sql"
        'Query' = '
            SELECT 
                [Server]= @@SERVERNAME
                --MB to GB
                , REPLACE(name, ''MB'', ''GB'')
                ,[value]= CAST(value as int)/1000
                , [value_in_use]= CAST(value_in_use as int)/1000
                --, value, value_in_use, [description]
            FROM sys.configurations
            WHERE name like ''%server memory%''
            ORDER BY name desc 
            OPTION (RECOMPILE);
        '
    }

    # Capture SQL Dataset
    # (Get-Date).ToSTring('s')  + " SQL query start..."
    $DS = Invoke-Sqlcmd @Params -As DataSet
    #(Get-Date).ToSTring('s')  + " SQL query end..."

    Write-host "-----"
    Write-host "SQL"

    sleep -Seconds 5
    $DS.Tables.Rows
    sleep -Seconds 5

}


#########################

Upvotes: 1

Views: 146

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175065

Stop using Write-Host to convey progress information - use Write-Progress for that instead!

$PCList= @("GCOD139","GCOD039","GCOP039") 
Write-Progress -Activity "Query servers" -Status "About to query: $($PCList -join ", ")"

# Query multiple servers
foreach ($PC in $PCList) {

    Write-Progress -Activity "Query servers" -Status "Querying: $PC"

    # SQL parameters
    $Params = @{
        'ServerInstance' = $PC;  
        'Database' = 'master';
    #   'Username' = 'svcBIPOC';
    #   'Password' = 'bipoc2020*';
    #   'InputFile' = "C:\ScriptFolder\TestSqlCmd.sql"
        'Query' = '
            SELECT 
                [Server]= @@SERVERNAME
                --MB to GB
                , REPLACE(name, ''MB'', ''GB'')
                ,[value]= CAST(value as int)/1000
                , [value_in_use]= CAST(value_in_use as int)/1000
                --, value, value_in_use, [description]
            FROM sys.configurations
            WHERE name like ''%server memory%''
            ORDER BY name desc 
            OPTION (RECOMPILE);
        '
    }

    # Capture SQL Dataset
    $DS = Invoke-Sqlcmd @Params -As DataSet
    $DS.Tables.Rows
}

Write-Progress -Activity "Query servers" -Completed

Now the progress messages won't interfere with the actual output from the function

Upvotes: 1

Related Questions