-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-NamBulkAdUser.ps1
107 lines (70 loc) · 2.29 KB
/
New-NamBulkAdUser.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
param (
[Parameter(Mandatory)]
[string]$FilePath
)
. .\New-NamAdUser.ps1
. .\common\New-Log.ps1
$logFile = New-Item -Path ".\log" -Name "log-$(get-date -Format ddMMyyyy-hhmmss).txt" -Force
$resultFile = New-Item -Path ".\result" -Name "result-$(get-date -Format ddMMyyyy-hhmmss).csv" -Force
Start-Transcript -Path $logFile.FullName
# Check if path exist
if (Test-Path -Path $FilePath) {
# Check if file extension is .csv
if ([IO.Path]::GetExtension($FilePath) -eq ".csv") {
# Import Csv file
try {
$importCsv = Import-Csv -path $FilePath -ErrorAction Stop
New-Log -Level "INFO" -Message "Import Input Csv file success"
}
catch {
New-Log -Level "ERROR" -Message $_.Exception
Break
} # end try
}
else {
New-Log -Level "ERROR" -Message "File type is not csv,"
Break
} # end if ([IO.Path]::GetExtension($FilePath) -eq ".csv")
}
else {
New-Log -Level "ERROR" -Message "Path to file not exist, aborting"
Break
} # end if (Test-Path -Path $FilePath)
foreach ($row in $importCsv) {
if ($null -eq $row.PREFERED_NAME) {
$FirstName = $row.FIRST_NAME
}
else {
$FirstName = $row.PREFERED_NAME
}
$LastName = $row.LAST_NAME
$Title = $row.JOB_NAME
$Department = $row.Department
$Manager = $row."Supervisor AD Account"
$Location = $row."location code"
$newAdUser = New-NamAdUser -FirstName $FirstName `
-LastName $LastName `
-Title $Title `
-Department $Department `
-Manager $Manager `
-Location $Location
$adUserInfo = [PSCustomObject]@{
FirstName = $newAdUser.FirstName
LastName = $newAdUser.LastName
Name = $newAdUser.Name
SamAccountName = $newAdUser.SamAccountName
Title = $newAdUser.Title
Department = $newAdUser.Department
Manager = $newAdUser.Manager
StreetAddress = $newAdUser.StreetAddress
City = $newAdUser.City
State = $newAdUser.State
PostalCode = $newAdUser.PostalCode
Country = $newAdUser.Country
OuPath = $newAdUser.OuPath
Password = $newAdUser.Password
Result = $newAdUser.Result
}
$adUserInfo | Export-Csv -Path $resultFile.FullName -NoTypeInformation -Append -Force
} # end foreach ($row in $importCsv)
Stop-Transcript