我正在尝试使用来查找一个数组 ( ) 中但不在另一个Compare-Object
数组 ( ) 中的元素。出于某种原因,仅比较 fileArray 中的最后两个元素。这是我要运行的代码。$fileArray
$dictionaryArray
Compare-Object
$dictionary = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\dictionary.txt"
$file = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\file.txt"
$dictionaryArray = @()
$fileArray = @()
$dictionaryLength = $dictionary | Measure-Object -Line
$fileLength = $file | Measure-Object -Line
$i = 0
while($i -lt $dictionaryLength.Lines){
$name = $dictionary | Select -Index $i
$i++
while($dictionary[$i] -ne " " -and $i -lt $dictionaryLength.Lines){
$dictionaryObject = New-Object -TypeName PSObject
$dictionaryObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
$dictionaryObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $dictionary[$i]
$dictionaryArray += $dictionaryObject
$i++
}
$i++
}
$i = 0
while($i -lt $fileLength.Lines){
$name = $file | Select -Index $i
$i++
while($file[$i] -ne " " -and $i -lt $fileLength.Lines){
$fileObject = New-Object -TypeName PSObject
$fileObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
$fileObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $file[$i]
$fileArray += $fileObject
$i++
}
$i++
}
$i = 0
Compare-Object -ReferenceObject $fileArray -DifferenceObject $dictionaryArray |
Where-Object {$_.SideIndicator -eq '<='} |
ForEach-Object {Write-Output $_.InputObject}
当我简单地运行时Compare-Object -ReferenceObject $fileArray
-DifferenceObject $dictionaryArray
,我得到了输出
InputObject SideIndicator
@{PC=Joe; File=nopethere} <=
@{PC=Joe; File=hi!!!@} <=
fileArray 中有比这更多的项目。当我跑步时$fileArray
,我得到
PC File
Eric I like to
Eric there
Joe code because
Joe hello
Joe but why?
Joe *no\thank/ you :c
Joe nopethere
Joe hi!!!@
为什么我的代码没有比较这些对象中的每一个?
编辑
这是字典.txt
Eric
because
hello there
Joe
but why?
*no\thank/ you :c
nope
这是文件.txt
Eric
I like to
there
Joe
code because
hello
but why?
*no\thank/ you :c
nopethere
hi!!!@
我使用我的循环来创建对象。每个段落的第一行是每个后续对象的“PC”。然后,每一行都是每个对象的“文件”。我想检查哪些对象在 fileArray 而不是在 dictionaryArray 中。我希望输出是
PC File
Eric I like to
Joe code because
Joe hello
Joe nopethere
Joe hi!!!@
任何帮助,将不胜感激!