Jump to content

PowerShell - Update AzureAD User Information

Go to solution Solved by JTPOTATO,

So we got there in the end 🙂

 

After running through a few edits here and there, and clearing it up a little, the below successfully works to update Azure Information automatically (So long as the details in the CSV are correct)

 

#Connect to Azure AD - Make sure you sign in with an admin account
Connect-AzureAD
# Get CSV content
$CSVrecords = Import-Csv "C:\Book2.csv" -Delimiter ","

# Create arrays for skipped and failed users
$SkippedUsers = @()
$FailedUsers = @()

# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"  
    if ($user) {
        try{
        #This will update 
        Set-AzureADUser -Company $CSVrecord.Company -Country $CSVrecord.Country -Department $CSVrecord.Department -JobTitle $CSVrecord.Title
        Set-AzureADUserManager -ObjectId $CSVrecord.ObjectID -RefObjectId $CSVrecord.ManagerID
        } catch {
        $FailedUsers += $upnC
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

# Array skipped users
# $SkippedUsers

# Array failed users
# $FailedUsers

 

Hi everyone!

 

I'm struggling with some PowerShell script I'm making and I just can't get it to run through everything.

 

So the idea is to import a CSV with the headers and referencing information as below:

 

UserProfileName, ObjectID, Company, CountryOrRegion, Department, Title, Manager, ManagerObjectID

 

I've made the below Powershell script based on the CSV headers:

Connect-AzureAD
# Get CSV content
$CSVrecords = Import-Csv "C:\Book2.csv" -Delimiter ","

# Create arrays for skipped and failed users
$SkippedUsers = @()
$FailedUsers = @()

# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-AzureADUser -Filter $UserPrincipalName  
    if ($user) {
        try{
        $user | Set-AzureADUser -Company $CSVrecord.Company -Country $CSVrecords.CountryOrRegion -Department $CSVrecords.Department -Title $CSVrecords.Title
        Set-AzureADUserManager -ObjectId $CSVrecord.ObjectID -RefObjectId $CSVrecord.ManagerID
        } catch {
        $FailedUsers += $upnC
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

# Array skipped users
# $SkippedUsers

# Array failed users
# $FailedUsers

 

 

I'm getting a failure where it picks up the user but fails to update them. Now I'm thinking It's the part I've highlighted in Blue that might be causing the issues... any help certainly welcome!

Link to comment
Share on other sites

Link to post
Share on other sites

Can't see what you've highlighted in the code tags. 

 

Are you getting any errors? If so, could you post them?

 

Also, when you say it's failing to update, are you seeing the users failing or being skipped, as your code writes out which it does. 

 

I'm not super familiar with the Azure AD Module, but the line:

$user = Get-AzureADUser -Filter $UserPrincipalName

Appears to be referencing a variable that doesn't exist in your script, unless there's more to your script you haven't posted, or it's a built in variable in the module. Did you mean to use $upn instead of $UserPrincipalName in the filter?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Oshino Shinobu said:

Can't see what you've highlighted in the code tags. 

 

Are you getting any errors? If so, could you post them?

 

Also, when you say it's failing to update, are you seeing the users failing or being skipped, as your code writes out which it does. 

 

I'm not super familiar with the Azure AD Module, but the line:


$user = Get-AzureADUser -Filter $UserPrincipalName

Appears to be referencing a variable that doesn't exist in your script, unless there's more to your script you haven't posted, or it's a build in variable in the module. Did you mean to use $upn instead of $UserPrincipalName in the filter?

Ah, just noticed the highlights disappearing

 

it was this line I'm thinking doesn't reference anywhere:

 

Set-AzureADUserManager -ObjectId $CSVrecord.ObjectID -RefObjectId $CSVrecord.ManagerID

The errors outputted match the error output from the script... so the script is picking up the current column in the CSV... error picture below

 

image.png.af687fa4c34fc65b02db24290736ed65.png

 

I've attached how the CSV has been setup also.

 

The Azure module is good, but the fact they made the Manager field it's own command is very annoying!

Book2 - Copy.csv

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, JTPOTATO said:

Ah, just noticed the highlights disappearing

 

it was this line I'm thinking doesn't reference anywhere:

 


Set-AzureADUserManager -ObjectId $CSVrecord.ObjectID -RefObjectId $CSVrecord.ManagerID

The errors outputted match the error output from the script... so the script is picking up the current column in the CSV... error picture below

 

image.png.af687fa4c34fc65b02db24290736ed65.png

 

I've attached how the CSV has been setup also.

 

The Azure module is good, but the fact they made the Manager field it's own command is very annoying!

Book2 - Copy.csv 451 B · 0 downloads

Okay, looks like the Azure AD module more or less works the same as the standard AD module so I'll set up some test stuff in my domain and see if I get the same results.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Oshino Shinobu said:

Okay, looks like the Azure AD module more or less works the same as the standard AD module so I'll set up some test stuff in my domain and see if I get the same results.

@JTPOTATOSeems I can't replicate it in my own environment as some of it is specific to Azure. 

 

I'd advise removing the Set-AzureADUserManager part of the script and try running it again to see if it works. If so, you definitely know it's that line causing it to fail. 

 

I'd also try running the Set-AzureADUserManager line outside of the try and catch to see if you can get any more detailed errors from it, perhaps with verbose enabled

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Oshino Shinobu said:

@JTPOTATOSeems I can't replicate it in my own environment as some of it is specific to Azure. 

 

I'd advise removing the Set-AzureADUserManager part of the script and try running it again to see if it works. If so, you definitely know it's that line causing it to fail. 

 

I'd also try running the Set-AzureADUserManager line outside of the try and catch to see if you can get any more detailed errors from it, perhaps with verbose enabled

Thanks, I'll split them up and give them a go. If they work, it'll just be a case of trying to merge them somehow using the same CSV import as the variable... this is a new type of thing for us!

Link to comment
Share on other sites

Link to post
Share on other sites

Change your $user reference line. userPrincipalName is not a valid identifier/parameter in Azure v2. You have to filter it.

 

$user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"

Link to comment
Share on other sites

Link to post
Share on other sites

 

3 hours ago, MikeS2483 said:

Change your $user reference line. userPrincipalName is not a valid identifier/parameter in Azure v2. You have to filter it.

 

$user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"

Thanks for this, So I've updated the script and made a slight change to the piping also

 

Realised I did a rookie mistake and call some of the variables under the plural $CSVrecords rather than $CSVrecord

 

That's fixed the general user update, however I'm still struggling to use the "Set-AzureADUserManager" command within the repeat

 

Any ideas

Link to comment
Share on other sites

Link to post
Share on other sites

So we got there in the end 🙂

 

After running through a few edits here and there, and clearing it up a little, the below successfully works to update Azure Information automatically (So long as the details in the CSV are correct)

 

#Connect to Azure AD - Make sure you sign in with an admin account
Connect-AzureAD
# Get CSV content
$CSVrecords = Import-Csv "C:\Book2.csv" -Delimiter ","

# Create arrays for skipped and failed users
$SkippedUsers = @()
$FailedUsers = @()

# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"  
    if ($user) {
        try{
        #This will update 
        Set-AzureADUser -Company $CSVrecord.Company -Country $CSVrecord.Country -Department $CSVrecord.Department -JobTitle $CSVrecord.Title
        Set-AzureADUserManager -ObjectId $CSVrecord.ObjectID -RefObjectId $CSVrecord.ManagerID
        } catch {
        $FailedUsers += $upnC
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

# Array skipped users
# $SkippedUsers

# Array failed users
# $FailedUsers

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×