none
script to move user OU, change description, disable account and add XXX infront fo samaccountname RRS feed

  • Question

  • I'm working on a script to move user OU, change description, disable account and add XXX infront fo samaccountname from a csv with samaccountname, but is not working. Any Ideas?

    import-module activedirectory
    Get-Content C:\script\user.csv | Foreach {Get-ADUser $_.DN | Move-ADObject -TargetPath "OU=NewUsers,DC=user,DC=global,DC=local" -PassThru | Disable-ADAccount | Set-ADUser $_.DN -Description Termed by local IT -SamAccountName "XXX"+"SamAccountName"}

    • Moved by Bill_Stewart Monday, June 6, 2016 6:12 PM Unanswerable drive-by question
    Thursday, March 10, 2016 4:05 PM

All replies

  • Not working how?

    Thursday, March 10, 2016 4:09 PM
  • What does not working mean?

    Do not place all of you code on  one line.  It is unreadable:

    Import-Csv C:\script\user.csv | 
    	Foreach {
    		$user=Get-ADUser $_.DN | 
    			Move-ADObject -TargetPath "OU=NewUsers,DC=user,DC=global,DC=local" -PassThru
    		$user|Disable-ADAccount
    		$user|Set-ADUser -Description Termed by local IT -SamAccountName 'XXX'+$user.SamAccountName
    	}


    \_(ツ)_/


    • Edited by jrv Thursday, March 10, 2016 5:27 PM
    • Proposed as answer by jrv Thursday, March 10, 2016 5:27 PM
    • Unproposed as answer by Bill_Stewart Monday, June 6, 2016 6:11 PM
    Thursday, March 10, 2016 4:11 PM
  • Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At line:2 char:54
    + Get-Content C:\script\user.csv | Foreach {Get-ADUser $_.DN | Move-ADObject -Targ ...
    +                                                      ~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Thursday, March 10, 2016 4:11 PM
  • Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At line:2 char:54
    + Get-Content C:\script\user.csv | Foreach {Get-ADUser $_.DN | Move-ADObject -Targ ...
    +                                                      ~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser


    Your input is bad. Do you actually have a DN column?

    Thursday, March 10, 2016 4:14 PM
  • Yes there is a column DN on the csv
    Thursday, March 10, 2016 5:18 PM
  • still not working
    Thursday, March 10, 2016 5:18 PM
  • still not working

    When in doubt simplify:

    Import-Csv C:\script\user.csv |
          Foreach {
               Write-Host $_.DN
              Get-ADUser$_.DN
    }


    \_(ツ)_/


    • Edited by jrv Thursday, March 10, 2016 5:26 PM
    Thursday, March 10, 2016 5:25 PM
  • Please take a look at the Description Parameter it should be inside quotes
    Import-Csv C:\script\user.csv | 
    	Foreach {
    		$user=Get-ADUser -Identity $_."DN" | 
    			Move-ADObject -TargetPath "OU=NewUsers,DC=user,DC=global,DC=local" -PassThru
    		$user|Disable-ADAccount
    		$user|Set-ADUser -Description "Termed by local IT" -SamAccountName 'XXX'+$user.SamAccountName
    	}








    • Edited by Alvaro Saenz Thursday, March 10, 2016 5:51 PM
    • Proposed as answer by Alvaro Saenz Thursday, March 10, 2016 5:56 PM
    Thursday, March 10, 2016 5:31 PM
  • still not working

    This is a completely useless response if you want us to be able to help you. I'll ask again, not working how?


    Thursday, March 10, 2016 6:20 PM
  • Sorry Guy,

    I was able to make it work with a little tweaking from jrv response

    Here is the final result

    import-module activedirectory Import-Csv C:\script\user.csv | Foreach { $user=Get-ADUser $_.DN | Move-ADObject -TargetPath "OU=NewUsers,DC=user,DC=global,DC=local" -PassThru

    $user|Set-ADUser -Description "Termed by local IT"

    $user|Disable-ADAccount

    $user|Set-ADUser -SamAccountName ('XXX'+$_.DN) }



    • Edited by RM0823 Thursday, March 10, 2016 7:22 PM
    Thursday, March 10, 2016 7:21 PM