Sqlserver-dba.com

Powershell : SQLCMD and invoke-expression

There was a requirement to add a SQL Server login to a group of SQL Servers.
The SQL Server login was to be uniform across all the servers. I tend to use SQLCMD for most
DBA tasks - and as my account is Windows based and with high level of privileges , I didn't
have to worry about supplying a different username / password for every SQL Serve instance.

This powershell script is very simple , iterating through a list of SQL Server instances which are
stored in a text file . For every instance a SQLCMD is run . This is done through the "invoke-expression" call.
This allows the DBA to maintain their sql scripts separately , which can be executed against any number of instances

 

-----------------CODE STARTS---------------------------

foreach ($svr in get-content "C:\MyInstances.txt"){
    $svr
    invoke-expression "SQLCMD -E -S $svr -i createMyuser.sql"
   
}


-------------CODE FINISHES----------------

Author: Jack Vamvas (http://www.sqlserver-dba.com)

Author: Jack Vamvas (http://www.sqlserver-dba.com)

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact