forked from Get-Nerdio/NerdioManagerPowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNerdioManagerPowerShell.psm1
More file actions
113 lines (107 loc) · 3.87 KB
/
NerdioManagerPowerShell.psm1
File metadata and controls
113 lines (107 loc) · 3.87 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
function Connect-Nme {
[CmdletBinding()]
param(
[string]$ClientId,
[string]$ApiScope,
[string]$ClientSecret,
[guid]$TenantId,
[string]$NmeUri
)
$script:NmeUri = $NmeUri
$script:ClientId = $ClientId
$script:ApiScope = $ApiScope
$script:TenantId = $tenantId
$script:ClientSecret = $ClientSecret
Set-NmeAuthHeaders -Force
if (Test-NmeApi) {
Write-Host "Connection successful."
}
}
function Set-NmeAuthHeaders {
[CmdletBinding()]
param([switch]$Force)
if ($null -eq $Script:AuthHeaders -or ($Script:TokenCreationTime -lt (get-date).AddSeconds(-3000)) -or $Force){
Write-Verbose "Renewing token"
$body = "grant_type=client_credentials&client_id=$ClientId&scope=$ApiScope&client_secret=$ClientSecret"
try {
Write-Verbose "Requesting auth token"
$response = Invoke-RestMethod "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method 'POST' -Body $body
}
catch {
$message = ParseErrorForResponseBody($_)
write-error ($message | Out-String)
}
$Script:TokenCreationTime = get-date
$script:AuthHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$script:AuthHeaders.Add("Authorization", "Bearer $($response.access_token)")
Write-Debug "Renewed NME auth token"
}
else {
Write-Verbose "NME token is current; not renewing"
}
# Write-Debug "NME auth header is $($script:AuthHeaders | out-string)"
}
function ParseErrorForResponseBody($ErrorObj) {
Write-Verbose "Parsing response for error message"
if ($PSVersionTable.PSVersion.Major -lt 6) {
if ($ErrorObj.Exception.Response) {
$Reader = New-Object System.IO.StreamReader($ErrorObj.Exception.Response.GetResponseStream())
$Reader.BaseStream.Position = 0
$Reader.DiscardBufferedData()
$ResponseBody = $Reader.ReadToEnd()
if ($ResponseBody.StartsWith('{')) {
$ResponseBody = $ResponseBody | ConvertFrom-Json
}
Write-Output $ResponseBody
return $ResponseBody.errormessage
}
else {
Write-Debug "No errorobject.exception.response found"
$ErrorObj
}
}
else {
if ($ErrorObj.ErrorDetails.Message) {
return $ErrorObj.ErrorDetails.Message
}
else {
return $ErrorObj
}
}
}
function CapString {
Param(
[string]$String
)
$firstLetter = $string.Substring(0,1).ToUpper()
$restOfTheString = $string.Substring(1)
$string = $firstLetter + $restOfTheString
$string
}
function CapProps {
Param (
[Parameter(ValueFromPipeline=$true)][psobject]$Object
)
BEGIN {}
PROCESS {
if ($Object){
$TypeName = $Object.psobject.typenames | Where-Object {$_ -NotMatch 'System\.'} | select -First 1
$Properties = Get-Member -InputObject $Object | Where-Object MemberType -eq NoteProperty
$NewObject = New-Object -TypeName psobject
foreach ($prop in $Properties) {
if ($prop.Definition -match 'Object\[\]') {
if ($Object.($prop.name)) {
$NewObject | Add-Member -NotePropertyName (CapString $prop.name) -NotePropertyValue ($Object.($prop.name) | CapProps)
}
else {
$NewObject | Add-Member -NotePropertyName (CapString $prop.name) -NotePropertyValue $null
}
}
else {$NewObject | Add-Member -NotePropertyName (CapString $prop.name) -NotePropertyValue $Object.($prop.name)}
}
if ($TypeName) {$NewObject.PSObject.TypeNames.Insert(0, $TypeName)}
return $NewObject
}
}
END{}
}