%:您无法在空值表达式上调用方法

时间:2018-12-06 17:53:35

标签: powershell null

我认为我在PowerShell脚本中无法解释。让我在下面描述问题:

我创建了一个查询VSTS的函数,并将输出包含特定标签的bug数量。

function GetBugsFromVSTS($applicationName, $first_url, $second_url, $query) {
    #Building the body of the HTTP request to VSTS endpoint
    $bodyOfRequest = $query | ConvertTo-Json

    #Building headers of the HTTP request to VSTS endpoint
    $headers = @{
        'Content-Type'='application/json'
        'Accept'='application/json'
        'Authorization' = '' + $vsts_access_token + ''
    }

    $getBugs = Invoke-RestMethod -Uri $first_url `
               -Method Post `
               -Body $bodyOfRequest `
               -Headers $headers | ConvertTo-Json -Depth 100

    $json_decoded = $getBugs | ConvertFrom-Json

    if ($json_decoded.workItems -eq $null) {
        Write-Host "$applicationName Json decoded workitems are equal null" -ForegroundColor Red
    } else {
        Write-Host "$applicationName Json decoded workitems are not null" -ForegroundColor Green
        $json_decoded.workItems | % {
            $bug_id = $_.id
            Write-Host $bug_id
        }
    }

    $json_decoded.workItems | % {
        $bug_id = $_.id
        $url = ($second_url +$bug_id)

        $getVstsBugState = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

        [PSCustomObject]@{
            "Application Name" = $applicationName
            "Id" = $bug_id;
            "State" = $getVstsBugState.fields.'System.State';
            "Reason" = $getVstsBugState.fields.'System.Reason';
            "Severity" = $getVstsBugState.fields.'Microsoft.VSTS.Common.Severity'.Substring(4);
            "AssignedTo" = $getVstsBugState.fields.'System.AssignedTo'.displayName
        }
    }
}

当我尝试运行它时

    $application = "MyBeautifulApp"
    $application_first_url = "https://test.visualstudio.com/72L80E4b-1583-45c1-b669-d8de6133V895/_apis/wit/wiql?api-version=1.0"
    $application_second_url = "https://test.visualstudio.com/72L80E4b-1583-45c1-b669-d8de6133V895/_apis/wit/workItems/"
    $application_query = @{"query" = "SELECT [System.Id],[System.WorkItemType],[System.Title],[System.AssignedTo],[System.State] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] = 'Bug' AND [System.Title] CONTAINS 'Test' AND [System.TAGS] CONTAINS 'Test'"}
    GetBugsFromVSTS $application $application_first_url $application_second_url $application_query
} catch {
    Write-Warning "##vso[task.logissue type=warning;]Some problem occured querying MyBeautifulApp application. Please see below for error details"
    $_
}

这里最有趣的部分是我收到了这个非常奇怪的错误消息

  

警告:## vso [task.logissue type = warning;]发生了一些问题   查询MyBeautifulApp应用程序。请参阅下面的错误   详细信息

     

%:您不能在空值表达式上调用方法。

为了测试,我添加了if..else子句以测试$json_decoded.workItems不等于null,并且验证通过。如果我运行代码,则完整输出为

MyBeautifulApp Json decoded workitems are not null
9805
10330
10331
10371
10372
10373
10374

WARNING: ##vso[task.logissue type=warning;]Some problem occured querying MyBeautifulApp application. Please see below for error details
% : You cannot call a method on a null-valued expression. line 29

+     $json_decoded.workItems | % {
+                               ~~~
    + CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.ForEachObjectCommand

$json_decoded.workItems的第一个验证(第23行)怎么可能不表示变量等于null,而第二条语句(第29行 )表明它为空?

1 个答案:

答案 0 :(得分:1)

问题不是$json_decoded.workItems,而是问题发生在传递给% / ForEach-Object 的脚本块中:

由于您使用的是封闭的try / catch处理程序,因此,不幸的是,错误消息中未明确指出该块内有问题的单个语句。 >

但是,由于您的代码块中仅调用了一个方法,因此可以推断出有问题的行:

"Severity" = $getVstsBugState.fields.'Microsoft.VSTS.Common.Severity'.Substring(4);

换句话说:$getVstsBugState.fields.'Microsoft.VSTS.Common.Severity'的计算结果为$null,这就是.Substring()方法调用失败的原因。