Need Help with Moving Users in PowerShell Script

0
18
Asked By CuriousCoder92 On

I'm a beginner struggling with a PowerShell script intended to manage user accounts in Active Directory. Currently, my script exports a list of users from specific Organizational Units (OUs) that includes their full distinguished names and SamAccountNames. I'm trying to create a new script that references this list, matches each user to their distinguished name, and moves them to their designated location. However, I keep running into issues. My current script isn't yielding the results I want. Every attempt, even with AI help, has left me stumped. Below is the current script I'm working on. I'd appreciate any insights on how I can make this work!

4 Answers

Answered By ScripterSam On

Make sure you're correctly fetching the user in your script by avoiding hardcoding the SamAccountName like this:
`$ADUser = Get-ADUser -Filter "samaccountname -eq 'gstudent'"`. Instead, use the `$SamAccountName` variable directly from your user list. This will ensure the script works for all users listed in your CSV, not just one specific account.

Answered By PowerfulScriptor On

Before jumping into the script changes, could you clarify what you meant by "not working as intended"? Specific error messages can really help diagnose the issue. For example, are you seeing errors when you try to run the script, or does it not produce the expected output? Any details you can provide will help us assist you better!

CuriousCoder92 -

Oh, sorry about that! I get an error saying, "The operation could not be performed because the object's parent is either uninstantiated or deleted." That's where I get stuck.

Answered By ADGuru On

One more thing to check is whether the `Move-ADObject` is using the right identity. Ensure that `$ADUser` is not null and contains valid information before you call the move command. If the user doesn't exist, that could also trigger the type of error you're encountering.

Answered By TechWhiz123 On

I think one key issue might be with how you're using the `$TargetDN` variable. It sounds like you might need to ensure that it only contains the OU path, not the full distinguished name of the user, which could cause the move command to fail. You could try changing your script to remove everything up to and including the first comma like this:
`$TargetDN = $User.DistinguishedName -replace '^.*?,(?=[A-Z]{2}=)'`. This way, you ensure you're only passing in the OU part to the move command.

CuriousCoder92 -

Thanks for the suggestion! I'll give that a try and see if it helps with the errors.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.