Help with Powershell Script for Moving Users Based on CSV List

0
8
Asked By InquisitiveCat42 On

I'm trying to get my Powershell script to work, but I'm hitting a wall. My current script exports a list of users from certain OUs, showing each user's full Distinguished Name and SamAccountName. Now, I want to create another script that can take this list and move users back to their respective Distinguished Names. Every attempt I've made, even with AI help, isn't yielding the results I'm aiming for.

Here's what I've got so far:
```
$CSVPath = "./SAML_Users.csv" # Update with your CSV file path
# Import CSV file
[array] $Users = Import-Csv -Path $CSVPath
# Check if the CSV has data
if(($Users.count -gt 0) -eq $false){
Write-Output "No Entries"
return
}

foreach($User in $Users){
$SamAccountName = $User.SamAccountName
Write-Output $SamAccountName
$TargetDN = $User.DistinguishedName
try{
$ADUser = Get-ADUser -Filter "samaccountname -eq 'gstudent'" | Select-Object

if(-not $ADUser){
Write-Host 'User not found: $SamAccountName'
return
}

Move-ADObject -Identity $ADUser.DistinguishedName -TargetPath $TargetDN
Write-Host $ADUser
} catch{
Write-Host $_.Exception.Message
}
}
```

It doesn't work as intended, and I'm hoping someone here might be able to help me troubleshoot the issues or suggest improvements.

1 Answer

Answered By HelpfulCoder99 On

When you say 'not working as intended', what exactly happens? Clarifying the issue could help! For example, if you get an error like "The operation could not be performed because the object's parent is either uninstantiated or deleted," that suggests there could be an issue with your target path or how you're referencing users.

InquisitiveCat42 -

Thanks for the tip! I get an error saying 'The operation could not be performed because the object's parent is either uninstantiated or deleted' when I run the import. Could it be my target path?

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.