Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-EntraDeletedUser #1207

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
124 changes: 124 additions & 0 deletions module/Entra/AdditionalFunctions/Get-EntraDeletedUser.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Get-EntraDeletedUser {
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
param (
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Filter to apply to the query.")]
[System.String] $Filter,

[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all deleted users.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search string to use for vague queries.")]
[System.String] $SearchString,

[Alias('Id')]
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "ID of the user to retrieve.")]
[System.String] $UserId,

[Alias('Limit')]
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Maximum number of results to return.")]
[System.Nullable`1[System.Int32]] $Top,

[Alias('Select')]
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Properties to include in the results.")]
[System.String[]] $Property
)

PROCESS {
$params = @{}
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
$keysChanged = @{SearchString = "Filter" }
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["ProgressAction"]) {
$params["ProgressAction"] = $PSBoundParameters["ProgressAction"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Filter"]) {
$TmpValue = $PSBoundParameters["Filter"]
foreach ($i in $keysChanged.GetEnumerator()) {
$TmpValue = $TmpValue.Replace($i.Key, $i.Value)
}
$Value = $TmpValue
$params["Filter"] = $Value
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "mailNickName eq '$TmpValue' or (mail eq '$TmpValue' or (displayName eq '$TmpValue' or startswith(displayName,'$TmpValue')))"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["UserId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["UserId"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($PSBoundParameters.ContainsKey("Top")) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

Write-Debug("============================ TRANSFORMATIONS ============================")
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug("=========================================================================`n")

# Make the API call
$response = Get-MgDirectoryDeletedItemAsUser @params -Headers $customHeaders

# Process the response
foreach ($item in $response) {
if ($null -ne $item) {
$item | Add-Member -MemberType AliasProperty -Name ObjectId -Value $item.Id
}
}

# Check if the user requested 'Format-List' or 'FL' to output all properties
$outputPreference = $PSCmdlet.MyInvocation.Line.ToLower()
if ($outputPreference.Contains("format-list") -or $outputPreference.Contains("fl")) {
# Return all properties
$response
}
else {
# Return default properties
$response | Select-Object -Property Id, DisplayName, UserPrincipalName, UserType, DeletedDateTime,
@{Name = "PermanentDeletionDate"; Expression = { $_.DeletedDateTime.AddDays(30) } } | Format-Table -AutoSize
}
}
}
124 changes: 124 additions & 0 deletions module/EntraBeta/AdditionalFunctions/Get-EntraBetaDeletedUser.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Get-EntraBetaDeletedUser {
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
param (
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Filter to apply to the query.")]
[System.String] $Filter,

[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all deleted users.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search string to use for vague queries.")]
[System.String] $SearchString,

[Alias('Id')]
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "ID of the user to retrieve.")]
[System.String] $UserId,

[Alias('Limit')]
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Maximum number of results to return.")]
[System.Nullable`1[System.Int32]] $Top,

[Alias('Select')]
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Properties to include in the results.")]
[System.String[]] $Property
)

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
$keysChanged = @{SearchString = "Filter" }
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["ProgressAction"]) {
$params["ProgressAction"] = $PSBoundParameters["ProgressAction"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Filter"]) {
$TmpValue = $PSBoundParameters["Filter"]
foreach ($i in $keysChanged.GetEnumerator()) {
$TmpValue = $TmpValue.Replace($i.Key, $i.Value)
}
$Value = $TmpValue
$params["Filter"] = $Value
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "mailNickName eq '$TmpValue' or (mail eq '$TmpValue' or (displayName eq '$TmpValue' or startswith(displayName,'$TmpValue')))"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["UserId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["UserId"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($PSBoundParameters.ContainsKey("Top")) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

Write-Debug("============================ TRANSFORMATIONS ============================")
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug("=========================================================================`n")

# Make the API call
$response = Get-MgBetaDirectoryDeletedItemAsUser @params -Headers $customHeaders

# Process the response
foreach ($item in $response) {
if ($null -ne $item) {
$item | Add-Member -MemberType AliasProperty -Name ObjectId -Value $item.Id
}
}

# Check if the user requested 'Format-List' or 'FL' to output all properties
$outputPreference = $PSCmdlet.MyInvocation.Line.ToLower()
if ($outputPreference.Contains("format-list") -or $outputPreference.Contains("fl")) {
# Return all properties
$response
}
else {
# Return default properties
$response | Select-Object -Property Id, DisplayName, UserPrincipalName, UserType, DeletedDateTime,
@{Name = "PermanentDeletionDate"; Expression = { $_.DeletedDateTime.AddDays(30) } } | Format-Table -AutoSize
}
}
}
Loading