Broken PS script

Jesus Guzman 41 Reputation points


MS support created a script, that was working fine until I change computers. Now, when I try to run it, it fails with the folowing:

Set-AzureADUserExtension : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ObjectId'. Specified method is not supported.

At line:5 char:43

Set-AzureADUserExtension -ObjectId $user.ObjectId -ExtensionNa ...."

Like I mentioned, the only change was a different computer and last time we ran this script was around October 2022.

The script is the following:

Import-Csv .\users.csv | ForEach-Object { $user = Get-AzureADUser -Searchstring $_.UserPrincipalName $logfile = ".\logger.txt"

Set-AzureADUserExtension -ObjectId $user.ObjectId -ExtensionName "extension_My custom extension goes here" -ExtensionValue $user.PhysicalDeliveryOfficeName

Add-Content $logfile -value "Added Extension for $($user.DisplayName)

A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,037 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Steven Fearn 0 Reputation points

    Issue seems to be with Get-AzureADUser I believe objectid might be empty. Try running this in your script to actually test if it exists or not.

    $user = Get-AzureADUser -Searchstring $_.UserPrincipalName
    if ($user -eq $null) {
        Add-Content $logfile -value "Could not find user $($_.UserPrincipalName)"
    0 comments No comments