简体   繁体   English

使用 Z3D265B4E1EEB18CCZ0 对 Azure SQL 数据库运行 SQL 查询

[英]Run SQL query against Azure SQL database using PowerShell

My task is to retrieve the users list from our Azure SQL databases.我的任务是从我们的 Azure SQL 数据库中检索用户列表。 We have 100s of Azure SQL database and I want to use PowerShell to make the task fast.我们有 100 个 Azure SQL 数据库,我想使用 PowerShell 来快速完成任务。

I am using connection string (Active Directory Integrated).我正在使用连接字符串(Active Directory 集成)。 I believe I am able to login to the SQL database using the connection string with PowerShell.我相信我能够使用带有 PowerShell 的连接字符串登录到 SQL 数据库。

However, I am getting an error while running the SQL.但是,运行 SQL 时出现错误。 Below is the code and exception.下面是代码和异常。 Could you please help me?请你帮助我好吗?

Code:代码:

try {
    $new = 'Server=tcp:dummy.database.windows.net,1433;Authentication="Active Directory Integrated";Initial Catalog=xtoiteuitbdbsqldb01;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;'
    $sql = "SELECT * FROM sys.database_principals"
  $temp_result_object = invoke-sqlcmd -ConnectionString $new -Query $sql
} catch {
  "error when running sql $sql"
  write-host "Exception type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    write-host "Exception message: $($_.Exception.Message)" -ForegroundColor Red
    write-host "Error: " $_.Exception -ForegroundColor Red   
}

Exception:例外:

Exception type: ManagedBatchParser.ParserException异常类型:ManagedBatchParser.ParserException
Exception message:异常消息:
Error: ManagedBatchParser.ParserException错误:ManagedBatchParser.ParserException

at ManagedBatchParser.Parser.Parse()在 ManagedBatchParser.Parser.Parse()
at Microsoft.SqlServer.Management.PowerShell.ExecutionProcessor.ExecuteTSql(String sqlCommand)在 Microsoft.SqlServer.Management.PowerShell.ExecutionProcessor.ExecuteTSql(字符串 sqlCommand)

You can use the .NET SqlClient provider in order to use either password or "Active Directory Integrated" authentication, eg:您可以使用 .NET SqlClient提供程序来使用密码或“Active Directory Integrated”身份验证,例如:

# connect to database
$dbConn = New-Object System.Data.SqlClient.SqlConnection
$dbConn.ConnectionString = "Server=tcp:dummy.database.windows.net,1433;Database=DummyDB;Authentication=Active Directory Integrated;Encrypt=True;"
$dbConn.Open()

# construct command
$dbCmd = New-Object System.Data.SqlClient.SqlCommand
$dbCmd.Connection = $dbConn
$dbCmd.CommandText = "SELECT * FROM sys.database_principals"

# fetch all results
$dataset = New-Object System.Data.DataSet
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter
$adapter.SelectCommand = $dbCmd
$adapter.Fill($dataset)

# display results
$dataset.Tables | Format-Table

Try something like below and see if it helps:试试下面的方法,看看它是否有帮助:

$Svr = "SVR"
$SvrPort = 10003
$Username = "ABC\sql"
$Password = "ABC#@!"

$Password = $Password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Inst = $Svr + "," + $SvrPort

Get-PSSession

New-PSSession -ComputerName $Svr -Credential $Cred

Invoke-Sqlcmd -ServerInstance $Inst -Database "master" -Query "SELECT @@SERVERNAME AS SERVER_NAME"

Get-PSSession

Get-PSSession | Remove-PSSession

Invoke-Sqlcmd -ServerInstance $Inst -Database "master" -Query "SELECT @@SERVERNAME AS SERVER_NAME"

Get-PSSession

Also refer to below post for further reference:另请参阅以下帖子以供进一步参考:

Active Directory Password Connection Azure SQL Failure from Azure Automation Active Directory 密码连接 Azure SQL Azure 自动化失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM