0

我有这个数组:

[

"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]

并想对其进行整理并制作两个像这样的单独数组

[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo"
]


[
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]

希望得到您的帮助!谢谢 :)

4

6 回答 6

1

简单的数组过滤:

$arr="org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"

$arr2 = $arr.where{$PSItem -match 'joemarmoto'}
$arr3 = $arr.where{$PSItem -notmatch 'joemarmoto'}
于 2018-12-27T12:16:17.173 回答
1

一种稍微不同的方法是使用Group-Objectcmdlet 对计算的属性进行分组。您没有指定要排序的确切部分 - 虚拟电子邮件或后面的名称 - 所以我使用了虚拟电子邮件之后的部分。

结果为您提供了一个数组集合,其名称为 [计算的属性] 和一个组 [与该计算的属性匹配的项目]。您可以单独解决它们或使用它们来制作新数组。

$AllNames = @(
    'org-dummyemail-123-AccountName',
    'org-dummyemail-123-Email',
    'org-dummyemail-123-PAT',
    'org-dummyemail-123-PATName',
    'org-dummyemail-123-PATValidTo'
    'org-dummyemail1-joemarmoto-AccountName',
    'org-dummyemail1-joemarmoto-Email',
    'org-dummyemail1-joemarmoto-PAT',
    'org-dummyemail1-joemarmoto-PATName',
    'org-dummyemail1-joemarmoto-PATValidTo'
    )

# 3rd item index = 2
$PartToGroupOn = 2

$GroupedAllNames = $AllNames |
    Group-Object -Property {$_.Split('-')[$PartToGroupOn]}

$GroupedAllNames[1].Group

输出 ...

org-dummyemail-123-AccountName
org-dummyemail-123-Email
org-dummyemail-123-PAT
org-dummyemail-123-PATName
org-dummyemail-123-PATValidTo
于 2018-12-27T12:37:19.330 回答
0

同样的想法,但有一个哈希表:

$json = @'
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
'@

$a = $json | ConvertFrom-Json
$b = @{}

$a | ForEach-Object {
    $b[$_.Split('-')[0..2] -join '-'] += @($_)
}

$b | ConvertTo-Json
于 2018-12-27T15:32:38.693 回答
0

这是一个使用正则表达式来确定要分组的输入字符串部分的版本:

clear-host

'REGEX 1; org-dummyemail-123 vs org-dummyemail1-joemarmoto'

$groupedByAllButLast = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '([^-]+-[^-]+-[^-]+)-.*', '$1'}} 

for ([int]$i = 0; $i -lt $groupedByAllButLast.Count; $i++)
{
    Write-Verbose $groupedByAllButLast[$i].Name -Verbose
    $groupedByAllButLast[$i].Group
}

'REGEX 2; 123 vs joemarmoto'
$groupedByTheNameOrEmailBit = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '[^-]+-[^-]+-([^-]+)-.*', '$1'}} #all we've changed is the location of the opening bracket in this line 

for ([int]$i = 0; $i -lt $groupedByTheNameOrEmailBit.Count; $i++)
{
    Write-Verbose $groupedByTheNameOrEmailBit[$i].Name -Verbose
    $groupedByTheNameOrEmailBit[$i].Group
}

注意:这与李的方法基本相同;split我们仅使用正则表达式来捕获与我们的密钥相关的所有字符串部分,而不是使用和获取一个信号段...有关此有用功能的更多信息,请参阅MS Docs 。

于 2018-12-27T13:40:04.940 回答
0

使用 $PSItem 变量

$arr="org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"

$arr2 = $arr | Where-Object {$PSItem -match 'joemarmoto'}
$arr3 = $arr | Where-Object {$PSItem -match '123'}

看看这里:https ://blogs.msdn.microsoft.com/mvpawardprogram/2013/04/15/working-with-the-new-psitem-automatic-variable-in-windows-powershell-3-0/

从该站点:Get-Help about_Automatic_Variables -ShowWindow

在“查找”框中键入“psitem”以突出显示包含此单词的所有文本:

如我们所见,$PSItem 与 $_ 相同。引入 $PSItem 变量的目的是使包含“管道中的当前对象”的代码更易于阅读和理解。

答案几乎与 Gert Jan Kraaijeveld 相同,但解释了 $PSItem 的作用以及该信息的来源。

于 2018-12-27T13:31:20.350 回答
0

将您的输入视为 Json 对象(来自此处的字符串)脚本

  • -使用 Select-Object 和
    计算属性 从前 3 个 /dash 分隔的部分构建 akey 。
  • 按此键分组
  • 从组值重建 Json 对象。

$Json = @"
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
"@ | ConvertFrom-JSon

$Json | Select-Object @{n='key';E={($_ -split '-')[0..2] -join '-'}},
                      @{n='value';e={$_}} |
    Group-Object key | ForEach-Object{
        $_.Group.Value | ConvertTo-Json
    }

有这个输出:

[
    "org-dummyemail-123-AccountName",
    "org-dummyemail-123-Email",
    "org-dummyemail-123-PAT",
    "org-dummyemail-123-PATName",
    "org-dummyemail-123-PATValidTo"
]
[
    "org-dummyemail1-joemarmoto-AccountName",
    "org-dummyemail1-joemarmoto-Email",
    "org-dummyemail1-joemarmoto-PAT",
    "org-dummyemail1-joemarmoto-PATName",
    "org-dummyemail1-joemarmoto-PATValidTo"
]
于 2018-12-27T13:17:59.463 回答