-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanSilkWebScript.ps1
More file actions
158 lines (138 loc) · 11.1 KB
/
CleanSilkWebScript.ps1
File metadata and controls
158 lines (138 loc) · 11.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
######################################################################################################################################
# Powershell Silk Performer Cleanup Script #
# Creator: Erik Post #
# Version: V4 #
# #
# Description: #
# This tool is build to automate some manual labor done after a new recording for Silk Web Scripts. #
# Please keep in mind that it is made for NEW recordings and does not work on existing script! #
# #
# Change Log: #
# 2020-04-09 V1: Initial log #
# 2020-04-09 V2: Added restoration of the <USE_HTML_VAL> in Forms after removing "" #
# 2020-04-15 V3: Removed removing of "" and restore actions #
# Added commenting of truelog sections and switch for it #
# Added commenting of static content and switch for it #
# Added switch for commenting cookies #
# 2020-04-24 V4: Fixed too greedy newline removal #
# #
# KNOWN ISSUES: #
# 1. Path for Source and Destination cannot handle quotes, this is required for locations with spaces #
# #
# Known possible improvements: #
# 1. Accept quotes in Source and Destination Files (required for location with spaces #
# 2. Create function with params #
# 2.1 Regex Pattern #
# 2.2 Replacement pattern #
# 2.3 Output text #
# 3. Cut long lines to smaller ones again #
# 4. Remove comment lines #
# #
# Things that are being picked up by the script #
# - Remove Silkmade newlines #
# - Remove Minimum Mean Time for all Web calls (Timers at the end of a Web call) #
# - Remove ThinkTimes #
# - Remove regular newlines #
# - Remove newlines created by removing ThinkTimes #
# - Replace Epoch timestamp(ms) with GetTimeStamp function #
# - Remove the " " which is created in the forms section after removing newlines #
# - Comment truelog sections #
# - Comment static content png css js svg bmp #
# - Comment all the WebCookieSet's #
# #
######################################################################################################################################
param([Parameter(Mandatory=$true)] $SourceFile, [Parameter(Mandatory=$true)] $DestinationFile, $CommentTruelog=$true, $CommentStaticData=$true, $CommentCookies=$true, $DebugEnabled=$true)
# Testing SourceFile location
$FileExist = Test-Path -Path $SourceFile
if ( -Not $FileExist)
{
Write-Error "Could not find sourcefile at location '$SourceFile' aborting"
exit 1
}
# Create the Print output Object
$PrintOutputObject = @()
# Read file
$Input = (Get-Content -raw -Path $SourceFile)
#--------------------------------
# Remove Silk Comments - TESTING
#$NumberOfMatches = Select-String -InputObject $Input -Pattern "`r`n$" -AllMatches
#$NumberOfMatches = $NumberOfMatches.Matches.Count
#$PrintOutputObject += new-object psobject -property @{Text="Comment Lines";"#Found"="$NumberOfMatches"}
#$Input = $Input -replace "^//.*",""
#--------------------------------
# Remove Silkmade newlines
# Explanation: regex starts with checking for certain character to be available before the \r\n, newline shouls either be 6 or 7 spaces and then should not contain \\h (binary)
$NumberOfMatches = Select-String -InputObject $Input -Pattern "(`"|, |,|\(|\w)`r`n( | )(?!`"\\h)" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Remove Silkmade newlines";"#Found"="$NumberOfMatches"}
$Input = $Input -replace "(`"|, |,|\(|\w)`r`n( | )(?!`"\\h)",'$1'
# Remove Minimum Mean Time for all Web calls
$NumberOfMatches = Select-String -InputObject $Input -Pattern ", [0-9]*\.[0-9]*\);" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="URL webtimes found";"#Found"="$NumberOfMatches"}
$Input = $Input -Replace ", [0-9]*\.[0-9]*\);", ");"
# Remove ThinkTimes
$NumberOfMatches = Select-String -InputObject $Input -Pattern "ThinkTime\([\d]\.[\d]\);" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Thinktimes found";"#Found"="$NumberOfMatches"}
$Input = $Input -replace "ThinkTime\([\d]\.[\d]\);",""
if ($CommentCookies)
{
# Comment all the WebCookieSet's
$NumberOfMatches = Select-String -InputObject $Input -Pattern "WebCookieSet" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Web Cookies found";"#Found"="$NumberOfMatches"}
$Input = $Input -replace "WebCookieSet","//WebCookieSet"
}
if ($CommentStaticData)
{
# Comment all the static content like css and image files
$NumberOfMatches = Select-String -InputObject $Input -Pattern 'Web(.*)(png"|css"|js"|svg"|bmp")' -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Static Content";"#Found"="$NumberOfMatches"}
$Input = $Input -replace 'Web(.*)(png"|css"|js"|svg"|bmp")','//Web$1$2'
}
if ($CommentTruelog)
{
# Comment the truelog lines
$NumberOfMatches = Select-String -InputObject $Input -Pattern "Truelog" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Truelog sections";"#Found"="$NumberOfMatches"}
$Input = $Input -replace "Truelog","//Truelog"
}
# Remove regular newlines
$NumberOfMatches = Select-String -InputObject $Input -Pattern ";`r`n `r`n Web" -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Normal newlines";"#Found"="$NumberOfMatches"}
$Input = $Input -Replace ";`r`n `r`n Web",";`r`n Web"
# Remove newlines created by removing ThinkTimes
$NumberOfMatches = Select-String -InputObject $Input -Pattern " `r`n " -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Newlines from removing ThinkTimes";"#Found"="$NumberOfMatches"}
$Input = $Input -Replace " `r`n "," "
# Replace Epoch timestamp(ms) with GetTimeStamp function
$NumberOfMatches = Select-String -InputObject $Input -Pattern '(:= "\d{13}")([,|;])' -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Epoch Timestamps";"#Found"="$NumberOfMatches"}
$Input = $Input -Replace '(:= "\d{13}")([,|;])',':= GetTimestamp(TIMESTAMP_IN_MS)$2 // $1$2'
# Remove the " " which is created in the forms section after removing newlines
$NumberOfMatches = Select-String -InputObject $Input -Pattern '" "' -AllMatches
$NumberOfMatches = $NumberOfMatches.Matches.Count
$PrintOutputObject += new-object psobject -property @{Text="Double quotes with spaces";"#Found"="$NumberOfMatches"}
$Input = $Input -Replace '" "',""
#Write output to console
if ($DebugEnabled)
{
Write-output -InputObject $PrintOutputObject
}
#--------------------------------
# TESTING for large lines to be splitted
#$NumberOfMatches = Select-String -InputObject $Input -Pattern '([\W]{50})(.*)' -AllMatches
#Write-Output "------"
#$NumberOfMatches = $NumberOfMatches.Matches.Count
#Write-Output "TESTING: $NumberOfMatches"
#$Input = $Input -Replace '([\W]{50})(.*)','TESTING: $2'
#Write-Output "TESTING: $1"
#--------------------------------
# Write to file
$Input > $DestinationFile