简体   繁体   English

从 Azure SQL InlineSqlTask 任务创建一个 output 变量 azure devops

[英]creating an output variable azure devops from the Azure SQL InlineSqlTask task

I have this in yaml with azure devops:我在 yaml 和 azure devops 中有这个:

- task: SqlAzureDacpacDeployment@1
  displayName: 'Azure SQL InlineSqlTask'
  inputs:
    azureSubscription: 'Service Connection'
    AuthenticationType: servicePrincipal
    ServerName: 'xxx.database.windows.net'
    DatabaseName: 'xxx-Dev'
    deployType: InlineSqlTask
    SqlInline: |
     select name as username,
                  create_date,
                  modify_date,
                  type_desc,
                  authentication_type_desc as authentication_type
                  from sys.database_principals where type_desc = 'EXTERNAL_USER'

I need to be able to get the output of this t-sql command stored in a variable so i can use it later on in my pipeline.我需要能够将此 t-sql 命令的 output 存储在一个变量中,以便稍后在我的管道中使用它。 Any ideas how i get this output and store it in the variable.任何想法我如何得到这个 output 并将它存储在变量中。

I would usually use a powershell command to set the environment variable but obviously this cannot be done within this task.我通常会使用 powershell 命令来设置环境变量,但显然这不能在此任务中完成。

Any ideas would be amazing.任何想法都会很棒。

I have sorted this out.我已经解决了这个问题。 So I can use a powershell script within Azure Devops so i can directly get the variable and then use the setvariable task to assign the output as an env:variable.所以我可以在 Azure Devops 中使用 powershell 脚本,这样我就可以直接获取变量,然后使用 setvariable 任务将 output 分配为 env:variable。

$query = "select name as username,
             create_date,
             modify_date,
             type_desc,
             authentication_type_desc as authentication_type
             from sys.database_principals where type_desc = 'EXTERNAL_USER'"

$clientid = "guid"
$tenantid = "guid"
$secret = "guid"

$request = Invoke-RestMethod -Method POST `
           -Uri "https://login.microsoftonline.com/$tenantid/oauth2/token"`
           -Body @{ resource="https://database.windows.net/"; grant_type="client_credentials"; client_id=$clientid; client_secret=$secret }`
           -ContentType "application/x-www-form-urlencoded"
$access_token = $request.access_token


$sqloutput = Invoke-Sqlcmd -ServerInstance $.database.windows.net -Database db$ -AccessToken $access_token -query $query


Write-Host "##vso[task.setvariable variable=GetVar;]$sqloutput"

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

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