Created
April 24, 2026 08:22
-
-
Save cschar/83b221f6e83f33477a037c5b53b40fae to your computer and use it in GitHub Desktop.
a reset
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
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$TenantId, | |
| [Parameter(Mandatory=$true)] | |
| [string]$ClientId, | |
| [Parameter(Mandatory=$true)] | |
| [string]$ClientSecret, | |
| [Parameter(Mandatory=$true)] | |
| [string]$TargetApplicationName, | |
| [string]$OwnerUserPrincipalName = "firstname.lastname@gmail.com" | |
| ) | |
| Install-Module Microsoft.Graph -Scope CurrentUser -Force | |
| $secureSecret = ConvertTo-SecureString $ClientSecret -AsPlainText -Force | |
| $credential = New-Object System.Management.Automation.PSCredential($ClientId, $secureSecret) | |
| Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $credential -NoWelcome | |
| $app = Get-MgApplication -Filter "displayName eq '$TargetApplicationName'" | |
| if (-not $app) { | |
| throw "No application found with displayName '$TargetApplicationName'" | |
| } | |
| if ($app.Count -gt 1) { | |
| throw "Multiple applications found with displayName '$TargetApplicationName'. Use AppId/ObjectId instead." | |
| } | |
| $user = Get-MgUser -UserId $OwnerUserPrincipalName | |
| $ownerRef = @{ | |
| "@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$($user.Id)" | |
| } | |
| New-MgApplicationOwnerByRef ` | |
| -ApplicationId $app.Id ` | |
| -BodyParameter $ownerRef | |
| Write-Host "Added $OwnerUserPrincipalName as owner of application '$TargetApplicationName'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment