Skip to content

Instantly share code, notes, and snippets.

@ivangeorgiev
Created September 24, 2020 17:47
Show Gist options
  • Save ivangeorgiev/7f336267e14565c09af5c724248535e3 to your computer and use it in GitHub Desktop.
Save ivangeorgiev/7f336267e14565c09af5c724248535e3 to your computer and use it in GitHub Desktop.
Azure Active Directory (AD) With PowerShell
<#
.SYNOPSIS
Add Application Registration Owner
#>
function Add-AzAdApplicationUserOwnerXYZ($ApplicationName, $UserName) {
$ApplicationId = (Get-AzureADApplication -SearchString $ApplicationName).ObjectId
if (!$ApplicationId) { Throw "Unable to find application $ApplicationName" }
$UserId = (Get-AzureADUser -ObjectId $UserName).ObjectId
if (!$UserId) { Throw "Unable to find user $UserId" }
Add-AzureADApplicationOwner -ObjectId $ApplicationId -RefObjectId $UserId
}
<#
.SYNOPSIS
Add Service Principal Owner
#>
function Add-AzAdServicePrincipalUserOwnerXYZ($ApplicationName, $UserName) {
$ApplicationId = (Get-AzureADApplication -SearchString $ApplicationName).ObjectId
if (!$ApplicationId) { Throw "Unable to find application $ApplicationName" }
$UserId = (Get-AzureADUser -ObjectId $UserName).ObjectId
if (!$UserId) { Throw "Unable to find user $UserId" }
Add-AzureADServicePrincipalOwner -ObjectId $ApplicationId -RefObjectId $UserId
}
<#
.SYNOPSIS
Remove Application Registration Owner
#>
function Remove-AzAdApplicationUserOwnerXYZ($ApplicationName, $UserName) {
$ApplicationId = (Get-AzureADApplication -SearchString $ApplicationName).ObjectId
if (!$ApplicationId) { Throw "Unable to find application $ApplicationName" }
$UserId = (Get-AzureADUser -ObjectId $UserName).ObjectId
if (!$UserId) { Throw "Unable to find user $UserId" }
Remove-AzureADApplicationOwner -ObjectId $ApplicationId -OwnerId $UserId
}
<#
.SYNOPSIS
Remove Service Principal Owner
#>
function Remove-AzAdServicePrincipalUserOwnerXYZ($ApplicationName, $UserName) {
$ApplicationId = (Get-AzureADApplication -SearchString $ApplicationName).ObjectId
if (!$ApplicationId) { Throw "Unable to find application $ApplicationName" }
$UserId = (Get-AzureADUser -ObjectId $UserName).ObjectId
if (!$UserId) { Throw "Unable to find user $UserId" }
Remove-AzureADServicePrincipalOwner -ObjectId $ApplicationId -OwnerId $UserId
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment