我连接到一个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,
1条答案
按热度按时间jv4diomz1#
简介
你的json格式有点不寻常,因为有几个怪癖:
*Quirk 1-员工列表在
data
对象上表示为一组数字 * 属性 *,如下所示:而不是像这样的JSON数组:
*Quirk 2-
workemail
和supervisoryorgmanaged
属性是包含嵌入式json的字符串-即:而不是
但没关系-现在我们可以看到它,我们可以围绕它工作...
回答
因此,首先,您可以丢弃大部分示例代码,只使用
这就提供了一个结构化的对象,您可以在不进行大量字符串操作的情况下处理它。
我将使用这个示例数据,这样你就可以剪切和粘贴它来尝试,而不必查询API,但是
Invoke-RestMethod
的返回值是相同的形状:要将员工按摩到数组中,我们可以这样做:
这基本上是查询
data
对象的元数据,以获取其属性 definitions(“0”,“1”等)作为数组,然后使用Member-Access Enumeration读取每个属性的值。然后我们需要解析嵌入的json,将 that 转换为结构化对象,我们可以从中提取值:
接下来,我们需要一个查找哈希表(您的关联数组)来将组织名称Map到电子邮件地址:
然后我们可以将查找应用于每个员工的组织:
把所有这些放在一起,我们得到:
你的问题是不清楚你想做什么的数据之后,但希望这是足以解开你目前的问题,你可以适应任何你需要做的事情从那里…