如何使用PowerShell修改SQL登录权限?

2022-08-14 00:00:00 powershell powershell-3.0 sql-server

我已经使用PowerShell中的Microsoft.SqlServer.Management.Smo.Login创建了SQL-LOGIN,没有任何问题。

仅针对创建的新SQL登录,我如何使用PowerShell设置其权限,如下图所示?(我要将"查看任何数据库"设置为"拒绝")。

谢谢!


解决方案

#Valid serverpermissions:
#AdministerBulkOperations AlterAnyConnection AlterAnyCredential AlterAnyDatabase AlterAnyEndpoint AlterAnyEventNotification
#AlterAnyLinkedServer AlterAnyLogin AlterAnyServerAudit   AlterResources AlterServerState AlterSettings AlterTrace AuthenticateServer
#ConnectSql ControlServer CreateAnyDatabase CreateDdlEventNotification CreateEndpoint CreateTraceEventNotification ExternalAccessAssembly
#UnsafeAssembly ViewAnyDatabase ViewAnyDefinition ViewServerState 

    #Set variables
    $ServerInstance = ""
    $name = ""
    $action = ""

    $server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerInstance

        $perm = new-object ('Microsoft.SqlServer.Management.Smo.ServerPermissionSet')
        $perm.$($permission.ToString()) = $true

        switch ($action)
        { 
            'Grant'  { $server.Grant($perm,$name) }
            'Deny'   { $server.Deny($perm,$name) }
            'Revoke' { $server.Revoke($perm,$name) }
        }

相关文章