Powershell拆分为哈希表

时间:2018-10-16 13:29:14

标签: powershell jira-rest-api

我正在尝试拆分从jira rest api获得的字符串,但是我找不到一种好的方法。 API返回此类对象

  

com.atlassian.greenhopper.service.sprint.Sprint@3b306c49 [id = 2792,rapidViewId = 920,state = CLOSED,name = ABI   报告/支持冲刺   12,startDate = 2018-09-11T09:45:26.622 + 02:00,endDate = 2018-09-27T22:00:00.000 + 02:00,completeDate = 2018-09-28T08:15:41.088 + 02:00, sequence = 2792] com.atlassian.greenhopper.service.sprint.Sprint@c518022 [id = 2830,rapidViewId = 920,state = ACTIVE,name = ABI   报告/支持冲刺   13,startDate = 2018-09-28T08:30:26.785 + 02:00,endDate = 2018-10-16T20:30:00.000 + 02:00,completeDate =,sequence = 2830]

我要做的是

$sprints = $issue.fields.customfield_10012 | Select-String -Pattern '\x5b(.*)\x5d' | ForEach-Object {$_.Matches.Groups[1].Value}

其中$ issue.fields.customfield_10012是从REST API返回的字段

这为我提供了exesse数据的条带化对象,我可以使用此数据将其转换为哈希表

Foreach ($sprint in $sprints) {
  Try {
    #assign values to variable
    $sprint = $sprint -split ',' | Out-String
    $sprint = ConvertFrom-StringData -StringData $sprint
    [int]$sId = $sprint.id
    $sName = "N'" + $sprint.name.Replace("'", "''") + "'"
    #insert into sql using Invoke-Sqlcmd
  }
  Catch {
    #Write log msg into log table about error in Staging of the worklog for the ticket
    $logMsg = "Staging sprint ($sId) for ticket ($key): $($_.Exception.Message)"
    Write-Host $logMsg
  }
}

但是我的用户很有创造力,并且sprint的名字之一是“ Sprint 11-AS,SS,RS”,它打破了我的-split','并转换为哈希表。

有什么主意如何将此字符串拆分为适当的哈希表?

  

com.atlassian.greenhopper.service.sprint.Sprint@3b306c49 [id = 2792,rapidViewId = 920,state = CLOSED,name = ABI   报告/支持冲刺   12,startDate = 2018-09-11T09:45:26.622 + 02:00,endDate = 2018-09-27T22:00:00.000 + 02:00,completeDate = 2018-09-28T08:15:41.088 + 02:00, sequence = 2792] com.atlassian.greenhopper.service.sprint.Sprint@c518022 [id = 2830,rapidViewId = 920,state = ACTIVE,名称= Sprint   11-   AS,SS,RS,startDate = 2018-09-28T08:30:26.785 + 02:00,endDate = 2018-10-16T20:30:00.000 + 02:00,completeDate =,sequence = 2830]

1 个答案:

答案 0 :(得分:1)

用逗号分隔字符串后跟一个等号的单词

使用这些记录中的每一行(如果它与源数据不匹配,您仍然可以使用下面的逻辑),我们进行匹配以将括号[]中的数据与外面的数据分开。然后,我们以积极的前瞻性对上述内部数据进行拆分,以获取哈希表。

$lines = "com.atlassian.greenhopper.service.sprint.Sprint@3b306c49[id=2792,rapidViewId=920,state=CLOSED,name=ABI Reports/Support sprint 12,startDate=2018-09-11T09:45:26.622+02:00,endDate=2018-09-27T22:00:00.000+02:00,completeDate=2018-09-28T08:15:41.088+02:00,sequence=2792]", 
"com.atlassian.greenhopper.service.sprint.Sprint@c518022[id=2830,rapidViewId=920,state=ACTIVE,name=Sprint 11 - AS,SS,RS,startDate=2018-09-28T08:30:26.785+02:00,endDate=2018-10-16T20:30:00.000+02:00,completeDate=,sequence=2830]"


$lines | Where-Object{$_ -match "^(?<sprintid>.*)\[(?<details>.*)\]"} | ForEach-Object{
    $Matches.details -split ",(?=\w+=)" | Out-String | ConvertFrom-StringData
}

如果我们使用[pscustomobject]类型的加速器,何时可以从中获得正确的对象集。

id           : 2792
startDate    : 2018-09-11T09:45:26.622+02:00
completeDate : 2018-09-28T08:15:41.088+02:00
sequence     : 2792
name         : ABI Reports/Support sprint 12
rapidViewId  : 920
endDate      : 2018-09-27T22:00:00.000+02:00
state        : CLOSED

id : 2830 startDate : 2018-09-28T08:30:26.785+02:00 completeDate : sequence : 2830 name : Sprint 11 - AS,SS,RS rapidViewId : 920 endDate : 2018-10-16T20:30:00.000+02:00 state : ACTIVE

我对ConvertFrom-StringData有更多的经验,但是TheIncorrigible1 mentions ... ConvertFrom-String的功能也很强大,可以减少此处的繁琐工作。