forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimize-Text.ps1
More file actions
78 lines (64 loc) · 2.58 KB
/
Optimize-Text.ps1
File metadata and controls
78 lines (64 loc) · 2.58 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
Function Optimize-Text {
[cmdletbinding(DefaultParameterSetName = "default")]
[OutputType([System.String], ParameterSetName = "default")]
[OutputType([psobject], ParameterSetName = "object")]
[Alias("ot")]
Param(
[Parameter(Position = 0, HelpMessage = "Enter some text", ValueFromPipeline, ParameterSetName = "default")]
[Parameter(ParameterSetName = "object")]
[string[]]$Text,
[Parameter(ParameterSetName = "object")]
[Parameter(ParameterSetName = "default")]
[regex]$Filter,
[Parameter(ParameterSetName = "object")]
[string]$PropertyName,
[Alias("comment")]
[Parameter(ParameterSetName = "object")]
[Parameter(ParameterSetName = "default")]
[string]$Ignore,
[Parameter(ParameterSetName = "object")]
[Parameter(ParameterSetName = "default")]
[switch]$ToUpper
)
Begin {
Write-Verbose "Starting $($MyInvocation.Mycommand)"
} #begin
Process {
foreach ($item in $text) {
#filter out items that don't have at least one non-whitespace character
if ($item -match "\S") {
#trim spaces
Write-Verbose "Trimming: $item"
$output = $item.Trim()
if ($Filter) {
Write-Verbose "Filtering"
$output = $output | where-object {$_ -match $filter}
}
#only continue if there is output
if ($output) {
Write-Verbose "Post processing $output"
if ($ToUpper) {
$output = $output.toUpper()
} #if to upper
#filter out if using the comment character
if ($Ignore -AND ($output -match "^[\s+]?$Ignore")) {
Write-Verbose "Ignoring comment $output"
} #if ignore
else {
if ($PropertyName) {
#create a custom object with the specified property
New-Object -TypeName PSObject -Property @{$PropertyName = $Output}
}
else {
#just write the output to the pipeline
$output
}
} #else not ignoring
} #if output
} #if item matches non-whitespce
} #foreach
} #process
End {
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #end function