首页 > 解决方案 > 使用 powershell 比较 2 个 csv 文件

问题描述

我编写了一个脚本,它可以生成一个包含所有可能的密码组合(0000-9999)的文件,然后它会带回所有不可用的密码(已经分配给 AD 用户)但最后我想开发该脚本通过比较前两个文件(具有所有可能组合的文件和具有已经采用的组合的文件)生成具有可用密码的第三个文件,脚本是这样的

Import-Module Activedirectory 
$all = '0000'..'9999' | out-file C:\TEMP\test\NewGroup9.txt
$re = [regex]'ixi:\+49.*(\d{4})'    
Get-ADUser -Filter * -Properties ProxyAddresses |  
  Select-Object @{Name = 'ixi'; Expression = {$re.Match($_.ProxyAddresses).Groups[1].Value}} | 
    Where-Object { $_.ixi.Trim() } | export-csv C:\TEMP\test\NewGroup14.csv -Delimiter ";" -notypeinformation -Encoding "UTF8"
Get-Content C:\TEMP\test\NewGroup14.csv | % { $_ -replace 'ixi', '' } | 
  sort -Descending | Set-Content C:\TEMP\test\NewGroup17.csv
compare-object -ReferenceObject $(Get-Content C:\TEMP\test\NewGroup9.txt) -DifferenceObject $(get-content C:\TEMP\test\NewGroup17.csv) -IncludeEqual | 
  Where-Object SideIndicator -eq '<=' |
    Export-Csv C:\TEMP\test\NewGroup13.csv -Delimiter ";" -notypeinformation -Encoding "UTF8"

我写的比较不起作用,它一直返回与包含所有组合的文件相同的结果!我将不胜感激任何帮助谢谢

标签: powershellcomparison

解决方案


As you want to compare all numbers storing them in a file is IMO worthless

$all = 0..9999|%{"{0:D4}" -f $_}
# or alternatively
# $all = 0..9999|%{$_.ToString('0000')}

This could work (untested)

$re = [regex]'ixi:\+49.*(\d{4})'

$used = Get-ADUser -Filter * -Properties ProxyAddresses |
  Where ProxyAddressess -match $re |
    Select-Object @{N='ixi';E={$Matches.Groups[1].Value}}|
      Select-Object -ExpandProperty ixi

$UnUsed = Compare-Object ($used|sort) $all | 
  Select-Object -Expandproperty InputObject

You can save the vars $Used/$UnUsed to a file with Set-Content.


推荐阅读