Forum Discussion

Shashikant_Gagare's avatar
Shashikant_Gagare
Copper Contributor
Mar 08, 2023
Solved

Get-AzureADUserManager : Cannot bind argument to parameter 'ObjectId' because it is null.

Hi All,

I am trying to fetch the Manager's Manager using the "Get-AzureADUserManager" function by passing ObjectId of manager.

However while passing the manager's object to get his/her manager , getting an error below :

 

Please help at the earliest.

 

Code : 

foreach($user in $users)
{
$row = $Datatable.NewRow()

$manager=Get-AzureADUserManager -ObjectId $user.ObjectId
$seniorM=Get-AzureADUserManager -ObjectId $manager.ObjectId

$row.Name=$user.GivenName
$row.Surname=$user.Surname
$row.manager=$manager.DisplayName
$row.seniorM=$seniorM.DisplayName

$Datatable.Rows.Add($row)
}

 

Error Message :

Get-AzureADUserManager : Cannot bind argument to parameter 'ObjectId' because it is null.
At line:29 char:43
+ $seniorM=Get-AzureADUserManager -ObjectId $manager.ObjectId
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-AzureADUserManager], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.GetUserManager

 

  • Shashikant_Gagare 

     

    It looks like you are trying to get the manager's manager by passing the ObjectId of the $manager object, but the $manager object itself is null. This is causing the Get-AzureADUserManager cmdlet to fail.

    To avoid this error, you should first check if the $manager object is not null before trying to get its manager. You can do this by adding a simple if statement:

     

     

    foreach($user in $users)
    {
        $row = $Datatable.NewRow()
    
        $manager=Get-AzureADUserManager -ObjectId $user.ObjectId
    
        if($manager -ne $null)
        {
            $seniorM=Get-AzureADUserManager -ObjectId $manager.ObjectId
            $row.seniorM=$seniorM.DisplayName
        }
    
        $row.Name=$user.GivenName
        $row.Surname=$user.Surname
        $row.manager=$manager.DisplayName
    
        $Datatable.Rows.Add($row)
    }

     

    This way, if the $manager object is null, the if statement will prevent the $seniorM object from being created and the script will continue without throwing an error.

2 Replies

  • Shashikant_Gagare 

     

    It looks like you are trying to get the manager's manager by passing the ObjectId of the $manager object, but the $manager object itself is null. This is causing the Get-AzureADUserManager cmdlet to fail.

    To avoid this error, you should first check if the $manager object is not null before trying to get its manager. You can do this by adding a simple if statement:

     

     

    foreach($user in $users)
    {
        $row = $Datatable.NewRow()
    
        $manager=Get-AzureADUserManager -ObjectId $user.ObjectId
    
        if($manager -ne $null)
        {
            $seniorM=Get-AzureADUserManager -ObjectId $manager.ObjectId
            $row.seniorM=$seniorM.DisplayName
        }
    
        $row.Name=$user.GivenName
        $row.Surname=$user.Surname
        $row.manager=$manager.DisplayName
    
        $Datatable.Rows.Add($row)
    }

     

    This way, if the $manager object is null, the if statement will prevent the $seniorM object from being created and the script will continue without throwing an error.

Resources