Advertisement
jmusick

PartnerAutomationTemplate

Feb 24th, 2023 (edited)
886
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, ExchangeOnlineManagement
  3.  
  4. $ErrorActionPreference = "stop"
  5.  
  6. $PartnerAppId = "{CLIENT_ID}"
  7.  
  8. $MsalParams = @{
  9.     ClientId    = $PartnerAppId;
  10.     Interactive = $true;
  11.     Scope       = "https://api.partnercenter.microsoft.com/user_impersonation";
  12. }
  13.  
  14. $PartnerAccessToken = Get-MsalToken @MsalParams
  15.  
  16. $PartnerCustomersParams = @{
  17.     Uri     = "https://api.partnercenter.microsoft.com/v1/customers";
  18.     Headers = @{
  19.         "Authorization" = "Bearer $($PartnerAccessToken.AccessToken)"
  20.     };
  21.     Method  = "Get";
  22. }
  23.  
  24. # Use this cool module to select a single customer from the list.
  25.  
  26. $Customer = (Invoke-RestMethod @PartnerCustomersParams).items | `
  27.     Select-Object -ExpandProperty companyProfile | `
  28.     Select-Object -Property companyName, tenantId | `
  29.     Out-ConsoleGridView -OutputMode Single -Title "Select your tenant"
  30.  
  31. # Get tokens for Exchange Online and Microsoft Graph
  32.  
  33. $MsalCustomerGraphParams = @{
  34.     ClientId = $PartnerAppId;
  35.     TenantId = $Customer.tenantId;
  36.     Scope    = "Directory.Read.All"; # Change this depending on what you're doing below
  37.     Silent   = $true; # Uses the saved refresh token
  38. }
  39.  
  40. $MsalCustomerEXOParams = @{
  41.     ClientId = $PartnerAppId;
  42.     TenantId = $Customer.tenantId;
  43.     Scope    = "https://outlook.office365.com/.default";
  44.     Silent   = $true; # Uses the saved refresh token
  45. }
  46.  
  47. $CustomerGraphAccessToken = Get-MsalToken @MsalCustomerGraphParams
  48. $CustomerEXOAccessToken = Get-MsalToken @MsalCustomerEXOParams
  49.  
  50. # Connect to Exchange Online and Microsoft Graph and run commands
  51.  
  52. Connect-ExchangeOnline -DelegatedOrganization $Customer.tenantId -AccessToken $CustomerEXOAccessToken.AccessToken
  53. Connect-MgGraph -AccessToken $CustomerGraphAccessToken.AccessToken
  54.  
  55. Get-MgUser
  56. Get-ExoMailbox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement