比较两个数组,在 PowerShell 中通过电子邮件发送结果
Compare Two Arrays, Email Results in PowerShell
我正在尝试比较两个数组:一个包含用户名列表(动态,有时用户名较多,有时较少),另一个包含文件名列表(也是动态的)。每个文件名都包含用户名和其他文本,例如“用户名报告 [日期].xlsx”。目标是匹配数组A和数组B之间的元素。
数组 A 只是用户名。
$Username 中包含的数组 A 的输出只是:
人物A
乙
人物C
等...
数组 B 包含文件路径,但我可以将其缩小到只有文件名,如 $ArrayB.Name(完整路径为 $ArrayB.FullName)。数组B的命名格式为“用户名报告[日期].xlsx”。
包含在 $LatestFiles.Name(文件名)中的数组 B 的输出是:
PersonA 报告 1-1-21.xlsx
PersonB 报告 1-1-21.xlsx
PersonC 报告 1-1-21.xlsx
匹配后,最后一块是如果数组A中的元素与数组B中的元素匹配,则将ArrayB.FullName附加到相应的用户名+“@domain.com”。
不幸的是,我什至无法使匹配正常工作。
我试过:
foreach ($elem in $UserName) { if ($LatestFiles.Name -contains $elem) { "there is a match" } }
和
foreach ($elem in $UserName) {
if($LatestFiles.Name -contains $elem) {
"There is a match"
} else {
"There is no match"
}
}
和几个不同的变体,但我无法让它们输出匹配项。感谢任何帮助。
关于无法匹配的原因的简短回答:
-Contains
用于将值与集合匹配,而不是 String
。您最好使用 -Like
作为比较运算符。或者,至少,您可能会尝试查看文件名(而不仅仅是包含用户的名称部分)是否在用户名集合中。
我听起来好像您不是在简单地比较数组,而是比较复杂的事情,即您对两个匹配元素所做的操作。
$LatestFiles |
ForEach-Object {
# first, let's take the formatted file name and make it more usable
$fileName = $_.BaseName -split ' ' # BaseName over Name so that it strips the extension
Write-Output @{
User = $fileName[0]
Date = $fileName[2]
File = $_
}
} -PipelineVariable FileData |
# next, only accept valid users
Where-Object User -In $UserName |
# note that we don't need the value from $UserName because we already have it in $FileData.User (what we matched)
ForEach-Object {
# finally do your thing with it.
$userWithDomain = "$($FileData.User)@domain.com"
Write-Output $userWithDomain # or whatever you want
}
我正在尝试比较两个数组:一个包含用户名列表(动态,有时用户名较多,有时较少),另一个包含文件名列表(也是动态的)。每个文件名都包含用户名和其他文本,例如“用户名报告 [日期].xlsx”。目标是匹配数组A和数组B之间的元素。
数组 A 只是用户名。 $Username 中包含的数组 A 的输出只是: 人物A 乙 人物C 等...
数组 B 包含文件路径,但我可以将其缩小到只有文件名,如 $ArrayB.Name(完整路径为 $ArrayB.FullName)。数组B的命名格式为“用户名报告[日期].xlsx”。
包含在 $LatestFiles.Name(文件名)中的数组 B 的输出是: PersonA 报告 1-1-21.xlsx PersonB 报告 1-1-21.xlsx PersonC 报告 1-1-21.xlsx
匹配后,最后一块是如果数组A中的元素与数组B中的元素匹配,则将ArrayB.FullName附加到相应的用户名+“@domain.com”。
不幸的是,我什至无法使匹配正常工作。
我试过:
foreach ($elem in $UserName) { if ($LatestFiles.Name -contains $elem) { "there is a match" } }
和
foreach ($elem in $UserName) {
if($LatestFiles.Name -contains $elem) {
"There is a match"
} else {
"There is no match"
}
}
和几个不同的变体,但我无法让它们输出匹配项。感谢任何帮助。
关于无法匹配的原因的简短回答:
-Contains
用于将值与集合匹配,而不是 String
。您最好使用 -Like
作为比较运算符。或者,至少,您可能会尝试查看文件名(而不仅仅是包含用户的名称部分)是否在用户名集合中。
我听起来好像您不是在简单地比较数组,而是比较复杂的事情,即您对两个匹配元素所做的操作。
$LatestFiles |
ForEach-Object {
# first, let's take the formatted file name and make it more usable
$fileName = $_.BaseName -split ' ' # BaseName over Name so that it strips the extension
Write-Output @{
User = $fileName[0]
Date = $fileName[2]
File = $_
}
} -PipelineVariable FileData |
# next, only accept valid users
Where-Object User -In $UserName |
# note that we don't need the value from $UserName because we already have it in $FileData.User (what we matched)
ForEach-Object {
# finally do your thing with it.
$userWithDomain = "$($FileData.User)@domain.com"
Write-Output $userWithDomain # or whatever you want
}