如何仅使用管道名称检查数据工厂管道运行的当前状态?

How to check current status of a Data Factory pipelinerun with just Pipeline name?

是否可以通过 Powershell 或 API 仅使用管道名称检查 Azure 数据工厂管道运行 的当前状态?

我看到您可以使用 Get-AzDataFactoryV2PipelineRun,但这需要您拥有管道运行id。

我的目标是构建一个脚本,该脚本将首先检查管道 运行 是否正在 运行ning,如果没有则触发它。我想避免脚本触发 pipeline运行 这样就会同时有多个 pipeline 运行s 运行ning.

我已经用 PowerShell 完成了,虽然我确信有更好的方法,但这是我想出的解决方案。

它基本上获取了从一周前到今天的所有管道 运行 id,遍历每一个,如果它找到一个 "InProgress",它不会做任何事情。如果没有管道 运行 id 处于该状态,则执行它。

您显然需要在 运行 之前进行身份验证:

$before = get-date
$after = (get-date).AddDays(-7)
$runIds = Get-AzDataFactoryV2PipelineRun -DataFactoryName $DataFactoryName -ResourceGroupName $ResourceGroupName -LastUpdatedAfter $after -LastUpdatedBefore 
$before
$shouldRun = 1

$runIds | ForEach-Object {
    ## Check for all statuses
    if($_.Status -eq "InProgress"){
        $shouldRun = 0
    }
}

if($shouldRun){
    ## Logic to run pipeline, this is just an example.
    Invoke-AzDataFactoryV2Pipeline -PipelineName $PipelineName -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName
} 

使用.Net Core SDK

ServiceClientCredentials cred = new TokenCredentials(accessToken);
using (var client = new DataFactoryManagementClient(cred) { SubscriptionId = subscriptionId })
{
    RunQueryFilter filter1 = new RunQueryFilter("PipelineName", "Equals", new List<string> { "ActualPipelineName" });
    RunQueryFilter filter2 = new RunQueryFilter("Status", "Equals", new List<string> { "Queued" });
    DateTime before = DateTime.UtcNow;
    DateTime after = before.AddHours(-4);
    RunFilterParameters param = new RunFilterParameters(after, before, null, new List<RunQueryFilter> { filter1, filter2 }, null);
    PipelineRunsQueryResponse pipelineResponse = client.PipelineRuns.QueryByFactory(resourceGroupName, azureDataFactoryName, param);
    int? QueuedPipelines = pipelineResponse?.Value?.Count;
}

在过滤器 1 中,您可以使用 "In" 运算符来查询多个 PipelineName。

在 filter2 中,您可以使用 ADF 的任何有效状态(成功、进行中、排队等)

同样的事情也可以使用 REST 实现 API。

P.S- 如果管道数超过 100(对于该特定状态),您可能需要使用 Continuation 令牌。