Search code examples
powershellazure-powershell

How to filter the output in powershell for class


How to filter this output in PowerShell with Enddate in below output

$expiryApps = $appRegistrations | Where-Object { $_.PasswordCredentials.EndDate -lt $thresholdDate } | Select-Object DisplayName, objectID, PasswordCredentials
 foreach($i in $expiryApps){
     $ExpieredKey = Get-AzureADApplicationPasswordCredential -objectID $i.objectID 
     foreach($expiry in $ExpieredKey){
         write-host $expiry
     }
 }

i get this output and i need to filter by Enddate here

class PasswordCredential {
  CustomKeyIdentifier: 
  EndDate: 9/9/2023 6:30:00 PM
  KeyId: 
  StartDate: 9/8/2023 6:30:00 PM
  Value: 
}

class PasswordCredential {
  CustomKeyIdentifier: 
  EndDate: 3/6/2024 9:02:26 AM
  KeyId: 
  StartDate: 9/8/2023 9:02:26 AM
  Value: 
}

Solution

  • How to filter this output in PowerShell with Enddate in below output

    I have created some applications with secret end dates set to '2023-12-07' for the purpose of fetching the secrets.

    Application

    enter image description here

    Here is the updated script to filter the application secrets with end date.

        $thresholdDate = Get-Date "2023-12-07"
        $appRegistrations = Get-AzureADApplication
        
        foreach ($app in $appRegistrations) {
            $PasswordCredentials = Get-AzureADApplicationPasswordCredential -ObjectId $app.ObjectId
            foreach ($credential in $PasswordCredentials) {
                $formattedEndDate = $credential.EndDate.ToString("MM/dd/yyyy")
                if ($formattedEndDate -eq $thresholdDate.ToString("MM/dd/yyyy")) {
                    Write-Host "Application: $($app.DisplayName)"
                    Write-Host "  PasswordCredential:"
                    Write-Host "    EndDate: $($credential.EndDate)"
                }
            }
        }
    

    The script will check all applications, and if the end date matches, it will display the application.

    Output:

    enter image description here