Error when adding more servers to execute query in Powershell

Mario2286 441 Reputation points
2023-08-22T09:52:50.3033333+00:00

I m able to execute this powershell scripts when i just put a single server but when i add two servers or more , i m getting error as below . Does anyone have idea on this ? Fyi, this sql query will select data from a multiple database which have same table and insert into another table in different server

User's image

$tableName = "dbo.a_category"
$tableName2 = "dbo.b_category"
$SQLServer ="DB01","DB02"

$databases = invoke-sqlcmd -ServerInstance $SQLServer -Database "master" -Query "select name from sys.databases where name like '%cms%'"

foreach ($database in $databases)
{
    $DBname = $database.name
    #Execute scripts
    $dataset = Invoke-DbaQuery -SqlInstance $SQLServer -Database $database.name -file "C:\queryset\copy.sql" -As DataSet
$dataset | Write-DbaDbTableData -SqlInstance "DB15" -Database testdb -Table $tableName2

    $database.name
}
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,922 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue-MSFT 41,716 Reputation points Microsoft External Staff
    2023-08-23T02:01:48.44+00:00

    Hi,

    According to the help file of the Invoke-Sqlcmd cmdlet, the "-ServerInstance" parameter only accepts a PSObject, not an array.

    Please refer to this link for more detals.

    Invoke-Sqlcmd

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.