This repository has been archived by the owner on Mar 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert-Customers.ps1
71 lines (69 loc) · 2.53 KB
/
Convert-Customers.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
function Convert-Customers {
[cmdletbinding()]
param (
$Path
)
Begin {
Write-Verbose -Message "[BEGIN ] Function to convert csv file to table with expanded columns"
}
Process {
try {
$customers_data = Import-Csv -Path $Path
}
catch {
Write-Error -Message $_.Exception.Message -ErrorAction Stop
}
$customers_table = @()
foreach ($item in $customers_data) {
$item_obj = $item.Name.Split(" ")
$x = $item_obj.Count
try {
switch ($x) {
2 {
$hash = @{
"Last Name" = $item_obj[1]
"First Name" = $item_obj[0]
Age = $item.Age
}
}
3 {
$matches = $item_obj[0] -match '\w{2,3}\.'
if ($matches) {
$hash = @{
"Last Name" = $item_obj[2]
"First Name" = $item_obj[1]
Age = $item.Age
}
}
else {
$hash = @{
"Last Name" = $item_obj[2]
"First Name" = $item_obj[0]
"Middle Initial" = $item_obj[1].Substring(0, 1)
Age = $item.Age
}
}
}
4 {
$hash = @{
"Last Name" = $item_obj[3]
"First Name" = $item_obj[1]
"Middle Initial" = $item_obj[2].Substring(0, 1)
Age = $item.Age
}
}
default { Write-Verbose -Message "[Process] Could not find object count" }
}
}
catch {
Write-Error -Message $_.Exception.Message -ErrorAction Stop
}
$ps_object = new-object psobject -property $hash
$customers_table += $ps_object
}
return $customers_table | Select-Object "Last Name", "First Name", "Middle Initial", Age
}
End {
Write-Verbose -Message "[END] Completed converstion to table"
}
}