ADuser - Align department attribute with departmentnumber Attribute

Welcome Forums General PowerShell Q&A ADuser - Align department attribute with departmentnumber Attribute

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
8 months, 1 week ago.

  • Author
    Posts
  • #95558

    Participant
    Points: 0
    Rank: Member

    Every department we have has a departmentnumber, so IF HR has departmentnumber 60, then a user in the HR department should have 60 set in the departmentnumber attribute. In our organisation, departmentnames and numbers will not change.
    If somebody suggest a Hashtable instead, I would like a small demo to build from, because I am not that skilled with Hashtables but I can understand they sometimes can make your life easier in powershell.

    Param (
    [String]$Username ,
    [String]$HRDepartment = "HR" ,
    [int]$HR = "60"
    )
    Get-ADUser -Identity $Username
    IF ( $_.$Username.Department -eq $HRdepartment ) {Set-ADUser $Username -departmentnumber $HR }

    Then I get this error:

    Get-ADUser : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
    At C:\PS\GetTest2.ps1:11 char:22
    + Get-ADUser -Identity $Username
    +                      ~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
  • #95562

    Participant
    Points: 0
    Rank: Member

    Edited so I get email notification.

  • #95565

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    It's telling you that $Username is empty. When you run your script, you need to provide a value for it.

    Also, -DepartmentNumber is not a parameter of Set-ADUser – you can see that in the help. The help also has examples if how to set properties for which there isn't a parameter. From the docs:

    Set-ADUser -Identity GlenJohn -Replace @{title="director";mail="glenjohn@fabrikam.com"}
    

    That @ thing is the hash table. It's just a list of property=value pairs.

  • #95628

    Participant
    Points: 0
    Rank: Member

    Hi Mr. Jones
    I found this example:

    $user = Get-ADUser GlenJohn -Properties mail,department
    $user.mail = "glen@fabrikam.com"
    $user.department = "Accounting"
    Set-ADUser -instance $user

    And then added the IF like this:

    $user = Get-ADUser testuser -Properties department,departmentnumber
    $HRnr="10"
    $ITnr="12"
    $Salesnr="90"
    
    IF ( $user.department -eq "HR" ) 
    {
    $user.departmentnumber = $HRnr
    Set-ADUser -instance $User
    }
    IF ( $user.department -eq "IT" ) 
    {
    $user.departmentnumber = $ITnr
    Set-ADUser -instance $User
    }
    IF ( $user.department -eq "Sales" ) 
    {
    $user.departmentnumber = $Salesnr
    Set-ADUser -instance $User
    }
    

    I have thought that I need to add
    Param(
    And add a variable for the Username since it should run automated.
    )
    I dont mind to hardcode a lot of IF's sentences. It is more in the area of, when I need to add an extra attribute value.
    So after department,departmentnumber there will also be Division.
    How do I smarten this up?

The topic ‘ADuser - Align department attribute with departmentnumber Attribute’ is closed to new replies.