简体   繁体   中英

Trying to move AD user and disable the account running some PowerShell

I'm trying to run some PowerShell to move users to different OU and disable the account.

Here is my code

    $ADUsers = Import-Xlsx 'C:\User Creation\ADUsersRemoval.xlsx' | Foreach {
        $test = Get-ADUser -LdapFilter "samaccountname -eq '$("$Firstname.$Lastname")" |
                Move-ADObject -TargetPath "OU=Users,OU=Graveyard,DC=domain,DC=dc" -PassThru |
                Disable-ADAccount
    }
}

The code runs without errors but doesn't do anything to the user account. What am I doing wrong?

Update

I realized I missed adding the values for $Firstname and $Lastname .

Here is a code I tested with.

    $ADUsers = Import-Xlsx 'C:\User Creation\ADUsersRemoval.xlsx'

    foreach ($User in $ADUsers) {
        $Firstname = $User.Firstname
        $Lastname = $User.Lastname

        Get-ADUser -Filter "samaccountname -eq '$("$Firstname.$Lastname")'" -Properties 'mail'
    }
}

Now works absolutely fine, gives me the details below. clearly reading from my xlsx file ps. Doing it with '$("$Firstname.$Lastname")'" works fine. Any other way then it won't read the xlsx file.

PSComputerName    : server
RunspaceId        : ****************************************
DistinguishedName : CN=user65 test65,CN=Users,DC=domain,DC=com
Enabled           : True
GivenName         : user65
mail              : user65.test65@domain.com
Name              : user65 test65
ObjectClass       : user
ObjectGUID        : afc3fc3b-c43a-4e1b-ad83-804ee605eb00
SamAccountName    : user65.test65
SID               :*************************************
Surname           : test65
UserPrincipalName : user65.test65@domain.com

but when trying to do the task of moving user and disabling. it doesn't action

$ADUsers = Import-Xlsx 'C:\User Creation\ADUsersRemoval.xlsx'

foreach ($User in $ADUsers) {
    $Firstname = $User.Firstname
    $Lastname = $User.Lastname

    Get-ADUser -Filter "samaccountname -eq '$("$Firstname.$Lastname")'" |
        Move-ADObject -TargetPath "OU=Users,OU=Graveyard,DC=domain,DC=com" -PassThru |
        Disable-ADAccount
}

I have managed to get it working. I think it was only missing the Values. I'm sure I tried with them in a few times. Thanks for those that helped :)

Import-Module activedirectory
Install-module PSExcel

Get-command -module psexcel

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-XLSX 'C:\User Creation\ADUsersRemoval.xlsx'

Foreach ($User in $ADUsers){

$Firstname = $User.Firstname
$Lastname = $User.Lastname

Get-ADUser -filter "samaccountname -eq '$("$Firstname.$Lastname")'"| Move-ADObject -TargetPath "OU=Users,OU=Graveyard,DC=Domain,DC=com" -PassThru | Disable-ADAccount}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM