How to give a user SQL permission to execute stored procedure using powershell invoke-sqlcmd

Craig Garland 336 Reputation points
2025-06-05T22:39:26.3333333+00:00

Hi All,

Hope someone has a solution for me.

I have a MS SQL 2016 server. I have created a stored procedure and set execute as to Owner.

I would like to have standard user be able to run this stored procedure using the PowerShell command invoke-sqlcmd. Currently the powershell command works if the user has owner rights to the DB.

I cannot get it working for the standard user that only has exec rights to the stored procedure and public access to the DB. I keep getting the error logon failed for user XXX

The reason to restrict access is a task that needs to be run by first level helpdesk and currently, they need to wait for resource from our SQL Admin to complete the task.

Does anyone know how to set the permissions to allow a user to run a stored procedure without giving read / Write / Owner permissions to the DB.

Thanks for your time.

Craig

SQL Server Database Engine
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.3K Reputation points MVP Volunteer Moderator
    2025-06-06T08:49:15.05+00:00

    I keep getting the error logon failed for user XXX

    Then you need to grant the user access to the server itself. Once you have done that, this is sufficient:

    only has exec rights to the stored procedure and public access to the DB.

    So to summarise, what you need is:

    CREATE LOGIN "DOMAIN\Helpdesk" FROM WINDOWS
    go
    USE YourDB
    go
    CREATE USER "Domain\Helpdesk" 
    GRANT EXECUTE ON YourSP TO "Domain\Helpdesk"
    

    Note that Helpdesk here can be a Windows user or a Windows group. I very much recommend the latter, as helpdesk people are likely to come and go.

    As for the use of EXECUTE AS OWNER, a better technique is to use certificate signing, as EXECUTE AS OWNER disrupts several auditing scheme. But if you don't care about that, you can use it, if it is only a matter of operations on database level. But if there are also operations also on server level, certificate signing is the only option.


1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,416 Reputation points
    2025-06-06T04:55:01.7533333+00:00

    It doesn't matter, which tool you are using: SSMS, Azure Data Studio or PowerShell.

    All are issuing plain T-SQL commands against the database engine.

    And in this case it's a GRANT PERMISSION command, see

    https://learn.microsoft.com/en-us/sql/t-sql/statements/grant-transact-sql?view=sql-server-ver17

    Additional: Have a look at https://github.com/dataplat/dbatools

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.