Forum Discussion
Get-AzureADUserManager : Cannot bind argument to parameter 'ObjectId' because it is null.
- Mar 08, 2023
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.
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.
Varun_Ghildiyal - Script is working fine after applying nullable check. Thanks alot for your quick help.