Search code examples
powershellactive-directory

Powershell Setting other Attributes


Okay so I'm trying to figure out how to use PowerShell to write to AD in both the phone number and the other attributes:

enter image description here

Typically I am just using a code that will take a name and a number and apply it to the Mobile, telephone, pager etc.

Function AddNumberToAD
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [Alias('UserInfo')]
        [string]$UserNameToAdd,
        
        [Parameter(Mandatory=$true)]
        [Alias('PhoneNumber')]
        [string]$UsersPhoneNumberToAdd
    )
    
    $user = EmailToName $UserNameToAdd
    oneEmptyLine
    
    $usersRealName = Get-ADUser -Identity $user -Properties Name
    
    $title    = 'Assign Number'
    Write-Host "Users Name: ", $usersRealName.Name
    write-Host "Username: ", $user
    Write-Host "Email: ",$UserNameToAdd
    Write-Host "Phone Number to be Added: ",$UsersPhoneNumberToAdd
    
    $question = 'Assign Phone Number as?'
    $choices  = @(
        [System.Management.Automation.Host.ChoiceDescription]::new("&Mobile Number", "Verizon Wireless Number")
        [System.Management.Automation.Host.ChoiceDescription]::new("&Telephone Number", "Teams Phone Number")
        [System.Management.Automation.Host.ChoiceDescription]::new("&Cancel", "Exit Function")
    )

    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 2)
    if ($decision -eq 0) 
    {
        
    }
    elseif($decision -eq 1)
    {
        Write-Host "Setting user Telephone Number to Active Directory..." -ForegroundColor Green
        Set-ADUser $user -Add @{telephonenumber=$UsersPhoneNumberToAdd}
    }
    Elseif($decision -eq 2)
    {
        Return
    }           
    Else
    {
        Write-Host "Invalid input recieved..." -ForegroundColor Red
    

Works just fine, however we uttilize the other function and typically match the mobile number to the number: enter image description here

(ignore the 3, this is just an example)

I would like to write the script so it would insert the mobile number into the mobile other attribute but i can not figure out how to. Any assistance would be appreciated.

I have tried -OtherAttribute but I cant seem to make it work.

Tried just about everything that I can think of. Just trying to get it to replicate into the other column,

Telephone: enter image description here


Solution

  • The parameter you can use to set the user's mobile phone is -MobilePhone:

    if ($decision -eq 0) {
        Set-ADUser $user -MobilePhone $UsersPhoneNumberToAdd
    }
    

    If you want to use -Add to include the user's mobile (Other) then you need to look at the attribute's ldapDisplayName, for instance, for the mobile (Primary) and mobile (Other) you can use mobile and otherMobile.

    For your current function however if you want to set both attributes in one go then you will need to add a new parameter to accept both values, I'm using $ParameterForMobile and $ParameterForOtherMobile as a placeholder in this case.

    if ($decision -eq 0) {
        Set-ADUser $user -Add @{
            mobile      = $ParameterForMobile
            otherMobile = $ParameterForOtherMobile
        }
    }