如何从 Powershell 中的变量生成的列表中创建关联数组?

问题描述 投票:0回答:1

我正在连接到一个 API 并拉下经理列表以及他们可以使用 PowerShell 访问的市场。然后我使用替换命令将这些市场更改为所述市场的相应邮箱,这些邮箱将根据市场与经理共享。

#Pull WITH jobprofileid
$response = Invoke-RestMethod -Uri $GetARDIR -Method GET -ContentType $content  -Headers $headers | ConvertTo-Json -Depth 10

#Gather Response 
$response | Out-File $Outfile

#Get Email and Managed Org
$alignment = Get-Content $Outfile | Select-String -Pattern workemail,supervisoryorgmanaged

$cleanalignment = $alignment -replace "blah"

$fullalignment = $cleanalignment.trim()

$fullalignment

然后 $fullalignment 返回一个像这样的列表

[email protected]
[email protected]  [email protected]
[email protected]
[email protected]  [email protected]
[email protected] 
[email protected]
[email protected]
[email protected]

有些经理会应用多个市场,而其他经理只会应用一个市场。经理市场是列表中直接位于其名称下的市场。我的下一步将是自动分配这些邮箱,但我不确定如何使用我的列表进行分配。我相信我需要创建一个关联数组,以便能够抓住每个经理和他们的市场来分配他们。我将如何去做,或者这甚至不是我应该追求的道路?

API 数据示例

