This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
commons.ps1
75 lines (65 loc) · 2.61 KB
/
commons.ps1
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
<#
© 2018 Microsoft Corporation. All rights reserved. This sample code is not supported under any Microsoft standard support program or service.
This sample code is provided AS IS without warranty of any kind. Microsoft disclaims all implied warranties including, without limitation,
any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance
of the sample code and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business
profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the
sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#>
# This variable is used in other cmdlets to identify the REST client.
$ApplicationName = "Partner Center PowerShell Module v0.10.0.4"
function _applyTypes {
param($item,$type)
$item.PSObject.TypeNames.Insert(0, $type)
}
function _formatResult {
param ($obj, $type)
try
{
if($obj.items)
{
foreach($item in $obj.items){_applyTypes -item $item -type $type}
return $obj.items
}
}
catch
{
_applyTypes -item $obj -type $type
return $obj
}
}
function _testTenantContext($TenantId)
{
if ($TenantId.Length -lt 1)
{
throw ">>> Use Select-PCCustomer to select a specific tenant or use -TenantId parameter<<<"
}
}
function _testTokenContext($SaToken)
{
if ($SaToken.Length -lt 1)
{
throw ">>> Use Add-PCAuthentication to login to Partner Center or use -SaToken parameter<<<"
}
}
function _unsecureString
{
param ([SecureString]$string)
$tmp_cred = New-Object System.Management.Automation.PSCredential -ArgumentList "tmp", $string
return $tmp_cred.GetNetworkCredential().Password
}
function Get-SAToken
{
[CmdletBinding()]
param ($AadToken,[bool]$global)
$url = "https://api.partnercenter.microsoft.com/generatetoken"
$body = "grant_type=jwt_token"
$headers=@{Authorization="Bearer $AadToken"}
$response = Invoke-RestMethod -Uri $url -ContentType "application/x-www-form-urlencoded" -Headers $headers -Body $body -method "POST" #-Debug -Verbose
#setting SAToken variable as global
if ($global){
Set-Variable -Name "GlobalToken" -Value $response.access_token -Scope Global
}
return $response.access_token
}