Add Member to Role with wmi ?

  • How to add a win32 user defined in the sql server to a User defined role with WMI for sql server

    Guess MSSQL_MemberUser would do the trick, but how ? Can someone post a quick example

    Mydomain\User1 as username

    My_Role_Read as role name

    Thx

    Marc

  • This was removed by the editor as SPAM

  • Found the solution to my problem thought I'd share 

                                                         ' MyDB is the target DB

    objRoleName = "My_role_MyDB_r"        ' Role defined in MyDB

    ObjUserName = "NewUser"                 ' User defined in MyDB

    cmdobj = "sp_addrolemember "           ' in case I need looping with variables later on

    Set objLocator = CreateObject("wbemscripting.swbemlocator")

    ' Connect to correct NS

    Set ObjServices = ObjLocator.ConnectServer(".", "root\microsoftsqlserver")

    ' Integrated security version

    ObjServices.security_.impersonationlevel = 3

    ' What DB was that again

    set objService2 = objServices.Get("MSSQL_Database.SQLServerName='(LOCAL)',Name='MyDB'")

    ' Make the cmd usefull

    cmdobj = cmdobj & "'" & objRoleName & "', '" & ObjUserName & "'"

    ' Let us not wait anylonger do it

    objService2.executeimmediate(cmdobj)

    Hope this will help someone else

    Marc

     

  • You can also have a look here:

    http://www.microsoft.com/technet/community/scriptcenter/user/default.mspx

    Not sure if they have your script there, however this should save from reinventing the wheel anew, when it is unnecessary.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

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

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