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

Man*_*Rao 8 sql-server powershell azure

我的任务是从我们的 Azure SQL 数据库检索用户列表。我们有数百个 Azure SQL 数据库,我想使用 PowerShell 来加快任务速度。

我正在使用连接字符串(Active Directory 集成)。我相信我能够使用 PowerShell 的连接字符串登录 SQL 数据库。

但是,我在运行 SQL 时遇到错误。下面是代码和异常。请你帮助我好吗?

代码:

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   
}
Run Code Online (Sandbox Code Playgroud)

例外:

异常类型: ManagedBatchParser.ParserException
异常消息:
错误: ManagedBatchParser.ParserException

在 ManagedBatchParser.Parser.Parse()
在 Microsoft.SqlServer.Management.PowerShell.ExecutionProcessor.ExecuteTSql(String sqlCommand)

Tim*_*ude 15

下面的内容对我有用,Connect-AzAccount 通过浏览器触发交互式 AAD 身份验证

示例 11:使用访问令牌连接到 Azure SQL 数据库(或托管实例)

Import-Module SQLServer
Import-Module Az.Accounts -MinimumVersion 2.2.0

# Note: the sample assumes that you or your DBA configured the server to accept connections using
#       that Service Principal and has granted it access to the database (in this example at least
#       the SELECT permission).

### Obtain the Access Token: this will bring up the login dialog
Connect-AzAccount
$access_token = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token

# Now that we have the token, we use it to connect to the database 'mydb' on server 'myserver'
Invoke-Sqlcmd -ServerInstance myserver.database.windows.net -Database mydb -AccessToken $access_token`
              -query 'select * from Table1'
Run Code Online (Sandbox Code Playgroud)

或者获取托管身份访问令牌(来源

$resource = "https://database.windows.net"
$endpoint = $env:IDENTITY_ENDPOINT
$header = $env:IDENTITY_HEADER
$apiVersion = "2019-08-01"

$headers = @{ 'X-Identity-Header' = $header }

$url = "$($endpoint)?api-version=$apiVersion&resource=$resource"

$response = Invoke-RestMethod -Method Get -Uri $url -Headers $headers
$response.access_token
Run Code Online (Sandbox Code Playgroud)


Moh*_*rma -1

尝试下面的方法,看看是否有帮助:

$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
Run Code Online (Sandbox Code Playgroud)

另请参阅以下帖子以供进一步参考:

Azure 自动化中的 Active Directory 密码连接 Azure SQL 失败

  • 我们不想使用用户名和密码。我们希望使用 ADO.NET 连接字符串中的“Active Directory 集成”选项。 (3认同)