forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatFunctions.ps1
270 lines (244 loc) · 9.22 KB
/
FormatFunctions.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Function Format-Percent {
[cmdletbinding(DefaultParameterSetName = "None")]
[OutputType([Double], ParameterSetName = "None")]
[OutputType([String], ParameterSetName = "String")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "What is the value?")]
[ValidateNotNullorEmpty()]
[Alias("X", "Numerator")]
$Value,
[Parameter(Position = 1, Mandatory, HelpMessage = "What is the total value?")]
[ValidateNotNullorEmpty()]
[Alias("Y", "Denominator")]
$Total,
[ValidateNotNullorEmpty()]
[ValidateRange(0, 15)]
[int]$Decimal = 2,
[Parameter(ParameterSetName = "String")]
[Switch]$AsString
)
Write-Debug "STARTING: $($MyInvocation.Mycommand)"
Write-Debug "STATUS: Calculating percentage from $Value/$Total to $decimal places"
$result = $Value / $Total
if ($AsString) {
Write-Debug "STATUS: Writing string result"
#use the -F operator to build a percent string to X number of decimal places
$pctstring = "{0:p$Decimal}" -f $result
#remove the space before the % symbol
$pctstring.Replace(" ", "")
}
else {
Write-Debug "STATUS: Writing numeric result"
#round the result to the specified number of decimal places
[math]::Round( ($result * 100), $Decimal)
}
Write-Debug "ENDING: $($MyInvocation.Mycommand)"
} #end function
Function Format-Value {
[cmdletbinding(DefaultParameterSetName = "Default")]
Param(
[Parameter(Position = 1, Mandatory, ValueFromPipeline)]
[ValidateNotNullorEmpty()]
$InputObject,
[Parameter(Position = 0, ParameterSetName = "Default")]
[ValidateSet("KB", "MB", "GB", "TB", "PB")]
[string]$Unit,
[ValidateRange(0, 15)]
[Parameter(ParameterSetName = "Default")]
[Parameter(ParameterSetName = "Auto")]
[Parameter(ParameterSetName = "Number")]
[int]$Decimal,
[Parameter(ParameterSetName = "Auto")]
[switch]$Autodetect,
[Parameter(ParameterSetName = "Currency")]
[switch]$AsCurrency,
[Parameter(ParameterSetName = "Number")]
[switch]$AsNumber
)
Begin {
Write-Debug "STARTING: $($MyInvocation.Mycommand)"
Write-Debug "STATUS: Using parameter set $($PSCmdlet.ParameterSetName)"
} #begin
Process {
Write-Debug "STATUS: Formatting $Inputobject"
<#
divide the incoming value by the specified unit
There is no need to process other statements so I'm using the Break keyword
although in reality the rest of the statements wouldn't be processed anyway
#>
Switch ($PSCmdlet.ParameterSetName) {
"Default" {
Write-Debug "..as $Unit"
Switch ($Unit) {
"KB" { $value = $Inputobject / 1KB ; break }
"MB" { $value = $Inputobject / 1MB ; break }
"GB" { $value = $Inputobject / 1GB ; break }
"TB" { $value = $Inputobject / 1TB ; break }
"PB" { $value = $Inputobject / 1PB ; break }
default {
#just use the raw value
$value = $Inputobject
}
} #default
}
"Auto" {
Write-Debug "STATUS: Using Autodetect"
if ($InputObject -ge 1PB) {
Write-Debug "..as PB"
$value = $Inputobject / 1PB
}
elseif ($InputObject -ge 1TB) {
Write-Debug "..as TB"
$value = $Inputobject / 1TB
}
elseif ($InputObject -ge 1GB) {
Write-Debug "..as GB"
$value = $Inputobject / 1GB
}
elseif ($InputObject -ge 1MB) {
Write-Debug "..as MB"
$value = $Inputobject / 1MB
}
elseif ($InputObject -ge 1KB) {
Write-Debug "..as KB"
$value = $Inputobject / 1KB
}
else {
Write-Debug "..as bytes"
$value = $InputObject
}
Break
} #Auto
"Currency" {
Write-Debug "...as currency"
"{0:c}" -f $InputObject
#if using currency no other code in the Process block will be run
Break
}#Currency
"Number" {
Write-Debug "...as number"
#if -Decimal not used explicitly set it to 0
if (-Not $Decimal) {
$Decimal = 0
}
#format as a number to the specified number of decimal points
"{0:n$($decimal)}" -f $InputObject
Break
}
} #switch parameterset name
if ($PSCmdlet.ParameterSetName -notmatch "Currency|Number") {
Write-Debug "STATUS: Reformatting $value"
if ($decimal) {
Write-Debug "..to $decimal decimal places"
#round the number to the specified number of decimal places
[math]::Round($value, $decimal)
}
else {
#if not a currency and not using a decimal then treat the value as an integer
#and write the result to the pipeline
Write-Debug "..as [int]"
$value -as [int]
}
} #parameter set <> currency
} #process
End {
Write-Debug "ENDING: $($MyInvocation.Mycommand)"
} #end
}
Function Format-String {
[cmdletbinding()]
[OutputType([string])]
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[ValidateNotNullorEmpty()]
[string]$Text,
[switch]$Reverse,
[ValidateSet("Upper", "Lower", "Proper", "Toggle", "Alternate")]
[string]$Case,
[hashtable]$Replace,
[switch]$Randomize
)
Begin {
Write-Debug "STARTING: $($MyInvocation.Mycommand)"
Write-Debug "STATUS: Using parameter set $($PSCmdlet.parameterSetName)"
} #begin
Process {
Write-Debug "STATUS: Processing $Text"
if ($Reverse) {
Write-Debug "STATUS: Reversing $($Text.length) characters"
$rev = for ($i = $Text.length; $i -ge 0 ; $i--) { $Text[$i]}
#join the reverse array back into a string
$str = $rev -join ""
}
else {
#copy the Text value to this internal variable
$str = $Text
}
if ($Randomize) {
Write-Debug "STATUS: Randomizing text"
#get a random number of characters that is the same length as the original string
#and join them back together
$str = ($str.ToCharArray() | Get-Random -count $str.length) -join ""
} #Randomize
if ($Replace) {
foreach ($key in $Replace.keys) {
Write-Debug "STATUS: Replacing $key with $($replace.item($key))"
$str = $str.replace($key, $replace.item($key))
} #foreach
} #replace
Switch ($case) {
"Upper" {
Write-Debug "STATUS: Setting to upper case"
$str = $str.ToUpper()
} #upper
"Lower" {
Write-Debug "STATUS: Setting to lower case"
$str = $str.ToLower()
} #lower
"Proper" {
Write-Debug "STATUS: Setting to proper case"
$str = "{0}{1}" -f $str[0].toString().toUpper(), -join $str.Substring(1).ToLower()
} #proper
"Alternate" {
Write-Debug "STATUS: Setting to alternate case"
$alter = for ($i = 0 ; $i -lt $str.length ; $i++) {
#Odd numbers are uppercase
if ($i % 2) {
$str[$i].ToString().Tolower()
}
else {
$str[$i].ToString().ToUpper()
}
} #for
$str = $alter -join ""
} #alternate
"Toggle" {
Write-Debug "STATUS: setting to toggle case"
<#
use a regular expression pattern for a case sensitive match
Other characters like ! and numbers will fail the test
but the ToUpper() method will have no effect.
#>
#code to use methods from [CHAR] which should better handle other cultures
$toggle = for ($i = 0 ; $i -lt $str.length ; $i++) {
if ([char]::IsUpper($str[$i])) {
$str[$i].ToString().ToLower()
}
else {
$str[$i].ToString().ToUpper()
}
} #for
$str = $toggle -join ""
} #toggle
Default {
Write-Debug "STATUS: no further formatting"
}
}
#write result to the pipeline
$str
} #process
End {
Write-Debug "ENDING: $($MyInvocation.Mycommand)"
} #end
}