Connect Any Database equivalent for Pre-SQL2014. EnumMembers() failure.

  • Hi guys,
    Thought this could lie in the Connecting, Powershell and SMO sections among others and decided to post here in case anyone had encountered the same problem.

    I'm relatively new to SQL, and getting role members on database servers with an SMO and EnumMembers().
    Everything works fine with my admin account but been trying to move to a service account and hitting problems. On my 2014 servers I have a serverlevel role I've created that's granting "Connect Any Database" to the service account, that allows it to connect to current and future databases. Haven't been able to see inside the created objects yet as errors out on my 2008r2 server with: 

    The server principal "serviceAccount" is not able to access the database "database" under the current security context. '. Process Exit Code -1. The step failed.

    This I understand to be because it can't connect as has a server login but doesn't have a database login. Short of manually adding a login to each database or giving a high level of privilege any advice ? (Trying to keep the permissions I give to the service account as low as possible)

    For anyone who might be interested, a portion of my code:

    $databaseList = $svrConnection.Databases | WHERE {$_.IsSystemObject -eq $false}
    foreach ($db in $databaseList)
    {
      $dbRolesList = $db.Roles # get roles for the database
      foreach ($dbRole in $dbRolesList)
      {
       #Get all role members and process them
       $roleN = $dbRole.Name
       $dbRoleMemberList = $dbrole.EnumMembers()
       if($dbRoleMemberList)
       {
        Process-Members -listOfMembers $dbRoleMemberList -DatabaseName $db.Name -roleName $roleN
       }
      }
    }

Viewing 0 posts

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