powershell - 使用 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"
我写的比较不起作用,它一直返回与包含所有组合的文件相同的结果!我将不胜感激任何帮助谢谢
解决方案
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.
推荐阅读
- java - 从内部类中访问局部变量问题;需要声明为final
- reactjs - 在任一上下文中都找不到“商店”
- matrix - 在 r 中使用 geohashes 构建二进制矩阵
- matlab - 这些 Matlab 内置的 Poisson pmf 中哪个最准确?
- php - Wordpress - 如何在预览中只显示两个句子 the_excerpt
- typescript - 打字稿阻止从项目中的某些目录导入
- laravel - (Laravel)通过数据透视表的多态关系
- events - 在事件驱动架构中,是否可以让所有服务将它们的事件发送到一个组件,然后再将其转发给适当的服务?
- r - 将数据循环到函数中
- jena - 不同的读者是如何在耶拿注册的?