Include more servers in Powershell scripts

Mario2286 441 Reputation points
2021-06-04T10:55:23.88+00:00

I m new to Powershell and I need guidance on this Powershell scripts below. I m working in multi tenant environment. I need to execute a scripts on 15 servers and every servers have around 20 databases. This scripts help me a lot to execute a scripts in all database and output the results for every single database. I need to include more servers in $instancelist but when I add more servers it does not work. I need help to include more servers and this scripts must execute in all servers in $instancelist and the the output file must have server name, currently the output file have $path name, database name.log, it must have server name if I m able to include more servers.

One more thing, can I include BCP in this powershell scripts to output the results since it will come out with millions of rows

$InstanceList = "DESKTOP-6U9IKQD" $filepath = "C:\b1\script1.sql" $path = "C:\b1\jack"

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

foreach ($database in $databases) { $DBname = $database.name # Outputs one file per database $outfile = $path + "."+$database.name+".log" #Execute scripts Invoke-Sqlcmd -ServerInstance ${InstanceList} -Database $database.name -InputFIle $filepath | out-file -filepath $outfile

$database.name
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-06-06T21:36:58.86+00:00

    You need to change this line (#13 in my previous answer) from this:

    Invoke-Sqlcmd -ServerInstance ${InstanceList} -Database $database.name -InputFIle $filepath | 
    

    To this:

    Invoke-Sqlcmd -ServerInstance $_ -Database $database.name -InputFIle $filepath | 
    

    Sorry I missed that in my reply, :-(

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-06-05T19:07:59.53+00:00

    This might be what you're looking for:

    $InstanceList = "DESKTOP-6U9IKQD", "DESKTOP-another", "DESKTOP-andonemore"
    $filepath = "C:\b1\script1.sql" 
    $basename = "C:\b1\jack"
    
    $InstanceList |
        ForEach-Object{
            $databases = invoke-sqlcmd -ServerInstance $_ -Database "master" -Query "select name from sys.databases where name like 'adventureworks%'"
    
            foreach ($database in $databases) {
                # Outputs one file per database
                $outfile = $basename, $database.name, "log" -join "."
                #Execute scripts 
                Invoke-Sqlcmd -ServerInstance ${InstanceList} -Database $database.name -InputFIle $filepath | 
                    out-file -filepath $outfile
    
                $database.name
            }
    }
    

    BCP is just a command line executable so you can run it from your script.


  2. Mario2286 441 Reputation points
    2021-06-06T15:23:01.707+00:00

    Still the same error

    102724-image.png


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.