-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ps1
68 lines (58 loc) · 1.55 KB
/
main.ps1
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
Import-Module ActiveDirectory
cd $PSScriptRoot
Do {
$path = read-host -prompt 'File name'
$valid = Test-Path -Path $path -PathType Leaf
if (!$valid){
echo "Not a valid path"
}
}
Until($valid)
Try {
$csv = import-csv -path $path
$out_path = $path.replace('.csv','.out.csv')
$fields = $csv[0].psobject.Properties.name
$first_col = $fields[0]
$input_arr = $csv.$first_col
$unaliasedFields = $fields
}
Catch {
Write-Host -ForegroundColor red "Csv formating is incorrect"
throw $_
}
#aliases for fields
$fields = $fields -ireplace "\busername\b","SamAccountName"
$fields = $fields -ireplace "\bemail\b", "EmailAddress"
$fields = $fields -ireplace "\bfirstname\b","Surname"
$fields = $fields -ireplace "\blastname\b","GivenName"
$in_type = $fields[0]
$output=@()
Try {
$servers = get-content -path ".\servers.txt"
$servers_arr = $servers -split " "
foreach ($data in $input_arr) {
foreach ($server in $servers_arr) {
$output += Get-ADUser `
-Filter "$in_type -eq '$data'" `
-server $server `
-Properties $fields
}
}
}
Catch {
Write-Host -ForegroundColor red "A problem occured when fetching info from the AD"
throw $_
}
Try {
$output | select $fields | export-csv -notypeinformation -delimiter ';' -path $out_path
$content = get-content -path $out_path
$content[0] = '"{0}"' -f ($unaliasedFields -join '";"')
$content | set-content -path $out_path
}
Catch {
Write-Host -ForegroundColor red "A problem occured while saving the file"
throw $_
}
Finally {
Write-Host -ForegroundColor green "Successfully wrote output to $out_path"
}