Advertisement
jmusick

Update-PartnerAppConsent

Feb 24th, 2023 (edited)
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 7.2
  2. #Requires -Modules MSAL.PS, Microsoft.PowerShell.ConsoleGuiTools
  3.  
  4. param(
  5.     [Parameter(
  6.         HelpMessage = @"
  7. This is the list of scopes you'll need for your script or automation. Example:
  8.  
  9. Directory.Read.All,AccessReview.Read.All
  10. "@)]
  11.     [string] $Scope
  12. )
  13.  
  14. $ErrorActionPreference = "stop"
  15.  
  16. $PartnerAppId = "{CLIENT_ID}"
  17.  
  18. $MsalParams = @{
  19.     ClientId    = $PartnerAppId;
  20.     Interactive = $true;
  21.     Scope       = "https://api.partnercenter.microsoft.com/user_impersonation";
  22. }
  23.  
  24. $PartnerAccessToken = Get-MsalToken @MsalParams
  25.  
  26. $PartnerCustomersParams = @{
  27.     Uri     = "https://api.partnercenter.microsoft.com/v1/customers";
  28.     Headers = @{
  29.         "Authorization" = "Bearer $($PartnerAccessToken.AccessToken)"
  30.     };
  31.     Method  = "Get";
  32. }
  33.  
  34. $SelectedCustomers = (Invoke-RestMethod @PartnerCustomersParams).items | `
  35.     Select-Object -ExpandProperty companyProfile | `
  36.     Select-Object -Property companyName, tenantId | `
  37.     Out-ConsoleGridView -OutputMode Multiple -Title "Select your tenant"
  38.  
  39. <#
  40.     This step is what will add the application to your customer's organization.
  41.     The EnterpriseAppplicationId below is the app you're allowing to use your partner credentials.
  42.     In this example, it's just the Microsoft Graph enterprise application.
  43.     Notice how the scope isn't in our Partner App. You don't need to specify it in the partner app.
  44.     If you change the scope, it should add the ones you didn't include, but I don't see a way to remove any.
  45.     Your user account's permissions through GDAP are what determines what you really have permissions to so if your user
  46.     didn't have Directory.Read.All permissions, this wouldn't work.
  47.  
  48.     Since there's no way to check or force this to update, it will throw an error-like message if you've already done this.
  49. #>
  50.  
  51. # Forcing a scope selection from the list if one wasn't specified.
  52. if (!$Scope) {
  53.     $SelectedScopes = Find-MgGraphPermission | `
  54.         Where-Object PermissionType -eq "Delegated" | `
  55.         Select-Object Name, Description | `
  56.         Out-ConsoleGridView -OutputMode Multiple -Title "Select your scopes"
  57.  
  58.     $Scope = $SelectedScopes.Name -Join ","
  59.  
  60.     if (!$Scope) {
  61.         throw "Selecting a scope(s) is required to continue."
  62.     }
  63. }
  64.  
  65. $AppConsentHeaders = @{
  66.     "Accept"        = "application/json";
  67.     "Content-Type"  = "application/json";
  68.     "Authorization" = "Bearer $($PartnerAccessToken.AccessToken)"
  69. }
  70.  
  71. $AppConsentBody = ConvertTo-JSON -InputObject @{
  72.     "applicationid"     = $PartnerAppId;
  73.     "applicationGrants" = @(
  74.         @{
  75.             "enterpriseApplicationId" = "00000003-0000-0000-c000-000000000000"; # Microsoft Graph.
  76.             "scope"                   = $Scope
  77.         },
  78.         @{
  79.             "enterpriseApplicationId" = "00000002-0000-0ff1-ce00-000000000000"; # Exchange Online.
  80.             "scope"                   = "Exchange.Manage"
  81.         }
  82.     )
  83. }
  84.  
  85. $Index = 0
  86.  
  87. ForEach ($Customer in $SelectedCustomers) {
  88.     $Index++
  89.     $PercentComplete = [int][math]::Ceiling(100 / $SelectedCustomers.Count * $Index )
  90.  
  91.     Write-Progress `
  92.         -Activity $Customer.companyName `
  93.         -Status "$Index out of $($SelectedCustomers.Count)" `
  94.         -PercentComplete $PercentComplete
  95.  
  96.     $CustomerTenantId = $Customer.tenantId
  97.  
  98.     # Removing existing consent if it exists. There isn't a way to query or updates, unfortunately.
  99.     $PartnerCustomerRemoveAppConsentParams = @{
  100.         Uri     = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerTenantId/applicationconsents/$PartnerAppId";
  101.         Headers = $AppConsentHeaders;
  102.         Method  = "DELETE";
  103.     }
  104.  
  105.     Invoke-RestMethod @PartnerCustomerRemoveAppConsentParams
  106.  
  107.     # Adding new consents.
  108.     $PartnerCustomerPostAppConsentParams = @{
  109.         Uri     = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerTenantId/applicationconsents";
  110.         Headers = $AppConsentHeaders;
  111.         Body    = $AppConsentBody
  112.         Method  = "POST";
  113.     }
  114.  
  115.     Invoke-RestMethod @PartnerCustomerPostAppConsentParams | Out-Null
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement