{"id":2824,"date":"2018-04-10T10:47:59","date_gmt":"2018-04-10T09:47:59","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2824"},"modified":"2018-04-10T10:47:59","modified_gmt":"2018-04-10T09:47:59","slug":"how-to-grant-oauth2-permissions-to-an-azure-ad-application-using-powershell-unattended-silently","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2018\/04\/how-to-grant-oauth2-permissions-to-an-azure-ad-application-using-powershell-unattended-silently\/","title":{"rendered":"How to grant OAuth2 permissions to an Azure AD Application using PowerShell unattended \/ silently"},"content":{"rendered":"<p>You may know this button:<a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2825\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps.png\" alt=\"\" width=\"1121\" height=\"594\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps.png 1121w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps-300x159.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps-1024x543.png 1024w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2018\/04\/grant_oauth2_permissions_to_azure_app_including_multi_tenant_apps-768x407.png 768w\" sizes=\"auto, (max-width: 1121px) 100vw, 1121px\" \/><\/a>There is no native Powershell command to grant OAuth permissions to an Azure AD Application, so I wrote a function for that. Note that this is NOT a supported way to grant permissions to an application because it does not follow the proper admin consent flow that applications normally use.<\/p>\n<p>The great advantage of my method is that it can be used to grant permissions silently, AND to &#8216;hidden&#8217; and\/or multi-tenant applications that companies like Microsoft use for backend stuff like the Intune API. (e.g. the &#8216;Microsoft Intune Powershell&#8217; multi-tenant application).<\/p>\n<p><strong>The function requires AzureAD and AzureRM modules installed!<\/strong><\/p>\n<pre><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n\nFunction Grant-OAuth2PermissionsToApp{\n    Param(\n        &#x5B;Parameter(Mandatory=$true)]$Username, #global administrator username\n        &#x5B;Parameter(Mandatory=$true)]$Password, #global administrator password\n        &#x5B;Parameter(Mandatory=$true)]$azureAppId #application ID of the azure application you wish to admin-consent to\n    )\n\n    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force\n    $mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)\n    $res = login-azurermaccount -Credential $mycreds\n    $context = Get-AzureRmContext\n    $tenantId = $context.Tenant.Id\n    $refreshToken = @($context.TokenCache.ReadItems() | Where-Object {$_.tenantId -eq $tenantId -and $_.ExpiresOn -gt (Get-Date)})&#x5B;0].RefreshToken\n    $body = &amp;amp;amp;amp;quot;grant_type=refresh_token&amp;amp;amp;amp;amp;amp;amp;amp;amp;refresh_token=$($refreshToken)&amp;amp;amp;amp;amp;amp;amp;amp;amp;resource=74658136-14ec-4630-ad9b-26e160ff0fc6&amp;amp;amp;amp;quot;\n    $apiToken = Invoke-RestMethod &amp;amp;amp;amp;quot;https:\/\/login.windows.net\/$tenantId\/oauth2\/token&amp;amp;amp;amp;quot; -Method POST -Body $body -ContentType 'application\/x-www-form-urlencoded'\n    $header = @{\n    'Authorization' = 'Bearer ' + $apiToken.access_token\n    'X-Requested-With'= 'XMLHttpRequest'\n    'x-ms-client-request-id'= &#x5B;guid]::NewGuid()\n    'x-ms-correlation-id' = &#x5B;guid]::NewGuid()}\n    $url = &amp;amp;amp;amp;quot;https:\/\/main.iam.ad.ext.azure.com\/api\/RegisteredApplications\/$azureAppId\/Consent?onBehalfOfAll=true&amp;amp;amp;amp;quot;\n    Invoke-RestMethod \u2013Uri $url \u2013Headers $header \u2013Method POST -ErrorAction Stop\n}\n<\/pre><\/pre>\n<p>GITLAB:\u00a0<a href=\"https:\/\/gitlab.com\/Lieben\/assortedFunctions\/blob\/master\/Grant-OAuth2PermissionsToApp.ps1\" target=\"_blank\" rel=\"noopener noreferrer\">Grant-OAuth2PermissionsToApp.ps1<\/a><\/p>\n\n\n<p class=\"wp-block-paragraph\"><strong>Update 2021: <\/strong><a href=\"https:\/\/www.lieben.nu\/liebensraum\/2020\/04\/calling-graph-and-other-apis-silently-for-an-mfa-enabled-account\/\" data-type=\"post\" data-id=\"3412\">improved \/ mfa compatible token function<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may know this button:There is no native Powershell command to grant OAuth permissions to an Azure AD Application, so I wrote a function for that. Note that this is NOT a supported way to grant permissions to an application because it does not follow the proper admin consent flow that applications normally use. The &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2018\/04\/how-to-grant-oauth2-permissions-to-an-azure-ad-application-using-powershell-unattended-silently\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to grant OAuth2 permissions to an Azure AD Application using PowerShell unattended \/ silently<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,5,39],"tags":[],"class_list":["post-2824","post","type-post","status-publish","format-standard","hentry","category-automation","category-azure","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=2824"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2824\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=2824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=2824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=2824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}