[英]Comparing 2 csv files using powershell
我編寫了一個腳本,該腳本可以生成帶有所有可能的密碼組合(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"
我寫的比較不起作用,它不斷返回與文件組合相同的結果! 我將不勝感激任何幫助,謝謝
因為你想比較存儲在文件中的所有數字是IMO毫無價值
$all = 0..9999|%{"{0:D4}" -f $_}
# or alternatively
# $all = 0..9999|%{$_.ToString('0000')}
這可以工作(未經測試)
$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
您可以將變量$ Used / $ UnUsed保存到具有Set-Content的文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.