functions/administrativeunit/Remove-PSEntraIDAdministrativeUnit.ps1
|
function Remove-PSEntraIDAdministrativeUnit { <# .SYNOPSIS Delete administrative unit .DESCRIPTION Delete administrative unit from Microsoft Entra ID (Azure AD). When deleted, administrative unit resources are moved to a temporary container and can be restored within 30 days. After that time, they are permanently deleted. .PARAMETER InputObject PSMicrosoftEntraID.DirectoryManagement.AdministrativeUnit object in tenant/directory. .PARAMETER Identity DisplayName or Id of the administrative unit attribute populated in tenant/directory. .PARAMETER EnableException This parameter disables user-friendly warnings and enables the throwing of exceptions. This is less user friendly, but allows catching exceptions in calling scripts. .PARAMETER WhatIf Enables the function to simulate what it will do instead of actually executing. .PARAMETER Force Suppresses the confirmation prompt, for unattended use. An explicitly bound -Confirm wins over it, whatever its value: -Confirm:$true prompts even with -Force present. The two are therefore alternatives rather than a pair - passing both says nothing the second one does not already say. Without either, whether the command prompts is left to its ConfirmImpact and the session ConfirmPreference, which is the PowerShell default behaviour. .PARAMETER Confirm Prompts for confirmation before the command makes a change. -Confirm:$false suppresses that prompt. Bound explicitly it wins over -Force, whatever its value - so -Confirm:$true prompts even alongside -Force, and the two are alternatives rather than a pair. Left unbound, the decision belongs to this command's ConfirmImpact and the session ConfirmPreference, which is the PowerShell default behaviour. .PARAMETER PassThru When specified, the cmdlet will not execute the delete action but will instead return a `PSMicrosoftEntraID.Batch.Request` object for batch processing. .EXAMPLE PS C:\> Remove-PSEntraIDAdministrativeUnit -Identity "Marketing AU" Delete administrative unit "Marketing AU" from Microsoft Entra ID .EXAMPLE PS C:\> Get-PSEntraIDAdministrativeUnit -DisplayName "Old Department" | Remove-PSEntraIDAdministrativeUnit Delete administrative unit by piping from Get cmdlet .NOTES Administrative units provide a way to subdivide your organization and delegate administrative permissions to those subdivisions. When deleted, they can be restored within 30 days. #> [OutputType([PSMicrosoftEntraID.Batch.Request])] [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High', DefaultParameterSetName = 'InputObject')] param ([Parameter(Mandatory = $True, ValueFromPipeline = $true, ParameterSetName = 'InputObject')] [PSTypeName('PSMicrosoftEntraID.DirectoryManagement.AdministrativeUnit')] [object[]] $InputObject, [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Identity')] [Alias("Id", "AdministrativeUnitId")] [ValidateNotNullOrEmpty()] [string[]] $Identity, [Parameter()] [switch] $EnableException, [Parameter()] [switch] $Force, [Parameter()] [switch] $PassThru ) begin { [string] $service = Get-PSFConfigValue -FullName ('{0}.Settings.DefaultService' -f $script:ModuleName) Assert-EntraConnection -Service $service -Cmdlet $PSCmdlet [int] $commandRetryCount = Get-PSFConfigValue -FullName ('{0}.Settings.Command.RetryCount' -f $script:ModuleName) [System.TimeSpan] $commandRetryWait = New-TimeSpan -Seconds (Get-PSFConfigValue -FullName ('{0}.Settings.Command.RetryWaitInSeconds' -f $script:ModuleName)) [hashtable] $header = @{ 'Content-Type' = 'application/json' } [hashtable] $cmdLetConfirm = Resolve-PSEntraIDConfirmPreference -BoundParameters $PSBoundParameters -Force:$Force -Confirm:$Confirm } process { switch ($PSCmdlet.ParameterSetName) { 'InputObject' { foreach ($itemInputObject in $InputObject) { [string] $path = ("directory/administrativeUnits/{0}" -f $itemInputObject.Id) if ($PassThru.IsPresent) { [PSMicrosoftEntraID.Batch.Request]@{ Method = 'DELETE' Url = ('/{0}' -f $path) Headers = $header } } else { Invoke-PSFProtectedCommand -ActionString 'AdministrativeUnit.Remove' -ActionStringValues $itemInputObject.DisplayName -Target (Get-PSFLocalizedString -Module $script:ModuleName -Name Identity.Platform) -ScriptBlock { [void] (Invoke-EntraRequest -Service $service -Path $path -Header $header -Method Delete -ErrorAction Stop) } -EnableException:$EnableException @cmdLetConfirm -PSCmdlet $PSCmdlet -Continue -RetryCount $commandRetryCount -RetryWait $commandRetryWait if (Test-PSFFunctionInterrupt) { return } } } } 'Identity' { foreach ($administrativeUnit in $Identity) { [PSMicrosoftEntraID.DirectoryManagement.AdministrativeUnit] $aADAdministrativeUnit = Get-PSEntraIDAdministrativeUnit -Identity $administrativeUnit if ([object]::Equals($aADAdministrativeUnit, $null)) { if ($EnableException.IsPresent) { Invoke-TerminatingException -Cmdlet $PSCmdlet -Message ((Get-PSFLocalizedString -Module $script:ModuleName -Name AdministrativeUnit.Remove.Failed) -f $administrativeUnit) } } else { [string] $path = ("directory/administrativeUnits/{0}" -f $aADAdministrativeUnit.Id) if ($PassThru.IsPresent) { [PSMicrosoftEntraID.Batch.Request]@{ Method = 'DELETE' Url = ('/{0}' -f $path) Headers = $header } } else { Invoke-PSFProtectedCommand -ActionString 'AdministrativeUnit.Remove' -ActionStringValues $aADAdministrativeUnit.DisplayName -Target (Get-PSFLocalizedString -Module $script:ModuleName -Name Identity.Platform) -ScriptBlock { [void] (Invoke-EntraRequest -Service $service -Path $path -Header $header -Method Delete -ErrorAction Stop) } -EnableException:$EnableException @cmdLetConfirm -PSCmdlet $PSCmdlet -Continue -RetryCount $commandRetryCount -RetryWait $commandRetryWait if (Test-PSFFunctionInterrupt) { return } } } } } } } end {} } |