用于列出用户,角色并将用户添加到Azure SQL DB的PowerShell脚本

时间:2019-01-10 13:05:24

标签: sql azure powershell

请帮助我为在Azure SQL DB上创建用户创建PowerShell脚本,并显示数据库中的列表角色和用户。 谢谢。

1 个答案:

答案 0 :(得分:0)

这是PowerShell脚本演示。

您可以通过修改$SqlCmd.commandtext语句来实现所需的功能。

$Database   = 'Database'
$Server     = '"servername.database.windows.net"'
$UserName   = 'username'
$Password   = 'password'

$SqlConn = New-Object System.Data.SqlClient.SqlConnection

$SqlConn.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = False; User ID = $uid; Password = $pwd;"

$SqlConn.open()

#Execute Statement Method One
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.connection = $SqlConn
$SqlCmd.commandtext = 'delete top(1) from dbo.B'
$SqlCmd.executenonquery()

#Execute Statement Method Two
$SqlCmd = $SqlConn.CreateCommand()
$SqlCmd.commandtext = 'delete top(1) from dbo.B'
$SqlCmd.ExecuteScalar()

#Execute Statement: Query Disply
$SqlCmd.commandtext = 'select name,recovery_model_desc,log_reuse_wait_desc from sys.databases'
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$set = New-Object data.dataset
$SqlAdapter.Fill($set)
$set.Tables[0] | Format-Table -Auto

#Close The Connection
$SqlConn.close()

关于创建Azure Databsae用户的声明,您可以看到here

希望这可以为您提供帮助。