I want to disable a bunch of computers and move them to another OU in AD thru Powershell. Cannot get to work though, have tried several scripts but it does not work: cannot find object beneath dc=xxx,dc=xxx
However when i do a move adcomputer in powershell directly it does work, so there is something wrong with the get-content when i want to get the input from a file, can someone tell me what is wrong in the script?
Disable computeraccounts via een txt file
$Pclist = Get-Content C:\scripts\Computers.txt # Specify the path to the computers list.
Foreach($pc in $Pclist)
{
Disable-ADAccount -Identity "$pc."
Get-ADComputer -Identity "$pc" | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"
}
In computers.txt there are the computers:
computer1
computer2
computer3
When i do this it works:
get-adcomputer computername | Move-ADObject -TargetPath "OU=1,OU=2,OU=3,DC=domain,DC=name"
This is the error when launching the script:
Disable-ADAccount : Kan geen object met id computer1 vinden onder DC=domain,DC=name.
At line:4 char:1
- Disable-ADAccount -Identity "$pc"
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : ObjectNotFound: (computer1 :ADAccount) [Disable-ADAccount], ADIdentityNotFoundException
- FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount
Get-ADComputer : Kan geen object met id computer1 vinden onder DC=domain,DC=name.
At line:5 char:1
- Get-ADComputer -Identity "$pc" | Move-ADObject -TargetPath "OU=1OU ...
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : ObjectNotFound: (computer1 :ADComputer) [Get-ADComputer], ADIdentityNotFoundException
- FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer