question

JooVictor-4806 avatar image
0 Votes"
JooVictor-4806 asked ·

Change users in AD

Hi guys!

I'm having trouble writing a script in PowerShell. I need a script that reads a CSV with two columns, later I need to filter the field in the first column and apply a value that is inside the other column in the user attributes in AD.

I wrote this script below but it is not working:

Import-Module ActiveDirectory
$Users = "C:\Users\joao.victor\Documents\leiaute.csv"
Import-CSV $Users -Header UserPrincipalName,title | %
Get-ADUser -Filter 'Name -like "
"' -SearchBase 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet' -Properties | % {Set-ADUser $ -add @{title=$.title}}

Can anybody help me?


windows-server-powershellwindows-active-directory
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Okay, let's try this version. The AD filter has always been quirky. This one contains an additional bit of data validation:

 Import-Module ActiveDirectory   # this probably isn't necessary any longer
 $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
 $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
 Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
     ForEach-Object{
         if ($_.UserPrincipalName.trim().length -ge 3){
             $upn = $_.UserPrincipalName.trim()
             if ($u = Get-ADUser -Filter {UserPrincipalName -eq $upn} -SearchBase $OU){  # "UPN" is unique. Using "-like" isn't necessary
                 $u | Set-ADUser -Title $_.title 
             }
             else{
                 Write-Host "Could not locate user: " $upn
             }
         }
         else{
             Write-Host "The UserPrincipalName '$($_.UserPrincipalName)' is either empty or too short"
         }
     }
· 2
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

still not working.

79177-screenshot-5.png


0 Votes 0 ·
screenshot-5.png (106.2 KiB)

Dear friend, I inform you that your script worked perfectly. The error presented earlier was due to the wrong path of the script. It worked perfectly. Thank you very much!

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Try this:

 Import-Module ActiveDirectory   # this probably isn't necessary any longer
 $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
 $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
 Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
     ForEach-Object{
         if ($u = Get-ADUser -Filter "UserPrincipalName -eq $($_.UserPrincipalName)" -SearchBase $OU){  # "UPN" is unique. Using "-like" isn't necessary
             $u | Set-ADUser -Title $_.title 
         }
         else{
             Write-Host "Could not locate user: " $_.UserPrincipalName
         }
     }
· 6
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the feedback.
The script that passed me returned the following error:

Get-ADUser : Error parsing query: 'UserPrincipalName -eq usuario.teste' Error Message: 'syntax error' at position: '23'.
At line:6 char:19
+ if ($u = Get-ADUser -Filter "UserPrincipalName -eq $($_.UserPrincipalNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser


0 Votes 0 ·

Change the filter to this (with no quotation marks at all)

 - Filter {UserPrincipalName -eq $_.UserPrincipalName}
1 Vote 1 ·
JooVictor-4806 avatar image JooVictor-4806 RichMatheisen-8856 ·

didn't work.

78924-screenshot-2.png


0 Votes 0 ·
screenshot-2.png (96.2 KiB)
Show more comments
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

Please check to see if this works.

 $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
 $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
 Import-CSV $Users -Header UserPrincipalName,title | ForEach-Object{
     $UPN = $_.UserPrincipalName
     Get-ADUser -Filter {UserPrincipalName -eq $UPN} -SearchBase $OU | Set-ADUser -Title $_.title
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

didn't work.

79200-screenshot-6.png


0 Votes 0 ·
screenshot-6.png (71.9 KiB)

How about adding the "-server" parameter ?

 Get-ADUser -Filter {UserPrincipalName -eq $UPN} -SearchBase $OU -Server elv.intranet

https://social.technet.microsoft.com/Forums/en-US/cbb98c7c-455c-4ce7-93d6-6a16e26fdc55/addadgroupmember-to-add-group-membership-to-parent-domain?forum=winserverpowershell


0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Are you working in a multi-domain Active Directory forest? Is there a Global Catalog server in your AD site?

Try this code and see if the exception contains the name of the server that you should be using:

 Import-Module ActiveDirectory   # this probably isn't necessary any longer
 $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
 $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
 Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
     ForEach-Object{
         if ($_.UserPrincipalName.trim().length -ge 3){
             $upn = $_.UserPrincipalName.trim()
             Try{
                 $u = Get-ADUser -Filter {UserPrincipalName -eq $upn} -SearchBase $OU -ErrorAction STOP
                 if ($u){
                    $u | Set-ADUser -Title $_.title
                 }
                 else{
                     Write-Host "Could not locate user: " $upn
                 }
             }
             Catch{
                 Write-Host "Finding '$upn' caused an exception. See if the referral points to the correct server for the user."
                 Write-Host $_.Exception.Referral
             }
         }
         else{
             Write-Host "The UserPrincipalName '$($_.UserPrincipalName)' is either empty or too short"
         }
     }
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.