none
The scritpt makes the GET, but does not do SET to change the attributes RRS feed

  • Question

  • This my Script.

    The script excuta without errors, but does not change the attributes, help-me please


    =================================================

    Get-Command -Module ActiveDirectory 
    $users = Get-ADUser -SearchBase "dc=icaro,dc=br" -Filter * -Properties DisplayName, Office, Department
    $Users = Import-Csv -Delimiter ";" -Path "C:\Users\test\Documents\test.txt"  


    ForEach ( $user in $users ) 

      $Displayname = $User.usuario
      $office = $User.Matricula 
      $Department = $user.'Local de trabalho'

    "modificando $($user.usuario)" 
    Get-ADUser -Filter {DisplayName -eq "$Displayname"} | 
    Set-ADUser $Displayname -office $office -Department $Department

    ==============================================

    Wednesday, January 8, 2014 1:19 PM

Answers

  • Hi Joan,

    this script has plenty of lines, that are not needed. Here's a version that ought to work:

    # Loads the Active Directory commands
    Import-Module ActiveDirectory
    
    # Load the users from file
    $Users = Import-Csv -Delimiter ";" -Path "C:\Users\test\Documents\test.txt"
    
    # For all users do
    Foreach ($User in $Users)
    {
    	# Load properties (make sure those are typed as in the Csv)
    	$DisplayName = $User.usuario
    	$Office = $User.Matricula 
    	$Department = $user.'Local de trabalho'
    	
    	# Grab the user from the AD
    	$TempUser = Get-ADUser -Filter { DisplayName -like $DisplayName }
    	
    	# Write Changes to AD Object
    	$TempUser | Set-ADUser -Office $Office -Department $Department
    }

    The "Get-Command"-Command lists all available commands from the module ActiveDirectory. It does not load them however. That's what "Import-Module" is for.

    Loading all users at the beginning is redundant, you immediately overwrite the same variable in the next line with the content from the Csv file.

    The Get-ADUser | Set-ADUser Pipeline means you do not need to identify the user for the Set-ADUser-Cmdlet with the $DisplayName, as you did. I split it into two different lines to make it more easily commentable.

    Cheers and may that work,
    Fred


    There's no place like 127.0.0.1

    Wednesday, January 8, 2014 1:35 PM

All replies

  • Hi Joan,

    this script has plenty of lines, that are not needed. Here's a version that ought to work:

    # Loads the Active Directory commands
    Import-Module ActiveDirectory
    
    # Load the users from file
    $Users = Import-Csv -Delimiter ";" -Path "C:\Users\test\Documents\test.txt"
    
    # For all users do
    Foreach ($User in $Users)
    {
    	# Load properties (make sure those are typed as in the Csv)
    	$DisplayName = $User.usuario
    	$Office = $User.Matricula 
    	$Department = $user.'Local de trabalho'
    	
    	# Grab the user from the AD
    	$TempUser = Get-ADUser -Filter { DisplayName -like $DisplayName }
    	
    	# Write Changes to AD Object
    	$TempUser | Set-ADUser -Office $Office -Department $Department
    }

    The "Get-Command"-Command lists all available commands from the module ActiveDirectory. It does not load them however. That's what "Import-Module" is for.

    Loading all users at the beginning is redundant, you immediately overwrite the same variable in the next line with the content from the Csv file.

    The Get-ADUser | Set-ADUser Pipeline means you do not need to identify the user for the Set-ADUser-Cmdlet with the $DisplayName, as you did. I split it into two different lines to make it more easily commentable.

    Cheers and may that work,
    Fred


    There's no place like 127.0.0.1

    Wednesday, January 8, 2014 1:35 PM
  • fred,

    thank you very much, worked perfectly ;)

    Wednesday, January 8, 2014 1:49 PM