如何创建一个关联数组的列表中产生的一个变量在PowerShell?

unftdfkk  于 2023-05-17  发布在  Shell
关注(0)|答案(1)|浏览(152)

我连接到一个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返回一个类似于

manager1@company.com
market1@company.com  market2@company.com
manager2@comapny.com
market3@company.com  market4@company.com
manager3@company.com 
market5@company.com
manager4@company.com
market6@manager.com

有些经理人会有多个市场,而其他人只会有一个。经理人市场是列表中直接位于其名下的市场。我的下一步将是自动分配这些邮箱,但我不确定如何去与我的名单。我相信我将需要创建一个关联数组,以便能够抓住每个经理和他们的市场来分配他们。我该怎么做呢?或者这不是我应该追求的道路?
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":  "[\"employee@company.com\"]",
                           "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,
jv4diomz

jv4diomz1#

简介

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

*Quirk 1-员工列表在data对象上表示为一组数字 * 属性 *,如下所示:

{
  "data": {
    "0": { ... employee ... },
    "1": { ... employee ... },
    ...
    "N": { ... employee ... }
  }
}

而不是像这样的JSON数组:

{
  "data": [
    { ... employee ... },
    { ... employee ... },
    ...
    { ... employee ... }
  ]
}

*Quirk 2-workemailsupervisoryorgmanaged属性是包含嵌入式json的字符串-即:

"0": {
      "workemail": "[\"employee@company.com\"]",
      "supervisoryorgmanaged": "[\"Stores of Carolinas\",\"Stores of Georgia East\"]",
    },

而不是

"0": {
      "workemail": [
        "employee@company.com"
      ],
      "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": "[\"jim@company.com\"]",
      "supervisoryorgmanaged": "[\"Stores of North\",\"Stores of South\"]"
    },
    "1": {
      "workemail": "[\"bob@company.com\"]",
      "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
# ---------           ---------------------
# ["jim@company.com"] ["Stores of North","Stores of South"]
# ["bob@company.com"] ["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
# ---------       ---------------------              ------------
# jim@company.com {Stores of North, Stores of South}
# bob@company.com {Stores of East, Stores of West}

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

$lookups = @{
    "Stores of North" = "north@company.com"
    "Stores of South" = "south@company.com"
    "Stores of East"  = "east@company.com"
    "Stores of West"  = "west@company.com"
}

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

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

$employees
# workemail       supervisoryorgmanaged              orgaddresses
# ---------       ---------------------              ------------
# jim@company.com {Stores of North, Stores of South} {north@company.com, south@company.com}
# bob@company.com {Stores of East, Stores of West}   {east@company.com, west@company.com}

把所有这些放在一起,我们得到:

# 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
# ---------       ---------------------              ------------
# jim@company.com {Stores of North, Stores of South} {north@company.com, south@company.com}
# bob@company.com {Stores of East, Stores of West}   {east@company.com, west@company.com}

你的问题是不清楚你想做什么的数据之后,但希望这是足以解开你目前的问题,你可以适应任何你需要做的事情从那里…

相关问题