-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_restdata.ps1
More file actions
192 lines (147 loc) · 6.07 KB
/
get_restdata.ps1
File metadata and controls
192 lines (147 loc) · 6.07 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
# $Id: get_restdata.ps1,v 1.1 2012/03/09 16:06:26 powem Exp $
# Last Modified: $Date: 2012/03/09 16:06:26 $
# $Revision: 1.1 $
# Michael Powe
# WebTrends Technical Account Manager
# 3 March 2012
function Set-RestHash{
param([parameter(Mandatory=$true,
HelpMessage="API version, e.g. v3.")] $version,
[parameter(Mandatory=$true,
HelpMessage="Profile number, e.g. 27211.")] $profile,
[parameter(Mandatory=$true,
HelpMessage="Report number, e.g. 18d039ae036.")] $report,
[parameter(Mandatory=$true,
HelpMessage="Data type: 'agg', 'trend', 'indv'.")] $type,
[parameter(Mandatory=$true,
HelpMessage="totals: 'all', 'none', 'only'.")] $totals,
[parameter(Mandatory=$true,
HelpMessage="format: 'html', 'xml', 'xml2', 'json'.")] $format,
[parameter(Mandatory=$true,
HelpMessage="Measures, e.g. '99' for all measures or '0,1,2' for 1st 3.")] $measures,
[parameter(Mandatory=$true,
HelpMessage="user login in the format domain\login-name")] $user,
[parameter(Mandatory=$true,
HelpMessage="user's login password.")] $pass
)
$restConfig = @{}
$restConfig.version = $version
$restConfig.profile = $profile
$restConfig.report = $report
$restConfig.type = $type
$restConfig.totals = $totals
$restConfig.format = $format
$restConfig.measures = "measures={0}" -f $measures
$restConfig.user = $user
$restConfig.pass = $pass
return $restConfig
}
function Split-RestUrl {
param([parameter(Mandatory = $true)] [string] $restUrl)
$restConfig = @{}
#$URL = "https://ws.webtrends.com/v3/Reporting/profiles/21897/reports/hGgJLBjviR6/?totals=none&start_period=2011m01d01&end_period=2011m01d31&period_type=indv&measures=0*1*2&format=html&suppress_error_codes=true"
$ws_uri_regex = "https?://(?<domain>[a-z]+\.[a-z]+\.com)/(?<version>[a-z0-9]+)/[^/]+/[^/]+/(?<profile>[0-9]+)/reports/[^/]+/\?.*"
$ws_qry_regex = ""
$query = $restUrl.split('?')[1]
$fields = $query.split('&')
return $restConfig
}
function Get-RestData{
<#
.Synopsis
Retrieve report data from WebTrends via REST.
.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}&suppress_error_codes=true" -f `
$Conf.version, `
$Conf.profile, `
$Conf.report, `
$Conf.totals, `
$Start, $End, `
$Conf.type, `
$Conf.measures, `
$Conf.format
$Username = $Conf.user
$Password = $Conf.pass
$UserAgent = $env:USERNAME+":"+$env:COMPUTERNAME
$restData = "" | Select-Object Count,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.Count = ($restData.Data).Length
if ($reader -ne $null){
$reader.Close()
}
if ($response -ne $null){
$response.Close()
}
return $restData
}