• Hi DHeath

    If you manage 20+ servers, you might want to look into Powershell. I manage 100 of the blighters, and powershell has made my life a lot easier!

    Check out this script below, which should do what you want. All you need is a textfile AllServers.txt with your server names in:

    SERVERA

    SERVERA\INST

    SERVERB

    ...

    The script (in this case I use a SQL authenticated login, but of course you can use Windows authentication as well):

    EDIT: NB this has only been tested on my local SQL installation!

    cls

    # load assemblies

    [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

    [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum")

    [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")

    [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")

    $userName = "sqldba"

    $password = "P@$$w0rd"

    # file errors will get sent to

    $errorfile = "C:\errors_ChangeDBOwner.txt"

    New-Item -itemType file $errorfile -force | Out-Null

    # file output will get sent to

    $outputfile = "C:\ChangeDBOwner.txt"

    New-Item -itemType file $outputfile -force | Out-Null

    # loop through all SQL Servers

    $servers = Get-Content "c:\AllServers.txt"

    try

    {

    foreach($srvname in $servers){

    $srv = New-Object Microsoft.SqlServer.Management.Smo.Server ($srvname)

    $srv.ConnectionContext.NonPooledConnection = "True"

    # login using SQL authentication, supplying the username and password

    $srv.ConnectionContext.LoginSecure=$false;

    $srv.ConnectionContext.set_Login($userName)

    $srv.ConnectionContext.Password = $password

    $sa = $srv.Logins["sa"]

    foreach($db in $srv.Databases)

    {

    if(!$db.IsSystemObject)

    {

    $db.SetOwner($sa.Name)

    }

    }

    $srv.ConnectionContext.Disconnect()

    }

    }

    catch{

    $err = $Error[0].Exception

    while ( $err.InnerException )

    {

    $err = $err.InnerException

    }

    $srv.Name + " - " + $db.Name + " : " + "TRAPPED: " + $err.Message | Out-File -append -filePath $errorfile

    $err

    }

    I use scripts based on this template for all sorts of handy things - checking for failed jobs on all servers, bringing back information on database settings for all servers/databases...

    It's well worth a look, IMO

    ------------------------------------------------------------------------
    Bite-sized fiction (with added teeth) [/url]