forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertfrom-text.ps1
More file actions
134 lines (118 loc) · 5.31 KB
/
convertfrom-text.ps1
File metadata and controls
134 lines (118 loc) · 5.31 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
Function ConvertFrom-Text {
[cmdletbinding(DefaultParameterSetname = "File")]
[alias("cft")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a regular expression pattern that uses named captures")]
[ValidateScript( {
if (($_.GetGroupNames() | Where-Object {$_ -notmatch "^\d{1}$"}).Count -ge 1) {
$True
}
else {
Throw "No group names found in your regular expression pattern."
}
})]
[Alias("regex", "rx")]
[regex]$Pattern,
[Parameter(Position = 1, Mandatory, ParameterSetName = 'File')]
[ValidateScript( {Test-Path $_})]
[alias("file")]
[string]$Path,
[Parameter(Position = 1, Mandatory, ValueFromPipeline, ParameterSetName = 'Inputobject')]
[ValidateNotNullorEmpty()]
[ValidateScript( {
if ($_ -match "\S+") {
$true
}
else {
Throw "Cannot process an empty or null line of next."
$false
}
})]
[string]$InputObject,
[Parameter(HelpMessage = "Enter an optional typename for the object output.")]
[ValidateNotNullorEmpty()]
[string]$TypeName,
[Parameter(HelpMessage = "Do not use Write-Progress to report on processing. This can improve performance on large data sets.")]
[switch]$NoProgress
)
Begin {
$begin = Get-Date
Write-Verbose "$((Get-Date).TimeOfDay) Starting $($MyInvocation.Mycommand)"
Write-Verbose "$((Get-Date).TimeOfDay) Using pattern $($pattern.ToString())"
if ($NoProgress) {
Write-Verbose "$((Get-Date).TimeOfDay) Suppressing progress bar"
$ProgressPreference = "SilentlyContinue"
}
#Get the defined capture names
$names = $pattern.GetGroupNames() | Where-Object {$_ -notmatch "^\d+$"}
Write-Verbose "$((Get-Date).TimeOfDay) Using names: $($names -join ',')"
#define a hashtable of parameters to splat with Write-Progress
$progParam = @{
Activity = $myinvocation.mycommand
Status = "pre-processing"
}
} #begin
Process {
If ($PSCmdlet.ParameterSetName -eq 'File') {
Write-Verbose "$((Get-Date).TimeOfDay) Processing $Path"
Try {
$progParam.CurrentOperation = "Getting content from $path"
$progParam.Status = "Processing"
Write-Progress @progParam
$content = Get-Content -path $path | Where-Object {$_ -match "\S+"}
Write-Verbose "$((Get-Date).TimeOfDay) Will process $($content.count) entries"
} #try
Catch {
Write-Warning "Could not get content from $path. $($_.Exception.Message)"
Write-Verbose "$((Get-Date).TimeOfDay) Exiting function"
#Bail out
Return
}
} #if file parameter set
else {
Write-Verbose "$((Get-Date).TimeOfDay) processing input: $Inputobject"
$content = $InputObject
}
if ($content) {
Write-Verbose "$((Get-Date).TimeOfDay) processing content"
$content | foreach-object -begin {$i = 0} -process {
#calculate percent complete
$i++
$pct = ($i / $content.count) * 100
$progParam.PercentComplete = $pct
$progParam.Status = "Processing matches"
Write-Progress @progParam
#process each line of the text file
foreach ($match in $pattern.matches($_)) {
Write-Verbose "$((Get-Date).TimeOfDay) processing match"
$progParam.currentoperation = $match
Write-Progress @progParam
#get named matches and create a hash table for each one
$progParam.Status = "Creating objects"
Write-Verbose "$((Get-Date).TimeOfDay) creating objects"
$hash = [ordered]@{}
if ($TypeName) {
Write-Verbose "$((Get-Date).TimeOfDay) using a custom property name of $Typename"
$hash.Add("PSTypeName",$Typename)
}
foreach ($name in $names) {
$progParam.CurrentOperation = $name
Write-Progress @progParam
Write-Verbose "$((Get-Date).TimeOfDay) getting $name"
#initialize an ordered hash table
#add each name as a key to the hash table and the corresponding regex value
$hash.Add($name, $match.groups["$name"].value)
}
Write-Verbose "$((Get-Date).TimeOfDay) writing object to pipeline"
#write a custom object to the pipeline
[pscustomobject]$hash
}
} #foreach line in the content
} #if $content
} #process
End {
Write-Verbose "$((Get-Date).TimeOfDay) Ending $($MyInvocation.Mycommand)"
$end = Get-Date
Write-Verbose "$((Get-Date).TimeOfDay) Total processing time $($end-$begin)"
} #end
} #end function