lakeek
lakeek

Reputation: 25

Writing a hashtable value to an attribute

Powershell newbie here, my first script.

I have user objects with an AD custom attribute named tvCode with a values of 123456 or 6787682 or 983736 etc.

I would like to script something that will get the tvCode value from the user object

   When:
           123456 = Sony 

           6787682 = Samsung

           9837343 = LG

Write the value of "Sony" or "Samsung" or "LG" to the "City" attribute of the user object.

Looks like i may need to use a hashtable.

If possible do this for a specific OU

hope this makes sense

thanks

Upvotes: 0

Views: 293

Answers (1)

scrthq
scrthq

Reputation: 1076

function Convert-TVCode {
    Param
    (
    [parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
    [String[]]
    $Code
    )
    Process {
        foreach ($C in $Code) {
            switch ($C) {
                "123456" {$brand = "Sony"}
                "6787682" {$brand = "Samsung"}
                "9837343" {$brand = "LG"}
                default {
                    $brand = $null
                    Write-Warning "$C not included in switch statement. Returning"
                    return
                }
            }
            if ($brand) {
                Write-Verbose "Code '$C' matched to Brand '$brand' -- searching for users to update"
                Get-ADUser -Filter "tvCode -eq '$C'" | Set-ADUser -Replace @{tvCode=$brand}
            }
        }
    }
}

This function will allow you to update any users that have their tvCode attribute set as one of the target numerical values. You can have it hit multiple codes at once as well.

Examples:

Convert-TVCode -Code 123456
Convert-TVCode -Code 123456,6787682
Convert-TVCode -Code 123456,6787682,9837343 -Verbose

Update the switch statement in the function to customize it to your actual values and let me know if you have any questions!

Upvotes: 1

Related Questions