Powershell not working, no error or warnings:(

Eaven HUANG 2,126 Reputation points
2023-03-04T07:42:44.4+00:00

Dear experts,

I'm trying to script with following to set some users value, add them to their groups based on jobtitle and also department values, then move the users to target OU. The former part is done without issues but no groups were added, they are stay there but not moved to the target OU, what might be wrong?

Any help would be much appreciated.



# Import the AD module
# Import-Module ActiveDirectory

# Set the path of the CSV file
$csvFile = "D:\OneDrive - test\IT Dept\Account Management\ERP_Info\User_Re-enabled.csv"

# Import the CSV file
$users = Import-Csv $csvFile

# Loop through each user in the CSV file
foreach ($user in $users) {

    # Set the user's attributes
    $username = $user.username
    $expireDate = $user.expiredate
    $nationality = $user.nationality

    # Get the user object from AD
    $userObj = Get-ADUser -Identity $username -Properties *

    # Enable the user if it's currently disabled
    if ($userObj.Enabled -eq $false) {
        Enable-ADAccount $userObj
    }

    # Set the user's expiration date
    if ($expireDate -eq "Never") {
        Set-ADAccountExpiration $userObj -DateTime $expireDate
    } else {
        $expireDate = [datetime]::ParseExact($expireDate,"yyyy-MM-dd",$null)
        Set-ADAccountExpiration $userObj -DateTime $expireDate
    }

    # Set the user not to be hidden from address book
    Set-ADUser $userObj -Replace @{msexchhidefromaddresslists=$false}

    # Change the user's logon domain and email address
    Set-ADUser $userObj -UserPrincipalName "$username@test.edu.cn" -EmailAddress "$username@test.edu.cn"

    # Add the user to the groups
    $groups = @("ERP users", "testStaffUsers", "testUsers")

    # Check if the user's title contains Research Assistant or Student Assistant
    if ($userObj.Title -match "(?i)Research Assistant|Student Assistant") {
        $groups += "Research Assistants"
        Move-ADObject $userObj -TargetPath "OU=Research Assistant,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's title contains research fellow
    if ($userObj.Title -match "(?i)research fellow") {
        $groups += "Research Fellows"
    }

    # Check if the user's title contains teacher, pe instructor, Research Scientist, professor, tutor, visiting, lecturer, teaching fellow or editor
    if ($userObj.Title -match "(?i)teacher|pe instructor|Research Scientist|professor|tutor|visiting|lecturer|teaching fellow|editor") {
        $groups += "PanoptoTeachers", "Teachers"
    }

    # Check if the user's title contains Assistant Professor, Associate Professor, Professor, but exclude the title contains visiting
    if ($userObj.Title -match "(?i)Assistant Professor|Associate Professor|Professor" -and $userObj.Title -notmatch "(?i)visiting") {
        $groups += "Faculty"
    }

    # Check if the user's title contains lab manager, lab technician, lab engineer
    if ($userObj.Title -match "(?i)lab manager|lab technician|lab engineer") {
        $groups += "Lab Members"
    }

    # Check if the user's title contains pe instructor
    if ($userObj.Title -match "(?i)pe instructor") {
        $groups += "Physical Education Program"
    }

    # Check if the user's title contains Research Scientist
        if ($user.Title -match "(?i)Research Scientist") {
    Add-ADGroupMember -Identity "Other Scientists" -Members $user.SamAccountName
    }

    # Check if the user's nationality is CHN
    if ($user.Nationality -eq "CHN") {
        Add-ADGroupMember -Identity "Chinese Staff" -Members $user.SamAccountName
    }

    # Check if the user's department is Biotechnology and Food Engineering Program
    if ($user.Department -eq "Biotechnology and Food Engineering Program") {
        Add-ADGroupMember -Identity "Biotechnology and Food Engineering" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Biotechnology and Food Engineering,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Chemical Engineering Program
    if ($user.Department -eq "Chemical Engineering Program") {
        Add-ADGroupMember -Identity "Chemical Engineering" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Chemical Engineering,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Chemistry Program
    if ($user.Department -eq "Chemistry Program") {
        Add-ADGroupMember -Identity "Chemistry" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Chemistry,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is English Program
    if ($user.Department -eq "English Program") {
        Add-ADGroupMember -Identity "English" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=English Teachers,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Environmental Science and Engineering Program
    if ($user.Department -eq "Environmental Science and Engineering Program") {
        Add-ADGroupMember -Identity "Environmental Engineering" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Environmental Engineering,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is General Education Program
    if ($user.Department -eq "General Education Program") {
        Add-ADGroupMember -Identity "PE and GE Course Instructors" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=General Education,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Materials Science and Engineering Program
    if ($user.Department -eq "Materials Science and Engineering Program") {
        Add-ADGroupMember -Identity "Materials Science and Engineering" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Materials Science and Engineering,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Mathematics with Computer Science Program
    if ($user.Department -eq "Mathematics with Computer Science Program") {
        Add-ADGroupMember -Identity "Mathematics with Computer Science Program" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Mathematics with Computer Science Program,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Mechanical Engineering and Robotics Program
    if ($user.Department -eq "Mechanical Engineering and Robotics Program") {
        Add-ADGroupMember -Identity "Mechanical Engineering and Robotics Program" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Mechanical Engineering and Robotics Program,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Physical Education Program
    if ($user.Department -eq "Physical Education Program") {
        Add-ADGroupMember -Identity "Physical Education Program" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Physical Education Program,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }

    # Check if the user's department is Physics Program
    if ($user.Department -eq "Physics Program") {
        Add-ADGroupMember -Identity "Physics" -Members $user.SamAccountName
        Move-ADObject -Identity $user.DistinguishedName -TargetPath "OU=Physics,OU=Academic,OU=Staff,OU=Users,OU=test,DC=test,DC=edu,DC=cn"
    }
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Eaven HUANG 2,126 Reputation points
    2023-03-04T08:16:28.3+00:00

    I've fixed it, no need to follow

    0 comments No comments