-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathRemove-StringDiacritic.ps1
More file actions
139 lines (125 loc) · 6.06 KB
/
Remove-StringDiacritic.ps1
File metadata and controls
139 lines (125 loc) · 6.06 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function Remove-StringDiacritic {
# .SYNOPSIS
# This function will remove the diacritics (accents) characters from a string.
# .DESCRIPTION
# This function will remove the diacritics (accents) characters from a string.
# .PARAMETER Mode
# Set the function to run in StringMode or ObjectMode
# .PARAMETER String
# Specifies the String(s) on which the diacritics need to be removed. Exclusive to StringMode
# For Dynammic parameters, refer to https://powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/
# .PARAMETER Object
# Specifies the Object on which the diacritics of all columns need to be removed. Exclusive to ObjectMode
# For Dynammic parameters, refer to https://powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/
# .PARAMETER NormalizationForm
# Specifies the normalization form to use
# https://msdn.microsoft.com/en-us/library/system.text.normalizationform(v=vs.110).aspx
# .EXAMPLE
# PS C:\> Remove-StringDiacritic "L'été de Raphaël"
# L'ete de Raphael
# .NOTES
# Francois-Xavier Cat
# @lazywinadmin
# lazywinadmin.com
# github.com/lazywinadmin
# Edited by dkabal14
# github.com/dkabal14
[CMdletBinding()]
PARAM
(
[ValidateNotNullOrEmpty()]
[Alias('Text')]
[System.String][Parameter(Position=0,Mandatory=$true)][validateset("StringMode","ObjectMode")]$Mode,
[System.Text.NormalizationForm]$NormalizationForm = "FormD"
)
DynamicParam {
if ($Mode -eq "StringMode") {
#create a new ParameterAttribute Object
$StringMode = New-Object System.Management.Automation.ParameterAttribute
$StringMode.Position = 1
$StringMode.Mandatory = $true
#create an attributecollection object for the attribute we just created.
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attribute
$attributeCollection.Add($StringMode)
#add our paramater specifying the attribute collection
$ageParam = New-Object System.Management.Automation.RuntimeDefinedParameter('String', [System.String], $attributeCollection)
#expose the name of our parameter
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('String', $ageParam)
return $paramDictionary
}
if ($Mode -eq "ObjectMode") {
#create a new ParameterAttribute Object
$ObjectMode = New-Object System.Management.Automation.ParameterAttribute
$ObjectMode.Position = 1
$ObjectMode.Mandatory = $true
#create an attributecollection object for the attribute we just created.
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attribute
$attributeCollection.Add($ObjectMode)
#add our paramater specifying the attribute collection
$ageParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Object', [pscustomobject], $attributeCollection)
#expose the name of our parameter
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('Object', $ageParam)
return $paramDictionary
}
}
Process {
if ($Mode -eq "ObjectMode")
{
$objText = $PSBoundParameters.Object | ConvertTo-Csv
$result = @()
foreach ($line in $objText.Split("/s"))
{
$String = $line
FOREACH ($StringValue in $String) {
Write-Verbose -Message "$StringValue"
try {
# Normalize the String
$Normalized = $StringValue.Normalize($NormalizationForm)
$NewString = New-Object -TypeName System.Text.StringBuilder
# Convert the String to CharArray
$normalized.ToCharArray() |
ForEach-Object -Process {
if ([Globalization.CharUnicodeInfo]::GetUnicodeCategory($psitem) -ne [Globalization.UnicodeCategory]::NonSpacingMark) {
[void]$NewString.Append($psitem)
}
}
#Combine the new string chars
$result += $($NewString -as [string])
$result | ConvertFrom-Csv
}
Catch {
Write-Error -Message $Error[0].Exception.Message
}
}
}
}
else
{
$String = $PSBoundParameters.String
FOREACH ($StringValue in $String) {
Write-Verbose -Message "$StringValue"
try {
# Normalize the String
$Normalized = $StringValue.Normalize($NormalizationForm)
$NewString = New-Object -TypeName System.Text.StringBuilder
# Convert the String to CharArray
$normalized.ToCharArray() |
ForEach-Object -Process {
if ([Globalization.CharUnicodeInfo]::GetUnicodeCategory($psitem) -ne [Globalization.UnicodeCategory]::NonSpacingMark) {
[void]$NewString.Append($psitem)
}
}
#Combine the new string chars
Write-Output $($NewString -as [string])
}
Catch {
Write-Error -Message $Error[0].Exception.Message
}
}
}
}
}