gpt4 book ai didi

powershell - 扩展对象中的数组?

转载 作者:行者123 更新时间:2023-12-03 00:59:11 25 4
gpt4 key购买 nike

我正在使用一个Web请求来查询电话系统中的用户列表及其队列关联。生成的数据被放入一个对象中,并最终输出到屏幕和CSV中。问题在于,许多用户是多个队列的成员,并且所得的队列成员资格数据作为数组返回。

这就是我的输出:

First Name Last Name   Extension Skill Group                              Skill Level---------- ---------   --------- -----------                              -----------Kylo       Ren         1234      {Sales Support, Customer Service}        {8, 8}Flip       Zimmerman   4321      {Accounting, Marketing}                  {5, 3}Clyde      Logan       1122      {Sales Support, Customer Service}        {4, 9}Ronnie     Peterson    2211      {Sales Support, Customer Service,Claims} {4, 9, 1}Adam       Sackler     1212      Engineering                              2

As you can see the "Skill Group" and "Skill Level" columns are represented as arrays and the CSV that's saved just has System.Object[] for the cell which is obviously just saying that the cell contains an array. However if the user is a member of only one skill group then the returned data will be a normal string, like Adam Sackler in the above list.

One of the things I'm struggling with is that I don't know how the data should be displayed? Some suggestions from you fine folks would be appreciated.

For instance should I dynamically create columns for each skill group and associated skill level? What happens when a user has fewer, or more, columns than what's already there?

First Name Last Name   Extension Skill Group 1 Skill Level 1 Skill Group 2    Skill Level 2---------- ---------   --------- ------------- ------------- -------------    -------------Kylo       Ren         1234      Sales Support 8             Customer Service 8Flip       Zimmerman   4321      Accounting    5             Marketing        3

Or should each additional skill group and skill level be on a new line? Should additional skill group/level be on its own row or just use a carriage return in the cell?

First Name Last Name   Extension Skill Group      Skill Level---------- ---------   --------- -----------      -----------Kylo       Ren         1234      Sales Support    8                                 Customer Service 4Flip       Zimmerman   4321      Accounting       5                                      Marketing        3

In any case, here's the relevant code that's creating the object with the arrays:

[xml]$resourceCsqInfo = Invoke-WebRequest -Uri $uccxServerBaseUrl"resource?csqid="$($csq.id) -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

[object]$data = $resourceCsqInfo.resources.resource

foreach ($resource in $data) {
$outputList += New-Object -TypeName PSObject -Property @{
"First Name" = $resource.firstName;
"Last Name" = $resource.lastName;
"Extension" = $resource.extension;
"Skill Group" = $resource.skillmap.skillcompetency.skillNameUriPair.name;
"Skill Level" = $resource.skillmap.skillcompetency.competencelevel
}
}

变量 $resource.skillmap.skillcompetency.skillNameUriPair.name$resource.skillmap.skillcompetency.competencelevel是循环中的相关项目。

所以我的问题是如何在对象中扩展数组,以及传达附加信息的最佳方法是什么?

最佳答案

传达附加信息的最佳方式将完全取决于谁或使用什么信息。格式化人类的输出将不同于按摩输出以进行进一步的程序分析或操纵。

通常,第一个示例中的动态列选项仅在可能的列数很小的情况下才起作用。即便如此,以编程方式同时以视觉方式理解仍然有些混乱。

第二个选项是您已经拥有的输出的更易于阅读的版本。但是使它看起来像这样的方法基本上会将其变成纯文本,您无法进行任何进一步的工作。

我的首选选项通常是第二个选项的变体。与其尝试从后续技能行中省略名称/扩展名数据,不如复制它。因此,Kylo最初具有两种技能的条目变成了两个不同的Kylo条目,每个条目都有不同的技能。这在数据库 Realm 称为非规范化。

在foreach循环中,您将有一个子循环,该子循环遍历技能集合,然后为每种技能输出一个对象,而不是为单个$resource输出一个对象。所以也许是这样的:

foreach ($resource in $data) {
for ($i=0; $i -lt @($resource.skillmap.skillcompetency.skillNameUriPair).Count; $i++) {
[pscustomobject]@{
'First Name' = $resource.firstName
'Last Name' = $resource.lastName
'Extension' = $resource.extension
'Skill Group' = $resource.skillmap.skillcompetency.skillNameUriPair[$i].name
'Skill Level' = $resource.skillmap.skillcompetency.competencelevel[$i]
}
}
}

关于powershell - 扩展对象中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58421590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com