Home > Software design >  Powershell 7.2 Issue after migration to MS Graph
Powershell 7.2 Issue after migration to MS Graph

Time:01-27

Since the migration of the module Az.Resources from Azure AD Graph API to MS Graph API i am not able to run these combination of cmdlets,also piping is not working

get-azadserviceprincipal -displayname <some name> | get-azadspcredential

This seems to be a bug after the migration.The command get-azadspcredential needs the parameter -objectid and it gets this parameter from the output of get-azadserviceprincipal. Since it is not working any more,how can i pass the obejctid as input to get-azadspcredential

So what i tried so far:

#List service principal with name sp-acr-mit-pull
$sp=Get-AzADServicePrincipal -SearchString sp-acr-mit-pull
#Get Enddate for sp-acr-mit-pull and also create calculated property "Displayname"
$sp_enddate= @( foreach ($objectid in $sp.id)
{
  Get-AzADspcredential -objectid $objectid -ErrorAction SilentlyContinue | Select-Object -Property @{Name = 'DisplayName'; Expression = { $sp.DisplayName } },@{Name = 'EndDate'; Expression = { $_.EndDateTime -as [datetime] } }
})
$sp_enddate

This seems to work and output is:

DisplayName         EndDate             
-----------         -------             
sp-acr-mit-pull     2/24/2022 3:59:26 PM

But my problem is if Get-AzADServicePrincipal -SearchString <some name> finds more than one entry.E.g

$sp=Get-AzADServicePrincipal -SearchString sp-acr-mit
$sp_enddate= @( foreach ($objectid in $sp.id)
{
Get-AzADspcredential -objectid $objectid -ErrorAction SilentlyContinue | Select-Object -Property @{Name = 'DisplayName'; Expression = { $sp.DisplayName } },@{Name = 'EndDate'; Expression = { $_.EndDateTime -as [datetime] } }
})

$sp_enddate

i get this output

DisplayName                        EndDate             
-----------                        -------             
{sp-acr-mit-pull, sp-acr-mit-push} 2/24/2022 3:59:26 PM
{sp-acr-mit-pull, sp-acr-mit-push} 2/24/2022 3:59:25 PM

This happens because $sp.DisplayName contains two values:

$sp.DisplayName
sp-acr-mit-pull
sp-acr-mit-push

Any idea how to pass the values of $sp.DisplayName one by one?Expected output would be this:

DisplayName                        EndDate             
-----------                        -------             
sp-acr-mit-pull                    2/24/2022 3:59:26 PM
sp-acr-mit-push                    2/24/2022 3:59:25 PM

CodePudding user response:

Please see your script updated below.

$sp=Get-AzADServicePrincipal -SearchString sp-acr-mit
$sp_enddate= @(
    foreach ($objectid in $sp){
        Get-AzADspcredential -objectid $objectid.id -ErrorAction SilentlyContinue | Select-Object -Property @{Name = 'DisplayName'; Expression = { $objectID.DisplayName } },@{Name = 'EndDate'; Expression = { $_.EndDateTime -as [datetime] } }
    }
)

$sp_enddate

After reassessing your Select statement it appears you were outputting all instances of displayname in $sp rather than their individual elements. As where you need to reference this from the instantiated $ObjectID in your foreach loop.

Edit: Missed the fact you were removing all properties from $sp in the foreach loop, updated so it retains them and the call to Get-AzADspcredential references $objectid.id.

  •  Tags:  
  • Related