在我的PowerShell代码中,我有:
$retrieveSourceBranchFromBuildURL = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId" + "?api-version=5.0"
当我在浏览器中导航到URL时,会看到:
{
_links: {
self: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46070"
},
web: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_build/results?buildId=46070"
},
sourceVersionDisplayUri: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/sources"
},
timeline: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/Timeline"
},
badge: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/status/67"
}
},
parameters: "{"system.pullRequest.pullRequestId":"5766","system.pullRequest.sourceBranch":"refs/heads/pb/31333-test-branch/name","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"1cf19b95a59478a8554c2c03d65dcefe203529a6","system.pullRequest.sourceRepositoryUri":"https://something@dev.azure.com/something/something%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"1"}",
}
$buildInformation
变量:
$buildInformation = Invoke-RestMethod -Uri $retrieveSourceBranchFromBuildURL -Headers @{Authorization = $pat } -Method Get -ContentType 'application/json'
当我输出$buildInformation
的值时,我得到:
@{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=46079; buildNumber=20190624.3; status=completed; result=succeeded; queueTime=2019-06-24T07:57:05.7271255Z; startTime=2019-06-24T07:57:12.8021227Z; finishTime=2019-06-24T08:12:27.003113Z; url=https://dev.azure.com/embrace/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46079; definition=; buildNumberRevision=3; project=; uri=vstfs:///Build/Build/46079; sourceBranch=refs/pull/5740/merge; sourceVersion=735f7a813c343dbbca5e1d1b3e966e0bad1db762; priority=normal; reason=pullRequest; requestedFor=; requestedBy=; lastChangedDate=2019-06-24T08:14:41.11Z; lastChangedBy=; parameters={"system.pullRequest.pullRequestId":"5740","system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276","system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"5"}; orchestrationPlan=; logs=; repository=; keepForever=False; retainedByRelease=True;
当我输出$buildInformation.parameters
的值时:
Write-Host $buildInformation.parameters
{
"system.pullRequest.pullRequestId":"5740",
"system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team",
"system.pullRequest.targetBranch":"refs/heads/master",
"system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276",
"system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web",
"system.pullRequest.pullRequestIteration":"5"
}
但是当我尝试读取一个值时:
Write-Host $buildInformation.parameters.system.pullRequest.sourceBranch
输出为空。
那我该如何检索parameters.system.pullRequest.sourceBranch
值?
答案 0 :(得分:4)
要访问名称中带有“特殊字符”的属性,请将参数名称用引号引起来,如下所示:
$buildInformation.parameters.'system.pullRequest.sourceBranch'
注意:这里的窍门是parameters
的值在双引号内。因此,您可能无法以常规方式访问parameters
下的属性。要解决此问题,您可以直接在parameters
上进行转换:
$convertedParams = $buildInformation.parameters | ConvertFrom-Json
# Access desired property
$convertedParams.'system.pullRequest.sourceBranch'
当您尝试访问属性时
$buildInformation.parameters.system.pullRequest.sourceBranch
JSON的结构应如下所示:
PS> $buildInformation = '{
"parameters": {
"system": {
"pullRequest":{
"sourceBranch": "refs/heads/master-md/social-create-team"
}
}
}
}' | ConvertFrom-Json
# Checking output
PS> $buildInformation.parameters.system.pullRequest.sourceBranch
refs/heads/master-md/social-create-team