September 24, 2019 at 2:16 pm
Hello,
I have 2 files:
1. file: ACL.sql script in SQL
2. file .: instances_list.txt, where I have a listing of SQL instances where I want to apply ACL.sql
I need to use SQLCMD to load instances from instancelist.txt and apply ACL.sql
Will you help me, please?
Thank You!
Mark2F
September 24, 2019 at 4:58 pm
with powershell, here is how i would do it.
Assuming instances_list.txt is a single column list with no header.
also assuming the script has USE [somedb] or fully qualified names so it can execute form the master database.
$AllServersToScan = Get-Content -Path 'C:\Data\instances_list.txt'
$sqlfile = 'C:\Data\ACL.sql'
$LogPath = 'C:\Data\ACL_Log.txt'
foreach($SQLServer in $AllServersToScan)
{
try
{
$StagingFileName = "--Begin " + [System.IO.Path]::GetFileName($sqlfile) + ' on ' + $SQLServer
Write-Host $StagingFileName -ForegroundColor Green
Out-File -LiteralPath $LogPath -InputObject $StagingFileName -Append -Encoding ASCII -Width 99999
Invoke-SqlCmd -InputFile $sqlfile -ServerInstance $SQLServer -Database master -Querytimeout 0 | Out-File -LiteralPath $LogPath -Append -Encoding ASCII -Width 99999
$StagingFileName = "--End " + [System.IO.Path]::GetFileName($sqlfile)
Out-File -LiteralPath $LogPath -InputObject $StagingFileName -Append -Encoding ASCII -Width 99999
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$StagingFileName = "--Error " + [System.IO.Path]::GetFileName($sqlfile) + ' on ' + $SQLServer
Out-File -LiteralPath $LogPath -InputObject $FailedItem -Append -Encoding ASCII -Width 99999
Out-File -LiteralPath $LogPath -InputObject $ErrorMessage -Append -Encoding ASCII -Width 99999
Out-File -LiteralPath $LogPath -InputObject $StagingFileName -Append -Encoding ASCII -Width 99999
}
}
Lowell
September 24, 2019 at 5:11 pm
Unfortunately the policy of the organization banned Powershell. That's what I suggested first.
Your solution looks effective.
Thank you.
September 24, 2019 at 6:00 pm
then in that case, why don't you simply put all of the servers in Central Management Servers, and execute then via SSMS all together?
stick with the tools you know, isntead of sqlcmd , a programming language or powershell.
Lowell
October 7, 2019 at 1:18 pm
FOR /F "tokens=1 delims=," %%G IN (dblist.csv) DO FOR /F "tokens=1 delims=," %%G IN (dblist.csv) DO SQLCMD -S %%G -i "acl.sql" -m 12 -E -o "protocol_output.txt"
How do I make a change, but the protocol (-o "protocol_output.txt") has grown (=> appending)?
October 7, 2019 at 3:11 pm
Your last sentence doesn't quite make sense. You can just let the output come back and use redirects >>, to put the data into a file.
October 8, 2019 at 10:45 am
Hi,
FOR /F "tokens=1 delims=," %%G IN (instanceslist.csv) DO FOR /F "tokens=1 delims=," %%G IN (instanceslist.csv) DO SQLCMD -S %%G -i "acl.sql" -m 12 -E -o "protocol_output.txt"
If I have 5 database instances, I have 5 log entries for each, so a total of 25 log entries.
How to make each instance in the instances list be processed only once?
Best Regards,
M2F
October 15, 2019 at 8:07 am
Hi All,
please,
How do I make SQLCMD retrieve any SQL file name using –i? (* .sql) If there will always be only 1 file *.sql in the directory?
Thx!
M2F
October 15, 2019 at 4:15 pm
Try this in a file, edit the SQLCMD to match what you need.
@@echo off
setlocal EnableDelayedExpansion
for %%x in (*.sql) do (
set source=%%x
echo sqlcmd -E -i !source!
)
FWIW, banning PoSh is a little crazy, since MS has invested heavily in this and provides lots of interfaces for their products. This is really the future, and I might ask someone to reconsider.
October 16, 2019 at 12:42 pm
It works, thank you very much Steve!
October 16, 2019 at 5:08 pm
Glad it works. You are welcome and hope this solves your issue.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply