Loop All SQL Instances

  • I need to execute sp_helpsrvrolemember 'sysadmin' to about 100+ SQL instance. Does anyone has script that I can use to generate output to a file?

    Thank you in advance.

  • Create text file srvList.txt with all your instances names (each line is new instance). Then run this from command line:

    FOR /F %a IN (srvList.txt) DO osql -S %a -E -d master -I "EXEC sp_helpsrvrolemember 'sysadmin' " -n -h-1 -s , -w 1000 >> "output_SrvRoleMember.txt"

  • Thank you, below is the error from output file:

    usage: osql [-U login id] [-P password]

    [-S server] [-H hostname] [-E trusted connection]

    [-d use database name] [-l login timeout] [-t query timeout]

    [-h headers] [-s colseparator] [-w columnwidth]

    [-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]

    [-L list servers] [-c cmdend] [-D ODBC DSN name]

    [-q "cmdline query"] [-Q "cmdline query" and exit]

    [-n remove numbering] [-m errorlevel]

    [-r msgs to stderr] [-V severitylevel]

    [-i inputfile] [-o outputfile]

    [-p print statistics] [-b On error batch abort]

    [-X[1] disable commands [and exit with warning]]

    [-O use Old ISQL behavior disables the following]

    <EOF> batch processing

    Auto console width scaling

    Wide messages

    default errorlevel is -1 vs 1

    [-? show syntax summary]

  • Sorry, I misstyped it:

    FOR /F %a IN (srvList.txt) DO osql -S %a -E -d master -Q "EXEC sp_helpsrvrolemember 'sysadmin' " -n -h-1 -s , -w 1000 >> "output_SrvRoleMember.txt"

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply