-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwt_api_extract.ps1
More file actions
281 lines (205 loc) · 7.55 KB
/
wt_api_extract.ps1
File metadata and controls
281 lines (205 loc) · 7.55 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
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
270
271
272
273
274
275
276
277
278
279
280
281
<#
.Synopsis
Script to retrieve transaction data from Webtrends API.
.Description
This script pulls data from Webtrends API.
.Parameter aStart
The start date in the standard WebTrends web services format, e.g. "2012m09d01"
.Parameter aEnd
The end date in the standard Webtrends web services format, e.g. "2012m09d30"
.Parameter aProfile
The profile GUID.
.Parameter aReport
Optional parameter that takes the report GUID. Default is to pull from the
original Transaction ID error report.
.Parameter aQuery
Optional string for the query field to filter the report. If not present, the
entire report is pulled.
.Example
.\zinio_transaction_debug.ps1 "2012m09d01" "2012m09d30"
Pull the data from the default report for the date period 1 Sept 2012 to 30 Sept 2012
.Example
.\zinio_transaction_debug.ps1 "2012m09d01" "2012m09d30" "vqW1RZRkdX6"
Pull the data from the new report with ETS for the same date period.
.Example
.\zinio_transaction_debug.ps1 "2012m10d10" "2012m10d10"
Pull the data for the single day 10 Oct 2012.
.Outputs
Writes a file to the user desktop with the data. Uses $env:USERPROFILE environmental
variable to find the desktop. The output file is datestamped, e.g.
zinio_transactions_20121010_122802.html. Output is in HTML.
.Notes
Report GUIDS
WPAHs5Ljtv6 - Transaction ID Event Trace All Events (default)
vqW1RZRkdX6 - Transaction ID Event Trace All Events with ETS
$Id$
Last revised: $Date$
Version when it left home: $Revision$
Author: Michael Powe, Webtrends Technical Account Manager
#>
param(
[parameter(Mandatory=$true)]
[string]$aStart,
[parameter(Mandatory=$true)]
[string]$aEnd,
[parameter(Mandatory=$true)]
[string]$aProfile,
[parameter(Mandatory=$false)]
[string]$aReport,
[parameter(Mandatory=$false)
[string]$aQuery);
#region Declarations and Functions
function Set-wtRestConfiguration{
begin{
[string]$report = "";
if(-not($aReport)){
$report = 'WPAHs5Ljtv6';
} else {
$report = $aReport;
}
}
process{
$restConfig = @{
user="zinio\wt-mpowe";
pass="Pa55w0rd@";
version = 'v3';
profile = '36519';`
report = $report;
type = 'indv';
totals = 'none';
format = 'html';
measures = '0';
# query = '';
query = '[Transaction ID] LIKE com.* AND [iOS Transaction ID TimeStamp] NEQ "None"';
}
}
end{return $restConfig;}
}
function Get-RestData{
<#
.Synopsis
Retrieve report data from WebTrends via REST API.
.Description
A simple retrieval mechanism for REST data from WebTrends API. Pass in
the start and end dates in proper format. Use a hashtable for the other
essential values. This function builds the data request for report data
only.
The data is collected in an object, which is returned from the function.
The object contains a property 'Count', which can be used to see the size
of the returned data string. The property 'Data' contains the full string
returned in response to the request.
.Parameter Start
The start date in proper format, e.g. 2011m01d01.
.Parameter End
The end date in proper format.
.Parameter Conf
A hashtable containing configuration information for the data pull. The
hashtable should contain the following elements:
version,profile ID, report GUID, type (agg,indv or trend), totals
(all, none or only), format (html, json, xml or xml2), measures
(measures=0*1*3 or measures=99 for all measures), username (acct\login),
password.
-----
$restConfig = @{version="v3";
profile="21897";
report="hGgJLBjviR6";
type="indv";
totals="none";
format="html";
measures="measures=0*1*2";
user="acct\login";
pass="password"}
.Outputs
An object that contains the data retrieved and a count indicating the length of the data.
.Notes
This function uses the StreamReader method ReadToEnd(), so a huge data response may break.
The usual breakage is that the server closes the stream. I assume that this happens
because the server has a built-in limit to the amount of data it will return in a single
request. The solution is to chunk up the requests and then reassemble the chunks after
they've all been retrieved.
Some trivial exception handling is included, which may prevent falling into an infinite
loop of errors if something goes wrong.
#>
param (
[parameter(Mandatory = $true)] [string]$Start,
[parameter(Mandatory = $true)] [string]$End,
[parameter(Mandatory = $true)] [hashtable]$Conf)
$version = $Conf.version
$profile = $Conf.profile
$report = $Conf.report
$type = $Conf.type
$totals = $Conf.totals
$format = $Conf.format
# put in a nonsense number to include all measures
$measures = $Conf.measures
#$URL = "https://ws.webtrends.com/$version/Reporting/profiles/$profile/reports/$report/?totals=$totals&start_period=$start&end_period=$end&period_type=$type&$measures&format=$format&suppress_error_codes=true"
$URL = "https://ws.webtrends.com/{0}/Reporting/profiles/{1}/reports/{2}/?totals={3}&start_period={4}&end_period={5}&period_type={6}&{7}&format={8}&query={9}&suppress_error_codes=true" -f `
$Conf.version, `
$Conf.profile, `
$Conf.report, `
$Conf.totals, `
$Start, $End, `
$Conf.type, `
$Conf.measures, `
$Conf.format, `
[Web.Httputility]::UrlEncode($Conf.query)
$Username = $Conf.user
$Password = $Conf.pass
$UserAgent = $env:USERNAME+":"+$env:COMPUTERNAME
$restData = New-Object -TypeName psobject -Property @{Length=0; Data=""};
$URI = New-Object System.Uri($URL,$true)
$request = [System.Net.HttpWebRequest]::Create($URI)
$request.UserAgent = $(
"{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent,
$(if($Host.Version){$Host.Version}else{"1.0"}),
[Environment]::Version,
[Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win"))
#Establish the credentials for the request
$creds = New-Object System.Net.NetworkCredential($Username,$Password)
$request.Credentials = $creds
# Thrown if the request times out
trap [System.Net.WebException] { "A web exception was thrown, possibly caused by a timeout: $_"; break; }
$response = $request.GetResponse()
$reader = [IO.StreamReader] $response.GetResponseStream()
# thrown by ReadToEnd()
trap [System.IO.IOException] { "An IO exception occurred on StreamReader: $_"; break }
trap [System.OutOfMemoryException] { "An OutOfMemoryException occurred on StreamReader: $_"; break }
$responseHTML = $reader.ReadToEnd()
$restData.Data = $responseHTML
$restData.Length = ($restData.Data).Length
if ($reader -ne $null){
$reader.Close()
}
if ($response -ne $null){
$response.Close()
}
return $restData
} # end get-restdata
function Get-wtiOSTransactions{
param(
[parameter(Mandatory=$false)]
[alias("s")]
[string]$Start = $aStart,
[parameter(Mandatory=$false)]
[alias("e")]
[string]$End = $aEnd
)
begin{
$Conf = Set-wtRestConfiguration;
$startdate = $aStart;
$enddate = $aEnd;
$data = New-Object -TypeName psobject;
$datestamp = Get-Date -UFormat "%Y%m%d_%H%M%S";
}
process{
$data = Get-RestData -Start $startdate -End $enddate -Conf $Conf;
}
end{
Write-Host ("Content length returned is {0} bytes." -f $data.Length);
$data.Data | sc ("C:\Users\powem\Desktop\zinio_transactions_{0}.html" -f $datestamp)
}
} # end get-wtiostransactions
#endregion
#region Data Collection
Get-wtiOSTransactions
#endregion