Last active
May 2, 2019 19:49
-
-
Save poiriersimon/0a1af7f2d0adaf703e9af16740e0d298 to your computer and use it in GitHub Desktop.
Powershell function to connect to Intune Graph API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#You need AzureAD Module (Save-Module AzureAD -Path C:\temp) | |
#You need Function Get-GraphAuthHeaderBasedOnUPN @ https://gist.github.com/poiriersimon/ded7cdca600ba0aab84b75b7f47c1235 | |
Function Connect-Intune{ | |
param | |
( | |
[Parameter(Mandatory = $True)] | |
[string]$Tenant, | |
[Parameter(Mandatory = $True)] | |
[string]$UserPrincipalName, | |
[string]$clientId = "d1ddf0e4-d672-4dae-b554-9d5bdfd93547", | |
[string]$redirectUri = "urn:ietf:wg:oauth:2.0:oob", | |
[string]$resourceAppIdURI = "https://graph.microsoft.com", | |
[string]$AzureADPowershellModuleDir = "C:\Temp\AzureAD" | |
) | |
#Connect to Intune Graph API | |
# Checking if authToken exists before running authentication | |
if($Global:authToken){ | |
# Setting DateTime to Universal time to work in all timezones | |
$DateTime = (Get-Date).ToUniversalTime() | |
# If the authToken exists checking when it expires | |
$TokenExpires = ($Global:authToken.ExpiresOn.datetime - $DateTime).Minutes | |
if($TokenExpires -le 0){ | |
write-host "Authentication Token expired" $TokenExpires "minutes ago" -ForegroundColor Yellow | |
$Global:authToken = Get-GraphAuthHeaderBasedOnUPN -Tenant $Tenant -UserPrincipalName $UserPrincipalName -clientId $clientId -redirectUri $redirectUri -resourceAppIdURI $resourceAppIdURI -AzureADPowershellModuleDir $AzureADPowershellModuleDir -verbose:$Verbose | |
} | |
} | |
# Authentication doesn't exist, calling Get-GraphAuthHeaderBasedOnUPN function | |
else { | |
$Global:authToken = Get-GraphAuthHeaderBasedOnUPN -Tenant $Tenant -UserPrincipalName $UserPrincipalName -clientId $clientId -redirectUri $redirectUri -resourceAppIdURI $resourceAppIdURI -AzureADPowershellModuleDir $AzureADPowershellModuleDir -verbose:$Verbose | |
} | |
$Global:authToken | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment