ServicePrincipal StartDate and EndDate not displaying Using Graph API In Power Shell




I am trying to get list of SPNs that are going to expire soon. Using Graph API  I am executing below powershell script.


I am getting output appid and name always but StartDate and EndDate are not displaying for few of SPN.


Can you please help how to get it ?


Below is Power shell script I am using:

$TenantId = "*************"
$ClientId = "*************"
$ClientSecret = "*************"

$Body = @{
    'tenant' = $TenantId
    'client_id' = $ClientId
    'scope' = ''
    'client_secret' = $ClientSecret
    'grant_type' = 'client_credentials'

$Params = @{
    'Uri' = "$TenantId/oauth2/v2.0/token"
    'Method' = 'Post'
    'Body' = $Body
    'ContentType' = 'application/x-www-form-urlencoded'

$AuthResponse = Invoke-RestMethod @Params

$Headers = @{'Authorization' = "Bearer $($AuthResponse.access_token)"}

$method = "GET"

$uri2 = "{Id}"

$query2 = Invoke-WebRequest -Method $method -Uri $uri2  -ContentType "application/json" -Headers $Headers -ErrorAction Stop

$query2.content | ConvertFrom-Json | select appId,displayName,@{l="SecretExpiryDate";e={$pwdcreds2.passwordCredentials.endDateTime}}



I have another Powershell script it is giving startdate and enddate for same SPN but the problem is my Org is not allowed to fetch APP details from Azure AD due to security guidelines.



$ServicePrincipalIds = Get-AzADServicePrincipal | Where {$_.DisplayName -like '*'}

foreach($ServicePrincipalId in $ServicePrincipalIds)
$ServicePrincipalInfo = Get-AzADSpCredential -ObjectId $ServicePrincipalId.Id


2 Replies
best response confirmed by Brahmaiah (Contributor)

Well what exactly is:


as that's what you are using to parse for passwordCredentials. For the record, I can see them just fine in Graph explorer or calling the Graph API directly via PowerShell.


$result = ($test.Content | ConvertFrom-Json).passwordCredentials.startDateTime

@Vasil Michev thank you so much for reply.


I execute my powershell script by adding your command but it is not resulting anything. I guess if servicePrincipal has too many start and end date it is failing to convertfrom json.


Another powershell script resulting all list of start and end dates. Below is screen shot.



Can you please check sample data from your end :) and suggest me how to get result from API.


Thanks again for your help and support so far.