-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ps1
More file actions
82 lines (66 loc) · 1.69 KB
/
script.ps1
File metadata and controls
82 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$f1 = $args[0]
$f2 = $args[1]
$f3 = $args[2]
#$f1 = "master1.csv"
#$f2 = "master2.csv"
#$f3 = "diff.csv"
if($args.Length -ne 3) {
Write-Host "Usage: ",$PSCommandPath," <early_file.csv> <later_file.csv> <output_file.csv>"
exit
}
$data1 = @{}
$data2 = @{}
$csvfile = Import-Csv -Path $f1
$data1=@{}
foreach($r in $csvfile)
{
$data1[$r.Groups] = $r.ExistingUsers
}
$csvfile = Import-Csv -Path $f2
$data2=@{}
foreach($r in $csvfile)
{
$data2[$r.Groups] = $r.ExistingUsers
}
$changes = @{}
# find differences
$allkeys = $data2.Keys + $data1.Keys
$allkeys = $allkeys | select -Unique
foreach($g in $allkeys) {
$d1 = $data1[$g].Split(",") | sort
$d1 = $d1 -join ","
$d2 = $data2[$g].Split(",") | sort
$d2 = $d2 -join ","
if($d1 -ne $d2) {
$changes[$g] = @{"UsersTobeAdded"=[System.Collections.ArrayList]@(); "UsersTobeRemoved"=[System.Collections.ArrayList]@()}
}
}
foreach($g in $changes.Keys) {
# find users to add
foreach($u in $data2[$g].Split(",")) {
if($data1[$g].Split(",").Contains($u)) {
} else {
$changes[$g]["UsersTobeAdded"] += $u
}
}
# find users to remove
foreach($u in $data1[$g].Split(",")) {
if($data2[$g].Split(",").Contains($u)) {
} else {
$changes[$g]["UsersTobeRemoved"] += $u
}
}
}
$groups = $changes.Keys | sort
$output = @()
foreach($g in $groups) {
$output += [PSCustomObject][Ordered]@{
Groups = $g
UsersTobeAdded = $changes[$g]["UsersTobeAdded"] -join ","
UsersTobeRemoved = $changes[$g]["UsersTobeRemoved"] -join ","
}
}
if (Test-Path $f3) {
Remove-Item $f3
}
$output | Export-Csv -NoTypeInformation $f3