Arrays 查找数组1中但不在数组2中的项
我从两个不同的工具中提取了两个计算机列表,Arrays 查找数组1中但不在数组2中的项,arrays,powershell,foreach,Arrays,Powershell,Foreach,我从两个不同的工具中提取了两个计算机列表,Array1和Array2 现在我需要提取在Array1中的,但不在Array2中的 通过这样做,我成功地获得了所有匹配项: $matchingComp = @() foreach ($SCCMcomputer in $SCCMcomputers) { foreach ($eWPTcomputer in $eWPTcomputers) { if ($SCCMcomputer.Computername -eq $eWPTCompu
Array1
和Array2
现在我需要提取在Array1
中的,但不在Array2
中的
通过这样做,我成功地获得了所有匹配项:
$matchingComp = @()
foreach ($SCCMcomputer in $SCCMcomputers) {
foreach ($eWPTcomputer in $eWPTcomputers) {
if ($SCCMcomputer.Computername -eq $eWPTComputer.Computername) {
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $SCCMcomputer.Computername
$matchingComp +=$obj
}
}
}
$matchingComp | Export-Csv $inEWPT -Delimiter "," -NoTypeInformation
但是我仍然需要那些在$SCCMcomputer
中,但不在$eWPTcomputers
中的
我已经找到了一些其他语言(例如Perl)的解决方案,但不适用于PowerShell
更新
我仍然无法获得正确的输出,在Excel中使用以下公式:
输出如下所示:
意味着有些人在这里,有些人不在。powershell中的输出如下所示
表示0KB为emtpy
$SCCMcomputers | Export-Csv $sccmexport -Delimiter "," -NoTypeInformation
$eWPTcomputers | Export-Csv $ewptexport -Delimiter "," -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "=>"} |select inputobject | Export-Csv $inEWPT -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "=="} |select inputobject | Export-Csv $inBoth -NoTypeInformation
Compare-Object -ReferenceObject $SCCMcomputers -DifferenceObject $eWPTcomputers | ?{$_.sideIndicator -eq "<="} |select inputobject | Export-Csv $inSCCM -NoTypeInformation
通过删除哈希表的头,让数组中充满字符串,就可以实现奇妙的效果。。。。。甚至
-Property computername
也无法工作…:S使用比较对象
cmdlet
Compare Object-ReferenceObject$sccm-DifferenceObject$wpt |?{$\.sideIndicator-eq”您可以使用-contains
和-notcontains
$A1 ="asd","zxc","qwe",'a'
$A2 = "asd","zxc","bqwe",'b'
$A1,$A2 |
%{
$_|
%{
If ($A1 -contains $_ -and $A2 -notcontains $_) {$_}
}
}
使用比较对象
,如下所示
Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt -passthru
这应该只提供$sccm中的对象,而不是$wpt中的对象
更正:
上述代码适用于保证DifferenceObject是ReferenceObject的子集的情况。但是,如果DifferenceObject中还有其他对象不在ReferenceObject中,则该代码将失败。上述代码将返回在ReferenceObject或DifferenceObject中但不在两者中的任何对象
要正确地仅返回ReferenceObject中不存在于DifferenceObject中的对象,需要以下代码:
Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt |
Where-Object { $_.SideIndicator -eq '<=' } |
ForEach-Object { Write-Output $_.InputObject }
Compare Object-ReferenceObject$sccm-DifferenceObject$wpt|
在Object{$\ SideIndicator-eq'中,Compare Object方法确实是最好的方法。但还没有解决的是将干净的输出到Excel文件
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '<=' | Export-Csv sccm-only.csv -NoTypeInformation
$sccm=@(1,2,2,3)
$wpt=@(2,4)
比较对象-引用对象$sccm-差异对象$wpt|
何处物体{$\ SideIndicator-eq'也许知道这一点很好:我从两个不同服务器上的PS SQL查询中获得了两个数组,当然还有两个不同的DBs-也许已经有一种方法可以用SQL来实现它了?嘿,首先,感谢你的帮助…我尝试过了,输出非常混乱…我看不出它所做的事情背后的逻辑,一些计算机可能是e处理正确(avail.in 1和2中,已排序),但其中一些文件都在这两个文件中,并且仍被导出到此列表中--知道这是如何实现的吗?+1表示passthru,但缺少side指示符Compare Object-ReferenceObject$sccm-DifferenceObject$wpt-passthru |?{$\ sideIndicator-eq“
Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt -passthru
Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt |
Where-Object { $_.SideIndicator -eq '<=' } |
ForEach-Object { Write-Output $_.InputObject }
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '<=' | Export-Csv sccm-only.csv -NoTypeInformation
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '<=' | Select-Object InputObject | Export-Csv sccm-only.csv -NoTypeInformation
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '<=' | Select-Object @{ expression={$_.InputObject}; label='ComputerName' } | Export-Csv sccm-only.csv -NoTypeInformation
# Only in eWPT
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '=>' | Select-Object @{ expression={$_.InputObject}; label='ComputerName' } | Export-Csv sccm-only.csv -NoTypeInformation
# In both SCCM and eWPT
Compare-Object $sccm $ewpt | ?{ $_.SideIndicator -eq '==' | Select-Object @{ expression={$_.InputObject}; label='ComputerName' } | Export-Csv sccm-only.csv -NoTypeInformation
$sccm=@(1,2,2,3)
$wpt=@(2,4)
Compare-Object -ReferenceObject $sccm -DifferenceObject $wpt |
Where-Object { $_.SideIndicator -eq '<=' } |
ForEach-Object { Write-Output $_.InputObject }