%:不能对空值表达式调用方法



我觉得我在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.logisue type=WARNING;]出现了一些问题查询MyBeautifulApp应用程序。请参阅以下错误信息详细信息

%:不能对空值表达式调用方法。

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

MyBeautifulApp Json解码的工作项不为空9805103301033110371103721037310374警告:##vso[task.logisue type=WARNING;]查询MyBeautifulApp应用程序时出现一些问题。请参阅下面的错误详细信息%:不能对空值表达式调用方法。第29行+$json_edecoded.workItems|%{+~~~+CategoryInfo:InvalidOperation:(:([ForEach Object],RuntimeException+FullyQualifiedErrorId:InvokeMethodOnNull,Microsoft。PowerShell。命令。ForEachObjectCommand

为什么$json_decoded.workItems的第一次验证(第23行(不指示变量等于null,而第二条语句(第29行>(显示变量为null?

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

由于您使用的是一个封闭的try/catch处理程序,因此不幸的是,块内有问题的单个语句在错误消息中没有被精确定位。

然而,考虑到块中只有一个方法调用,可以推断出有问题的行:

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

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

最新更新