|
5 | 5 | function Get-EntraDeletedApplication {
|
6 | 6 | [CmdletBinding(DefaultParameterSetName = 'GetQuery')]
|
7 | 7 | param (
|
8 |
| - |
9 |
| - [Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
10 |
| - [Alias("Limit")] |
11 |
| - [System.Nullable`1[System.Int32]] $Top, |
12 |
| - |
13 |
| - [Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
| 8 | + [Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Filter to apply to the query.")] |
14 | 9 | [System.String] $Filter,
|
15 |
| - |
16 |
| - [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
| 10 | + |
| 11 | + [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all deleted applications.")] |
17 | 12 | [switch] $All,
|
18 |
| - |
19 |
| - [Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
| 13 | + |
| 14 | + [Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search string to use for vague queries.")] |
20 | 15 | [System.String] $SearchString,
|
21 |
| - [Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true)] |
22 |
| - [Alias("Select")] |
| 16 | + |
| 17 | + [Alias('Id')] |
| 18 | + [Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Unique Application object ID to retrieve.")] |
| 19 | + [System.String] $ApplicationId, |
| 20 | + |
| 21 | + [Alias('Limit')] |
| 22 | + [Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Maximum number of results to return.")] |
| 23 | + [System.Nullable`1[System.Int32]] $Top, |
| 24 | + |
| 25 | + [Alias('Select')] |
| 26 | + [Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Properties to include in the results.")] |
23 | 27 | [System.String[]] $Property
|
24 | 28 | )
|
25 | 29 |
|
26 |
| - PROCESS { |
| 30 | + PROCESS { |
27 | 31 | $params = @{}
|
28 | 32 | $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
|
29 |
| - $keysChanged = @{SearchString = "Filter"; ObjectId = "Id" } |
| 33 | + $keysChanged = @{ SearchString = "Filter" } |
30 | 34 |
|
31 |
| - if ($null -ne $PSBoundParameters["SearchString"]) { |
32 |
| - $TmpValue = $PSBoundParameters["SearchString"] |
33 |
| - $Value = "displayName eq '$TmpValue' or startswith(displayName,'$TmpValue')" |
34 |
| - $params["Filter"] = $Value |
| 35 | + if ($null -ne $PSBoundParameters["ErrorAction"]) { |
| 36 | + $params["ErrorAction"] = $PSBoundParameters["ErrorAction"] |
35 | 37 | }
|
36 |
| - |
37 |
| - if ($null -ne $PSBoundParameters["Filter"]) { |
38 |
| - $TmpValue = $PSBoundParameters["Filter"] |
39 |
| - foreach ($i in $keysChanged.GetEnumerator()) { |
40 |
| - $TmpValue = $TmpValue.Replace($i.Key, $i.Value) |
41 |
| - } |
42 |
| - $Value = $TmpValue |
43 |
| - $params["Filter"] = $Value |
| 38 | + if ($null -ne $PSBoundParameters["ApplicationId"]) { |
| 39 | + $params["DirectoryObjectId"] = $PSBoundParameters["ApplicationId"] |
44 | 40 | }
|
45 |
| - |
46 | 41 | if ($PSBoundParameters.ContainsKey("Verbose")) {
|
47 | 42 | $params["Verbose"] = $PSBoundParameters["Verbose"]
|
48 | 43 | }
|
49 |
| - |
50 |
| - if ($null -ne $PSBoundParameters["All"]) { |
51 |
| - if ($PSBoundParameters["All"]) { |
52 |
| - $params["All"] = $PSBoundParameters["All"] |
53 |
| - } |
54 |
| - } |
55 |
| - |
56 |
| - if ($PSBoundParameters.ContainsKey("Debug")) { |
57 |
| - $params["Debug"] = $PSBoundParameters["Debug"] |
| 44 | + if ($null -ne $PSBoundParameters["OutVariable"]) { |
| 45 | + $params["OutVariable"] = $PSBoundParameters["OutVariable"] |
58 | 46 | }
|
59 |
| - |
60 |
| - if ($PSBoundParameters.ContainsKey("Top")) { |
61 |
| - $params["Top"] = $PSBoundParameters["Top"] |
| 47 | + if ($null -ne $PSBoundParameters["InformationAction"]) { |
| 48 | + $params["InformationAction"] = $PSBoundParameters["InformationAction"] |
62 | 49 | }
|
63 | 50 | if ($null -ne $PSBoundParameters["WarningVariable"]) {
|
64 | 51 | $params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
|
65 | 52 | }
|
66 |
| - if ($null -ne $PSBoundParameters["InformationVariable"]) { |
67 |
| - $params["InformationVariable"] = $PSBoundParameters["InformationVariable"] |
68 |
| - } |
69 |
| - if ($null -ne $PSBoundParameters["InformationAction"]) { |
70 |
| - $params["InformationAction"] = $PSBoundParameters["InformationAction"] |
| 53 | + if ($PSBoundParameters.ContainsKey("Debug")) { |
| 54 | + $params["Debug"] = $PSBoundParameters["Debug"] |
71 | 55 | }
|
72 |
| - if ($null -ne $PSBoundParameters["OutVariable"]) { |
73 |
| - $params["OutVariable"] = $PSBoundParameters["OutVariable"] |
| 56 | + if ($null -ne $PSBoundParameters["PipelineVariable"]) { |
| 57 | + $params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"] |
74 | 58 | }
|
75 |
| - if ($null -ne $PSBoundParameters["OutBuffer"]) { |
76 |
| - $params["OutBuffer"] = $PSBoundParameters["OutBuffer"] |
| 59 | + if ($null -ne $PSBoundParameters["SearchString"]) { |
| 60 | + $TmpValue = $PSBoundParameters["SearchString"] |
| 61 | + $Value = "displayName eq '$TmpValue' or startsWith(displayName,'$TmpValue')" |
| 62 | + $params["Filter"] = $Value |
77 | 63 | }
|
78 | 64 | if ($null -ne $PSBoundParameters["ErrorVariable"]) {
|
79 | 65 | $params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
|
80 | 66 | }
|
81 |
| - if ($null -ne $PSBoundParameters["PipelineVariable"]) { |
82 |
| - $params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"] |
| 67 | + if ($null -ne $PSBoundParameters["Top"]) { |
| 68 | + $params["Top"] = $PSBoundParameters["Top"] |
83 | 69 | }
|
84 |
| - if ($null -ne $PSBoundParameters["ErrorAction"]) { |
85 |
| - $params["ErrorAction"] = $PSBoundParameters["ErrorAction"] |
| 70 | + if ($null -ne $PSBoundParameters["OutBuffer"]) { |
| 71 | + $params["OutBuffer"] = $PSBoundParameters["OutBuffer"] |
| 72 | + } |
| 73 | + if ($null -ne $PSBoundParameters["All"]) { |
| 74 | + if ($PSBoundParameters["All"]) { |
| 75 | + $params["All"] = $PSBoundParameters["All"] |
| 76 | + } |
86 | 77 | }
|
87 | 78 | if ($null -ne $PSBoundParameters["WarningAction"]) {
|
88 | 79 | $params["WarningAction"] = $PSBoundParameters["WarningAction"]
|
89 | 80 | }
|
| 81 | + if ($null -ne $PSBoundParameters["Filter"]) { |
| 82 | + $TmpValue = $PSBoundParameters["Filter"] |
| 83 | + foreach ($i in $keysChanged.GetEnumerator()) { |
| 84 | + $TmpValue = $TmpValue.Replace($i.Key, $i.Value) |
| 85 | + } |
| 86 | + $Value = $TmpValue |
| 87 | + $params["Filter"] = $Value |
| 88 | + } |
| 89 | + if ($null -ne $PSBoundParameters["InformationVariable"]) { |
| 90 | + $params["InformationVariable"] = $PSBoundParameters["InformationVariable"] |
| 91 | + } |
90 | 92 | if ($null -ne $PSBoundParameters["Property"]) {
|
91 | 93 | $params["Property"] = $PSBoundParameters["Property"]
|
92 | 94 | }
|
93 |
| - |
94 |
| - Write-Debug("============================ TRANSFORMATIONS ============================") |
| 95 | + |
| 96 | + # Debug logging for transformations |
| 97 | + Write-Debug "============================ TRANSFORMATIONS ============================" |
95 | 98 | $params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
|
96 |
| - Write-Debug("=========================================================================`n") |
97 |
| - |
98 |
| - $response = Get-MgDirectoryDeletedItemAsApplication @params -Headers $customHeaders |
99 |
| - |
100 |
| - $response | ForEach-Object { |
101 |
| - if ($null -ne $_) { |
102 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value Id |
103 |
| - |
104 |
| - $propsToConvert = @( |
105 |
| - 'AddIns', 'AppRoles', 'GroupMembershipClaims', 'IdentifierUris', 'Info', |
106 |
| - 'IsDeviceOnlyAuthSupported', 'KeyCredentials', 'OptionalClaims', |
107 |
| - 'ParentalControlSettings', 'PasswordCredentials', 'Api', 'PublicClient', |
108 |
| - 'PublisherDomain', 'Web', 'RequiredResourceAccess') |
109 |
| - |
110 |
| - foreach ($prop in $propsToConvert) { |
111 |
| - $value = $_.$prop | ConvertTo-Json -Depth 5 | ConvertFrom-Json |
112 |
| - $_ | Add-Member -MemberType NoteProperty -Name $prop -Value ($value) -Force |
| 99 | + Write-Debug "=========================================================================`n" |
| 100 | + |
| 101 | + try { |
| 102 | + # Make the API call |
| 103 | + if ($PSBoundParameters.ContainsKey("All") -and $All) { |
| 104 | + $response = Get-MgDirectoryDeletedItemAsApplication @params -PageSize 999 -Headers $customHeaders |
| 105 | + } |
| 106 | + else { |
| 107 | + $response = Get-MgDirectoryDeletedItemAsApplication @params -Headers $customHeaders |
| 108 | + } |
| 109 | + |
| 110 | + $response | ForEach-Object { |
| 111 | + if ($null -ne $_) { |
| 112 | + if ($null -ne $_.DeletedDateTime) { |
| 113 | + # Add DeletionAgeInDays property |
| 114 | + $deletionAgeInDays = (Get-Date) - ($_.DeletedDateTime) |
| 115 | + Add-Member -InputObject $_ -MemberType NoteProperty -Name DeletionAgeInDays -Value ($deletionAgeInDays.Days) -Force |
| 116 | + } |
| 117 | + |
113 | 118 | }
|
114 |
| - |
115 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name AppLogoUrl -Value Logo |
116 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name InformationalUrls -Value Info |
117 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name HomePage -Value Web.HomePageUrl |
118 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name LogoutUrl -Value Web.LogoutUrl |
119 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name ReplyUrls -Value Web.RedirectUris |
120 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name KnownClientApplications -Value Api.KnownClientApplications |
121 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name PreAuthorizedApplications -Value Api.PreAuthorizedApplications |
122 |
| - Add-Member -InputObject $_ -MemberType AliasProperty -Name Oauth2AllowImplicitFlow -Value Web.Oauth2AllowImplicitFlow |
123 | 119 | }
|
124 |
| - |
| 120 | + |
| 121 | + return $response |
| 122 | + } |
| 123 | + catch { |
| 124 | + # Handle any errors that occur during the API call |
| 125 | + Write-Error "An error occurred while retrieving the deleted applications: $_" |
125 | 126 | }
|
126 |
| - |
127 |
| - $response |
128 |
| - } |
| 127 | + } |
129 | 128 | }
|
130 | 129 |
|
0 commit comments