-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTM1ps_Common.psm1
418 lines (337 loc) · 13.4 KB
/
TM1ps_Common.psm1
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# ======================================================================================================
# _____ _ _ ___ __
# | | | |\/| / | | |_) ( (`
# |_| |_| | |_| |_| _)_)
#
# Commun functions:
# 'Get-Tm1Servers',
# 'Request-Tm1Login',
# 'Request-Tm1Logout',
# 'Request-Tm1Rest',
# 'Invoke-Tm1ExcelStringReplace'
# ======================================================================================================
# Module variables
$Tm1RestApiVersion = 'v1'
$Tm1Connections = (Get-Content "$PSScriptRoot\config.JSON" | ConvertFrom-Json).connections
$Tm1WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# To disregard the certificate
if ($PSEdition -ne 'Core') {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
}
function Get-Tm1Servers {
<#
.SYNOPSIS
...
.DESCRIPTION
...
.PARAMETER Tm1ConnectionName
Parameter 1
.INPUTS
None. You cannot pipe objects to this function.
.OUTPUTS
...
.EXAMPLE
$Tm1Login = Request-Tm1Login -Tm1ConnectionName 'connection01'
.LINK
https://github.com/ichermak/TM1ps
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory = $true)] [STRING]$Tm1ConnectionName
)
TRY {
# Get informations from the config file
$Tm1AdminHost = $Tm1Connections.$Tm1ConnectionName.adminhost
# Get TM1 servers
$Tm1Protocol = 'https'
$Tm1HttpPortNumber = '5898'
$Tm1RestApiUrl = $Tm1Protocol + '://' + $Tm1AdminHost + ':' + $Tm1HttpPortNumber + '/api' + '/' + $Tm1RestApiVersion
$Tm1RestRequestUrl = $Tm1RestApiUrl + '/' + 'Servers'
$Tm1RestRequestUrl = [System.Web.HttpUtility]::UrlPathEncode($Tm1RestRequestUrl)
if ($PSEdition -ne 'Core') {
$Tm1RestResult = Invoke-RestMethod -WebSession $Tm1WebSession -Method 'GET' -Headers $Tm1Headers -uri $Tm1RestRequestUrl
}
else {
$Tm1RestResult = Invoke-RestMethod -WebSession $Tm1WebSession -SkipCertificateCheck -Method 'GET' -Headers $Tm1Headers -uri $Tm1RestRequestUrl
}
$Tm1RestResult = $Tm1RestResult.value
}
CATCH {
Write-Error "$($_.Exception.Message)`n$($_.ErrorDetails.Message)"
Break
}
return $Tm1RestResult
}
function Request-Tm1Login {
<#
.SYNOPSIS
...
.DESCRIPTION
...
.PARAMETER Tm1ConnectionName
Parameter 1
.INPUTS
None. You cannot pipe objects to this function.
.OUTPUTS
...
.EXAMPLE
$Tm1Login = Request-Tm1Login -Tm1ConnectionName 'connection01'
.LINK
https://github.com/ichermak/TM1ps
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory = $true)] [STRING]$Tm1ConnectionName
)
TRY {
# Get informations from the config file
$Tm1AdminHost = $Tm1Connections.$Tm1ConnectionName.adminhost
$Tm1HttpPortNumber = $Tm1Connections.$Tm1ConnectionName.httpportnumber
$Tm1User = $Tm1Connections.$Tm1ConnectionName.user
$Tm1Password = $Tm1Connections.$Tm1ConnectionName.password
$Tm1UseSsl = $Tm1Connections.$Tm1ConnectionName.usessl
$Tm1CamNameSpace = $Tm1Connections.$Tm1ConnectionName.camnamespace
# Set Authorization
if ($Tm1CamNameSpace) {
$Tm1Headers = @{
"Authorization" = 'CAMNamespace ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($Tm1User):$($Tm1Password)"));
"Content-Type" = "application/json"
}
}
else {
$Tm1Headers = @{
"Authorization" = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($Tm1User):$($Tm1Password)"));
"Content-Type" = "application/json"
}
}
# Set Protocol
if ($Tm1UseSsl.ToLower() -eq 'true') {
$Tm1Protocol = "https"
}
else {
$Tm1Protocol = "http"
}
# Establish the connection
$Tm1RestApiUrl = $Tm1Protocol + "://" + $Tm1AdminHost + ":" + $Tm1HttpPortNumber + "/api"
$Tm1RestRequestUrl = $Tm1RestApiUrl + '/' + $Tm1RestApiVersion + '/' + 'ActiveSession'
$Tm1RestRequestUrl = [System.Web.HttpUtility]::UrlPathEncode($Tm1RestRequestUrl)
if ($PSEdition -ne 'Core') {
$Tm1LoginResult = Invoke-RestMethod -WebSession $Tm1WebSession -Method 'GET' -Headers $Tm1Headers -uri $Tm1RestRequestUrl
}
else {
$Tm1LoginResult = Invoke-RestMethod -WebSession $Tm1WebSession -SkipCertificateCheck -Method 'GET' -Headers $Tm1Headers -uri $Tm1RestRequestUrl
}
}
CATCH {
Write-Error "$($_.Exception.Message)`n$($_.ErrorDetails.Message)"
Break
}
return $Tm1LoginResult
}
function Request-Tm1Logout {
<#
.SYNOPSIS
...
.DESCRIPTION
...
.PARAMETER Tm1ConnectionName
Parameter 1
.INPUTS
None. You cannot pipe objects to this function.
.OUTPUTS
...
.EXAMPLE
$Tm1Logout = Request-Tm1Logout -Tm1ConnectionName 'connection01'
.LINK
https://github.com/ichermak/TM1ps
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory = $true)] [STRING]$Tm1ConnectionName
)
TRY {
# Get informations from the config file
$Tm1AdminHost = $Tm1Connections.$Tm1ConnectionName.adminhost
$Tm1HttpPortNumber = $Tm1Connections.$Tm1ConnectionName.httpportnumber
$Tm1UseSsl = $Tm1Connections.$Tm1ConnectionName.usessl
# Set Protocol
if ($Tm1UseSsl.ToLower() -eq 'true') {
$Tm1Protocol = "https"
}
else {
$Tm1Protocol = "http"
}
# Logout
$Tm1RestApiUrl = $Tm1Protocol + "://" + $Tm1AdminHost + ":" + $Tm1HttpPortNumber + "/api"
$Tm1RestRequestUrl = $Tm1RestApiUrl + '/' + 'logout'
$Tm1RestRequestUrl = [System.Web.HttpUtility]::UrlPathEncode($Tm1RestRequestUrl)
if ($PSEdition -ne 'Core') {
$Tm1LogoutResult = Invoke-RestMethod -WebSession $Tm1WebSession -Method 'GET' -uri $Tm1RestRequestUrl
}
else {
$Tm1LogoutResult = Invoke-RestMethod -WebSession $Tm1WebSession -SkipCertificateCheck -Method 'GET' -uri $Tm1RestRequestUrl
}
}
CATCH {
Write-Error "$($_.Exception.Message)`n$($_.ErrorDetails.Message)"
Break
}
return $Tm1LogoutResult
}
function Request-Tm1Rest {
<#
.SYNOPSIS
Allows to launch Tm1 Rest request.
.DESCRIPTION
Allows to launch Tm1 Rest request using a connection specified in the configuration file.
.PARAMETER Tm1ConnectionName
Parameter 1
.PARAMETER Tm1RestMethod
Parameter 2
.PARAMETER Tm1RestRequest
Parameter 3
.PARAMETER Tm1RestBody
Parameter 4
.INPUTS
None. You cannot pipe objects to this function.
.OUTPUTS
...
.EXAMPLE
Request-Tm1Rest -Tm1ConnectionName 'connection01' -Tm1RestMethod 'GET' -Tm1RestRequest 'Cubes?$select=Name&$expand=Dimensions($select=Name)'
.LINK
https://github.com/ichermak/TM1ps
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory = $true, Position = 1)] [STRING]$Tm1ConnectionName,
[Parameter(Mandatory = $true, Position = 2)] [STRING]$Tm1RestMethod,
[Parameter(Mandatory = $true, Position = 3)] [STRING]$Tm1RestRequest,
[Parameter(Mandatory = $false, Position = 4)] [STRING]$Tm1RestBody
)
TRY {
# Get informations from the config file
$Tm1AdminHost = $Tm1Connections.$Tm1ConnectionName.adminhost
$Tm1HttpPortNumber = $Tm1Connections.$Tm1ConnectionName.httpportnumber
$Tm1UseSsl = $Tm1Connections.$Tm1ConnectionName.usessl
# Set Protocol
if ($Tm1UseSsl.ToLower() -eq 'true') {
$Tm1Protocol = "https"
}
else {
$Tm1Protocol = "http"
}
# Build the rest request url
$Tm1RestApiUrl = $Tm1Protocol + "://" + $Tm1AdminHost + ":" + $Tm1HttpPortNumber + "/api"
$Tm1RestRequestUrl = $Tm1RestApiUrl + '/' + $Tm1RestApiVersion + '/' + $Tm1RestRequest
$Tm1RestRequestUrl = [System.Web.HttpUtility]::UrlPathEncode($Tm1RestRequestUrl)
# Execute the rest request
if ($Tm1RestBody) {
if ($PSEdition -ne 'Core') {
$Tm1RestRequestResult = Invoke-RestMethod -WebSession $Tm1WebSession -Method $Tm1RestMethod -uri $Tm1RestRequestUrl -Body $Tm1RestBody
}
else {
$Tm1RestRequestResult = Invoke-RestMethod -WebSession $Tm1WebSession -SkipCertificateCheck -Method $Tm1RestMethod -uri $Tm1RestRequestUrl -Body $Tm1RestBody
}
}
else {
if ($PSEdition -ne 'Core') {
$Tm1RestRequestResult = Invoke-RestMethod -WebSession $Tm1WebSession -Method $Tm1RestMethod -uri $Tm1RestRequestUrl
}
else {
$Tm1RestRequestResult = Invoke-RestMethod -WebSession $Tm1WebSession -SkipCertificateCheck -Method $Tm1RestMethod -uri $Tm1RestRequestUrl
}
}
}
CATCH {
Write-Error "$($_.Exception.Message)`n$($_.ErrorDetails.Message)"
Break
}
return $Tm1RestRequestResult
}
function Invoke-Tm1ExcelStringReplace {
<#
.SYNOPSIS
...
.DESCRIPTION
...
.PARAMETER Tm1FilePath
Parameter 1
.PARAMETER Tm1OldString
Parameter 2
.PARAMETER Tm1NewString
Parameter 3
.PARAMETER Tm1DestinationPath
Parameter 4
.INPUTS
None. You cannot pipe objects to this function.
.OUTPUTS
...
.EXAMPLE
...
.LINK
https://github.com/ichermak/TM1ps
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory = $true, Position = 1)] [ValidateScript({Test-Path $_ -PathType Any})] [System.IO.DirectoryInfo]$Tm1FilePath,
[Parameter(Mandatory = $true, Position = 2)] [STRING]$Tm1OldString,
[Parameter(Mandatory = $true, Position = 3)] [STRING]$Tm1NewString,
[Parameter(Mandatory = $false, Position = 4)] [System.IO.DirectoryInfo]$Tm1DestinationPath
)
TRY {
# Test if Tm1DestinationPath is empty use the same folder as Tm1FilePath
if (-Not ($Tm1DestinationPath)) {
if (Test-Path $Tm1FilePath -PathType Container) {
$Tm1DestinationPath = $Tm1FilePath
}
else {
$Tm1DestinationPath = Split-Path $Tm1FilePath
}
}
$Tm1DestinationPath = [System.IO.DirectoryInfo]($Tm1DestinationPath.Parent.FullName + "\" + $Tm1DestinationPath.Name)
# Set a unique temporary path
$Tm1TempPath = $Tm1DestinationPath.FullName + "\" + "Temp_" + (Get-Random).ToString()
while (Test-Path $Tm1TempPath -PathType Container) {
$Tm1TempPath = $Tm1DestinationPath.FullName + "\" + "Temp_" + (Get-Random).ToString()
}
# Get the excel files collection
$Tm1ExcelFiles = get-childitem -Path $Tm1FilePath -Recurse -File | Where-Object {$_.fullName -Match '.xls'}
foreach ($Tm1ExcelFile in $Tm1ExcelFiles) {
# Expand the excel file to temp folder
Expand-Archive -Path $Tm1ExcelFile.fullName -DestinationPath $Tm1TempPath -Force
# Search substring and replace it
$XmlFiles = get-childitem -Path $Tm1DestinationPath -Recurse -File | Where-Object {$_.fullName -Match '.xml'}
foreach ($XmlFile in $XmlFiles) {
if (-Not ($XmlFile.Name -eq '[Content_Types].xml')) {
(Get-Content $XmlFile.fullName) | Foreach-Object {$_ -replace $Tm1OldString, $Tm1NewString} | Set-Content $XmlFile.fullName
}
}
# Compress the content of the temp folder
$Tm1ExcelFile = $Tm1DestinationPath.FullName + '\' + $Tm1ExcelFile.Name
Get-ChildItem -Path $Tm1TempPath | Compress-Archive -DestinationPath $Tm1ExcelFile -CompressionLevel NoCompression -Force
}
}
CATCH {
Write-Error "$($_.Exception.Message)"
Break
}
FINALLY {
# Delete the temp folder
if (Test-Path $Tm1TempPath) {
Remove-Item -Path $Tm1TempPath -Recurse -Force
}
}
}