2016-09-08 66 views
-1

我正在使用System Center Orchestrator和Powershell爲JIRA設置自動化進程。在這個例子中,我已經有了來自JIRA Rest API的原始JSON數據。使用Powershell從JIRA Rest API解析問題密鑰

function ConvertFrom-Json20([object] $item){ 
    add-type -assembly system.web.extensions 
    $ps_js=new-object system.web.script.serialization.javascriptSerializer 

    #The comma operator is the array construction operator in PowerShell 
    return ,$ps_js.DeserializeObject($item) 
} 

[object]$JSON = '{Raw JSON Data from JIRA Variable}' 

$results = ConvertFrom-Json20($JSON) 

$key = @() 
$count = @() 
foreach($issue in $results.issues) { 
    $key += $issue.key 
    $count += $key.count 
} 

$key = @($key | Where-Object {$_ -ne $null}) 
$count = @($count | Where-Object {$_ -ne $null}) 

我使用不具備最新的PowerShell的包所以這就是爲什麼我已經包括ConvertFrom-Json20([object])功能的服務器。在SCORCH中,$key$count是發佈的數據變量。

回答

0

使用上面的代碼,您可以從JIRA Rest API的JSON數據中獲取Issue Key字段。

0

對於PowerShell的問題,如果您在服務器上安裝版本3.0或更高版本,您有幾種方法來替換json轉換函數。

  1. reactor進行註冊,並部署以下集成包: http://orchestrator.codeplex.com/releases/view/76101
  2. 爲運行.NET腳本活動PowerShell腳本塊內執行的PowerShell: https://automys.com/library/asset/powershell-system-center-orchestrator-practice-template

我不理解爲什麼你需要遍歷鍵並存儲鍵的當前索引的基於1的值。鑑於以上關於PowerShell,以下將給你你所顯示的相同結果。在你消費的地方,$ key數組需要考慮當前零元素索引:

$results = ConvertFrom-Json $JSON 
$key = $results.issues | Where-Object {$_.key -ne $null}