Thursday, May 16, 2013

Copy Attribute Values–PowerShell Script

This can be used to copy a value from one attribute to another.  In the below example, I am coping Exchange Alias (mailnickname) value to extensionattribute14

Script:

#
# Author - Santhosh Sivarajan
# Version 1.0
Clear
$N = 0
$UserInfoFile = New-Item -type file -force "D:\Scripts\UserInfo.txt"
"distinguishedName`tAlias" | Out-File $UserInfoFile -encoding ASCII
$ObjFilter = "(&(objectCategory=person)(objectCategory=User)(mail=*)(!extensionAttribute14=*))"
$objSearch = New-Object System.DirectoryServices.DirectorySearcher
$objSearch.PageSize = 15000
$objSearch.Filter = $ObjFilter 
$objSearch.SearchRoot = "LDAP://DC=sivarajan,DC=com"
$AllObj = $objSearch.FindAll()
    foreach ($Obj in $AllObj)
    {   
        $user = [ADSI] $Obj.path
        $objItemT = $Obj.Properties
        $tAlias = $objItemT.mailnickname
        $tUDN =  $objItemT.distinguishedname
        Write-host -NoNewLine "Updating.... $tUDN"
        $user.psbase.invokeSet("extensionattribute14","$tAlias")
        $user.setinfo()
        "$tUDN`t$tAlias" | Out-File $UserInfoFile -encoding ASCII -append
        Write-host "Done!"
        $N ++   
    }

Write-host "Successfully updated $N user obejcts.  Please reveiw $UserInfoFile for more information"

0 comments:

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More