{
    "data":  {
                 "0":  {
                           "index":  11,
                           "firstname":  "null",
                           "lastname":  "null",
                           "fullname":  "null",
                           "jobprofilename":  "null",
                           "jobprofileid":  "null",
                           "workphonenumber":  "[\"null\"]",
                           "worklocationname":  "null",
                           "worklocationsuporg":  "null",
                           "worklocationid":  "null",
                           "worklocationid4":  "null",
                           "altworklocationid":  null,
                           "altworklocationid4":  null,
                           "homeemail":  "null",
                           "workemail":  "[\"[email protected]\"]",
                           "workstreetaddress":  "null",
                           "workcity":  "null",
                           "workstate":  "null",
                           "workpostalcode":  null,
                           "workcountry":  "null",
                           "homestreetaddress":  "null",
                           "homecity":  "null",
                           "homestate":  "null",
                           "homecountry":  "null",
                           "homepostalcode":  null,
                           "employeeid":  null,
                           "homephone":  "null",
                           "title":  "null",
                           "department":  "null",
                           "departmentid":  null,
                           "company":  "null",
                           "organizationid":  "null",
                           "companyid":  "null",
                           "managername":  "null",
                           "managerid":  null,
                           "fzsitenumber":  null,
                           "hiredate":  "null",
                           "dob":  null,
                           "supervisoryorg":  "[\"null\"]",
                           "supervisoryorgmanaged": "[\"Stores of Carolinas\",\"Stores of Georgia East\"]",
                           "toolsroles":  "[\"null"]",
                           "termdatetime":  null,
                           "contingentworker":  null,
                           "remoteworker":  null,
                           "lastaction":  "null",
                           "lastdatetime":  "null",
                           "ldapsync":  null,
                           "tangosync":  null,
                           "corrigosync":  null,
                           "prophixsync":  null,
                           "procurosync":  null
                       },
                "1":   {
                           "index":  1207,
                           "firstname":  "null",
                           "lastname":  "null",
                           "fullname":  "null",
                           "jobprofilename":  "nulls",
                           "jobprofileid":  "null",
                           "workphonenumber":  "[\"null\"]",
                           "worklocationname":  "null",
                           "worklocationsuporg":  "null",
                           "worklocationid":  "null",
                           "worklocationid4":  "null",
                           "altworklocationid":  null,
                           "altworklocationid4":  null,
arrays list powershell variables
1个回答
1
投票

简介

你的 json 格式有点不寻常,因为有几个怪癖:

  • 怪癖 1 - 员工列表表示为 data 对象上的一组数字
    properties
    ,如下所示:
{
  "data": {
    "0": { ... employee ... },
    "1": { ... employee ... },
    ...
    "N": { ... employee ... }
  }
}

而不是像这样的 json 数组:

{
  "data": [
    { ... employee ... },
    { ... employee ... },
    ...
    { ... employee ... }
  ]
}
  • Quirk 2 -
    workemail
    supervisoryorgmanaged
    属性是包含嵌入式 json 的字符串 - 即:
    "0": {
      "workemail": "[\"[email protected]\"]",
      "supervisoryorgmanaged": "[\"Stores of Carolinas\",\"Stores of Georgia East\"]",
    },

而不是

    "0": {
      "workemail": [
        "[email protected]"
      ],
      "supervisoryorgmanaged": [
        "Stores of Carolinas",
        "Stores of Georgia East"
      ]
    },

但没关系 - 现在我们可以看到它,我们可以解决它......

回答

所以首先,您可以放弃大部分示例代码,只使用

# note - no trailing ConvertTo-Json
$response = Invoke-RestMethod `
    -Uri         $GetARDIR `
    -Method      "GET" `
    -ContentType $content `
    -Headers     $headers;

这为您提供了一个无需进行大量字符串操作即可处理的结构化对象。

我将使用这个示例数据,这样你就可以剪切和粘贴它来尝试而不是必须查询 api,但是

Invoke-RestMethod
的返回值具有相同的形状:

$response = @"
{
  "data": {
    "0": {
      "workemail": "[\"[email protected]\"]",
      "supervisoryorgmanaged": "[\"Stores of North\",\"Stores of South\"]",
    },
    "1": {
      "workemail": "[\"[email protected]\"]",
      "supervisoryorgmanaged": "[\"Stores of East\",\"Stores of West\"]",
    }
  }
}
"@ | ConvertFrom-Json

要将员工按摩到数组中,我们可以这样做:

# get an array of all the employee objects
$employees = $response.data.psobject.properties.value;

# show the results
$employees
# workemail           supervisoryorgmanaged
# ---------           ---------------------
# ["[email protected]"] ["Stores of North","Stores of South"]
# ["[email protected]"] ["Stores of East","Stores of West"]

这基本上是查询

data
对象的元数据以获取其属性 definitions(“0”、“1”等)作为数组,然后使用 Member-Access Enumeration 读取每个属性的值。

然后我们需要解析嵌入的 json 以将 that 转换为结构化对象,我们可以从中提取值:

$employees = $employees | foreach-object {
    [pscustomobject] @{
        # assume there's only one workemail address
        "workemail"             = @($_.workemail | ConvertFrom-Json)[0]
        "supervisoryorgmanaged" = @($_.supervisoryorgmanaged | ConvertFrom-Json)
        # add a new property to hold the email addresses
        "orgaddresses"          = $null
    }
}

$employees
# workemail       supervisoryorgmanaged              orgaddresses
# ---------       ---------------------              ------------
# [email protected] {Stores of North, Stores of South}
# [email protected] {Stores of East, Stores of West}

接下来,我们需要一个查找哈希表(您的关联数组)来将组织名称映射到电子邮件地址:

$lookups = @{
    "Stores of North" = "[email protected]"
    "Stores of South" = "[email protected]"
    "Stores of East"  = "[email protected]"
    "Stores of West"  = "[email protected]"
}

然后我们可以将查找应用于每个员工的组织:

foreach( $employee in $employees )
{
    $employee.orgaddresses = @(
        $employee.supervisoryorgmanaged
            | foreach-object {
                $lookups[$_]
           }
    );
}

$employees
# workemail       supervisoryorgmanaged              orgaddresses
# ---------       ---------------------              ------------
# [email protected] {Stores of North, Stores of South} {[email protected], [email protected]}
# [email protected] {Stores of East, Stores of West}   {[email protected], [email protected]}

把它们放在一起我们得到:

# get the raw employee data out of the response
$employees = $response.data.psobject.properties.value;

# parse the workemail and supervisoryorgmanaged properties
$employees = $employees | foreach-object {
    [pscustomobject] @{
        # assume there's only one workemail address
        "workemail"             = @($_.workemail | ConvertFrom-Json)[0]
        "supervisoryorgmanaged" = @($_.supervisoryorgmanaged | ConvertFrom-Json)
        # add a new property to hold the email addresses
        "orgaddresses"          = $null
    }
}

# map the organisations to eamil addresses
foreach( $employee in $employees )
{
    $employee.orgaddresses = @(
        $employee.supervisoryorgmanaged
            | foreach-object {
                $lookups[$_]
           }
    );
}

$employees
# workemail       supervisoryorgmanaged              orgaddresses
# ---------       ---------------------              ------------
# [email protected] {Stores of North, Stores of South} {[email protected], [email protected]}
# [email protected] {Stores of East, Stores of West}   {[email protected], [email protected]}

您的问题不清楚您之后要对数据做什么,但希望这足以解决您当前的问题,并且您可以从那里调整它以适应您需要做的任何事情......

© www.soinside.com 2019 - 2024. All rights reserved.