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-EntraDeletedDevice #1232

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions module/Entra/AdditionalFunctions/Get-EntraDeletedDevice.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Get-EntraDeletedDevice {
[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 devices.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search for devices.")]
[System.String] $SearchString,

[Alias('Id')]
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Device Object ID to retrieve.")]
[System.String] $DeviceObjectId,

[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["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["DeviceObjectId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["DeviceObjectId"]
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "displayName eq '$TmpValue' or startsWith(displayName,'$TmpValue')"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Top"]) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
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 ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

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

try {
# Make the API call with -PageSize 999 if -All is used
if ($PSBoundParameters.ContainsKey("All") -and $All) {
$response = Get-MgDirectoryDeletedItemAsDevice @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgDirectoryDeletedItemAsDevice @params -Headers $customHeaders
}

return $response
}
catch {
# Handle any errors that occur during the API call
Write-Error "An error occurred while retrieving the deleted devices: $_"
}
}
}
118 changes: 118 additions & 0 deletions module/EntraBeta/AdditionalFunctions/Get-EntraBetaDeletedDevice.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Get-EntraBetaDeletedDevice {
[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 devices.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search for devices.")]
[System.String] $SearchString,

[Alias('Id')]
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Device Object ID to retrieve.")]
[System.String] $DeviceObjectId,

[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["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["DeviceObjectId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["DeviceObjectId"]
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "displayName eq '$TmpValue' or startsWith(displayName,'$TmpValue')"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Top"]) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
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 ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

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

try {
# Make the API call with -PageSize 999 if -All is used
if ($PSBoundParameters.ContainsKey("All") -and $All) {
$response = Get-MgBetaDirectoryDeletedItemAsDevice @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgBetaDirectoryDeletedItemAsDevice @params -Headers $customHeaders
}

return $response
}
catch {
# Handle any errors that occur during the API call
Write-Error "An error occurred while retrieving the deleted devices: $_"
}
}
}
Loading