Skip to content

Commit 641f22f

Browse files
committed
update msgraph api wrapper
1 parent 2646aae commit 641f22f

4 files changed

Lines changed: 70 additions & 49 deletions

File tree

core/api/entraid/msgraph/api/Get-MonkeyMSGraphObject.ps1

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
12+
# See the License for the specIfic language governing permissions and
1313
# limitations under the License.
1414

1515
Function Get-MonkeyMSGraphObject{
@@ -84,121 +84,124 @@ Function Get-MonkeyMSGraphObject{
8484
[parameter(Mandatory=$False, HelpMessage='POST Data object')]
8585
[Object]$Data,
8686

87+
[parameter(Mandatory=$False, HelpMessage='Return raw response')]
88+
[Switch]$RawResponse,
89+
8790
[parameter(Mandatory=$False, HelpMessage='API Version')]
8891
[String]$APIVersion = "v1.0"
8992
)
9093
Begin{
9194
$Verbose = $Debug = $False;
9295
$InformationAction = 'SilentlyContinue'
93-
if($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters.Verbose){
96+
If($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters.Verbose){
9497
$Verbose = $True
9598
}
96-
if($PSBoundParameters.ContainsKey('Debug') -and $PSBoundParameters.Debug){
99+
If($PSBoundParameters.ContainsKey('Debug') -and $PSBoundParameters.Debug){
97100
$Debug = $True
98101
}
99-
if($PSBoundParameters.ContainsKey('InformationAction')){
102+
If($PSBoundParameters.ContainsKey('InformationAction')){
100103
$InformationAction = $PSBoundParameters['InformationAction']
101104
}
102-
if($null -eq $Authentication){
105+
If($null -eq $Authentication){
103106
Write-Warning -Message ($message.NullAuthenticationDetected -f "Microsoft Graph API")
104107
break
105108
}
106109
#Get Authorization Header
107110
$methods = $Authentication | Get-Member | Where-Object {$_.MemberType -eq 'Method'} | Select-Object -ExpandProperty Name
108111
#Get Authorization Header
109-
if($null -ne $methods -and $methods.Contains('CreateAuthorizationHeader')){
112+
If($null -ne $methods -and $methods.Contains('CreateAuthorizationHeader')){
110113
$AuthHeader = $Authentication.CreateAuthorizationHeader()
111114
}
112-
else{
115+
Else{
113116
$AuthHeader = ("Bearer {0}" -f $Authentication.AccessToken)
114117
}
115118
#$AuthHeader = ("Bearer {0}" -f $Authentication.AccessToken)
116119
#set msgraph uri
117120
$base_uri = ("/{0}" -f $APIVersion)
118121
$my_filter = $null
119122
#construct query
120-
if($Expand){
121-
if($null -ne $my_filter){
123+
If($Expand){
124+
If($null -ne $my_filter){
122125
$my_filter = ('{0}&$expand={1}' -f $my_filter, $Expand)
123126
}
124-
else{
127+
Else{
125128
$my_filter = ('?$expand={0}' -f $Expand)
126129
}
127130
}
128-
if($Filter){
129-
if($null -ne $my_filter){
131+
If($Filter){
132+
If($null -ne $my_filter){
130133
$my_filter = ('{0}&$filter={1}' -f $my_filter, [uri]::EscapeDataString($Filter))
131134
}
132-
else{
135+
Else{
133136
$my_filter = ('?$filter={0}' -f [uri]::EscapeDataString($Filter))
134137
}
135138
}
136-
if($Select){
137-
if($null -ne $my_filter){
139+
If($Select){
140+
If($null -ne $my_filter){
138141
$my_filter = ('{0}&$select={1}' -f $my_filter, (@($Select) -join ','))
139142
}
140-
else{
143+
Else{
141144
$my_filter = ('?$select={0}' -f (@($Select) -join ','))
142145
}
143146
}
144-
if($orderBy){
145-
if($null -ne $my_filter){
147+
If($orderBy){
148+
If($null -ne $my_filter){
146149
$my_filter = ('{0}&$orderby={1}' -f $my_filter, $orderBy)
147150
}
148-
else{
151+
Else{
149152
$my_filter = ('?$orderby={0}' -f $orderBy)
150153
}
151154
}
152-
if($Top){
153-
if($null -ne $my_filter){
155+
If($Top){
156+
If($null -ne $my_filter){
154157
$my_filter = ('{0}&$top={1}' -f $my_filter, $Top)
155158
}
156-
else{
159+
Else{
157160
$my_filter = ('?$top={0}' -f $Top)
158161
}
159162
}
160-
if($Count){
161-
if($null -ne $my_filter){
163+
If($Count){
164+
If($null -ne $my_filter){
162165
$my_filter = ('{0}&$count=true' -f $my_filter)
163166
}
164-
else{
167+
Else{
165168
$my_filter = ('?$count=true' -f $Top)
166169
}
167170
}
168-
if($me){
171+
If($me){
169172
$base_uri = ("{0}/me" -f $base_uri)
170173
}
171-
if($ObjectType){
174+
If($ObjectType){
172175
$base_uri = ("{0}/{1}" -f $base_uri, $ObjectType)
173176
}
174-
if($ObjectId){
177+
If($ObjectId){
175178
$base_uri = ("{0}/{1}" -f $base_uri, $ObjectId)
176179
}
177180
#Append filter to query
178-
if($my_filter){
181+
If($my_filter){
179182
$base_uri = ("{0}{1}" -f $base_uri,$my_filter)
180183
}
181184
#Construct final URI
182185
$Server = ("{0}" -f $Environment.Graphv2.Replace('https://',''))
183186
$final_uri = ("{0}{1}" -f $Server,$base_uri)
184187
$final_uri = [regex]::Replace($final_uri,"/+","/")
185188
$final_uri = ("https://{0}" -f $final_uri.ToString())
186-
if($RawQuery){
187-
if($my_filter){
189+
If($RawQuery){
190+
If($my_filter){
188191
$final_uri = ("{0}{1}" -f $RawQuery,$my_filter)
189192
}
190-
else{
193+
Else{
191194
$final_uri = ("{0}" -f $RawQuery)
192195
}
193196
}
194197
}
195198
Process{
196-
if($final_uri){
199+
If($final_uri){
197200
#Create Request Header
198201
$requestHeader = @{
199202
Authorization = $AuthHeader
200203
}
201-
if($PSBoundParameters.ContainsKey('AddConsistencyLevelHeader') -and $PSBoundParameters['AddConsistencyLevelHeader'].IsPresent){
204+
If($PSBoundParameters.ContainsKey('AddConsistencyLevelHeader') -and $PSBoundParameters['AddConsistencyLevelHeader'].IsPresent){
202205
[void]$requestHeader.Add('ConsistencyLevel','eventual')
203206
}
204207
#set count
@@ -218,11 +221,15 @@ Function Get-MonkeyMSGraphObject{
218221
Debug = $Debug;
219222
InformationAction = $InformationAction;
220223
}
224+
If($PSBoundParameters.ContainsKey('RawResponse') -and $PSBoundParameters['RawResponse'].IsPresent){
225+
[void]$param.Add('RawResponse',$true)
226+
}
227+
#Execute query
221228
$Objects = Invoke-MonkeyWebRequest @param
222229
}
223230
'POST'
224231
{
225-
if($Data){
232+
If($Data){
226233
$param = @{
227234
Url = $final_uri;
228235
Headers = $requestHeader;
@@ -235,7 +242,7 @@ Function Get-MonkeyMSGraphObject{
235242
InformationAction = $InformationAction;
236243
}
237244
}
238-
else{
245+
Else{
239246
$param = @{
240247
Url = $final_uri;
241248
Headers = $requestHeader;
@@ -251,32 +258,33 @@ Function Get-MonkeyMSGraphObject{
251258
$Objects = Invoke-MonkeyWebRequest @param
252259
}
253260
}
254-
if($ObjectType){
255-
Write-Verbose ("Getting {0} from microsoft graph" -f $ObjectType)
256-
}
257-
else{
258-
Write-Verbose $final_uri
261+
#Writes URL to verbose stream
262+
Write-Verbose $final_uri
263+
#Check objects
264+
If($null -ne $Objects -and $Objects -is [System.Net.Http.HttpResponseMessage]){
265+
$Objects
266+
return
259267
}
260-
if($null -ne $Objects -and $null -ne $Objects.PSObject.Properties.Item('value') -and @($Objects.value).Count -gt 0){
268+
ElseIf($null -ne $Objects -and $null -ne $Objects.PSObject.Properties.Item('value') -and @($Objects.value).Count -gt 0){
261269
#Count objects
262270
$countObjects += @($Objects.value).Count
263271
#return Value
264272
$Objects.value
265273
}
266-
elseif($null -ne $Objects -and $null -ne $Objects.PSObject.Properties.Item('value') -and $Objects.value.Count -eq 0){
274+
ElseIf($null -ne $Objects -and $null -ne $Objects.PSObject.Properties.Item('value') -and $Objects.value.Count -eq 0){
267275
#empty response
268276
$Objects.value
269277
}
270-
else{
278+
Else{
271279
#Count objects
272280
$countObjects += @($Objects).Count
273281
$Objects
274282
}
275-
if($Top -and $Top -ge $countObjects){
283+
If($Top -and $Top -ge $countObjects){
276284
return
277285
}
278286
#Search for paging objects
279-
if ($Objects.PsObject.Properties.Item('@odata.nextLink')){
287+
If ($Objects.PsObject.Properties.Item('@odata.nextLink')){
280288
$nextLink = $Objects.'@odata.nextLink'
281289
while ($null -ne $nextLink){
282290
#Make RestAPI call

core/api/entraid/msgraph/helpers/directoryrole/Get-MonkeyMSGraphEntraRoleAssignment.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Function Get-MonkeyMSGraphEntraRoleAssignment {
195195
Write-Output $allEntraIDRoleAssignment -NoEnumerate
196196
}
197197
Catch{
198+
write-host $_
198199
Write-Error $_
199200
return , $allEntraIDRoleAssignment
200201
}

core/api/entraid/msgraph/helpers/general/Get-MonkeyMSGraphProfilePhoto.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ Function Get-MonkeyMSGraphProfilePhoto {
7171
ObjectType = 'users';
7272
ObjectId = $ObjectId;
7373
Environment = $Environment;
74-
ContentType = 'application/json';
74+
ContentType = 'image/jpg';
7575
Method = "GET";
7676
APIVersion = $APIVersion;
77+
RawResponse = $True;
7778
InformationAction = $O365Object.InformationAction;
7879
Verbose = $O365Object.verbose;
7980
Debug = $O365Object.debug;
@@ -101,6 +102,7 @@ Function Get-MonkeyMSGraphProfilePhoto {
101102
Url = $logoUrl;
102103
ContentType = 'image/jpg';
103104
Method = "GET";
105+
RawResponse = $True;
104106
InformationAction = $O365Object.InformationAction;
105107
Verbose = $O365Object.verbose;
106108
Debug = $O365Object.debug;
@@ -128,6 +130,7 @@ Function Get-MonkeyMSGraphProfilePhoto {
128130
Url = $logoUrl;
129131
ContentType = 'image/jpg';
130132
Method = "GET";
133+
RawResponse = $True;
131134
InformationAction = $O365Object.InformationAction;
132135
Verbose = $O365Object.verbose;
133136
Debug = $O365Object.debug;
@@ -139,8 +142,15 @@ Function Get-MonkeyMSGraphProfilePhoto {
139142
}
140143
}
141144
End{
142-
if($null -ne $profilePhoto){
143-
return [Convert]::ToBase64String($profilePhoto)
145+
If($null -ne $profilePhoto -and $profilePhoto -is [System.Net.Http.HttpResponseMessage]){
146+
Try{
147+
$stream = $profilePhoto.Content.ReadAsStreamAsync()
148+
$memoryStream = $stream.GetAwaiter().GetResult()
149+
return [Convert]::ToBase64String($memoryStream.ToArray())
150+
}
151+
Catch{
152+
Write-Error $_
153+
}
144154
}
145155
}
146156
}

core/api/entraid/msgraph/helpers/users/Get-MonkeyMSGraphUser.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Function Get-MonkeyMSGraphUser {
6969
$graphAuth = $O365Object.auth_tokens.MSGraph
7070
}
7171
Process{
72+
Write-Host ($PSBoundParameters | Out-String) -ForegroundColor Yellow
73+
Write-Host ($PSCmdlet.ParameterSetName) -ForegroundColor Magenta
7274
if($PSCmdlet.ParameterSetName -eq 'UserId'){
7375
$params = @{
7476
Authentication = $graphAuth;

0 commit comments

Comments
 (0)