Broken PS script

Jesus Guzman 41 Reputation points
2023-02-24T20:18:54.1+00:00

Greetings,

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)


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

1 answer

Sort by: Most helpful
  1. Steven James Fearn 0 Reputation points
    2023-02-24T22:20:26.01+00:00

    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)"
        continue
    }
    
    
    0 comments No comments