powershell拆分成哈希表

powershell split into hashtable

我正在尝试拆分从 jira rest api 获得的字符串,但找不到好的方法。 APIreturns这种对象

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=ABI Reports/Support sprint 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 数据条带化的对象,我可以使用此

将其转换为散列 table
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 ',' 并转换为散列 table.

知道如何将此字符串拆分为正确的散列 table 吗?

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]

用逗号分隔字符串 后跟[=​​28=]一个带等号的词

在各自的行上处理每条记录(如果这与源数据不匹配,您仍然可以使用下面的逻辑)我们进行匹配以将大括号 [] 内的数据与大括号 [] 外的数据分开。然后我们按照上面讨论的那样对内部数据进行拆分,并进行积极的前瞻,以获得哈希表。

$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] 类型加速器,则可以直接从中获取对象集。

<pre>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</p> <p>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</pre>

我对 ConvertFrom-StringData 有更多的经验,但是 ... ConvertFrom-String 也很强大,可以减少一些腿部工作。