0% found this document useful (0 votes)
102 views

Powershell - Execute SQL Script For Multiple Instances

This script retrieves a list of SQL Server instances from a text file, runs a query against each instance to get the server name and SQL Server version, and outputs the results in a formatted table. It creates a SMO Server object for each instance, runs Invoke-Sqlcmd against the instance to execute the query on the master database, and pipes the results to Format-Table for output.

Uploaded by

Ajay Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Powershell - Execute SQL Script For Multiple Instances

This script retrieves a list of SQL Server instances from a text file, runs a query against each instance to get the server name and SQL Server version, and outputs the results in a formatted table. It creates a SMO Server object for each instance, runs Invoke-Sqlcmd against the instance to execute the query on the master database, and pipes the results to Format-Table for output.

Uploaded by

Ajay Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

$instances = Get-content "C:\Backup\sqlinstances.

txt"
$query = "SELECT @@SERVERNAME 'SERVERNAME', @@VERSION 'VERSION'"
$databasename = "master"
$instances |
ForEach-Object {
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server
-ArgumentList $_
Invoke-Sqlcmd -ServerInstance $_ -Database $databasename -Query $query
} | format-table -auto

You might also like