Import user photos to ad in several different ways

Source: Internet
Author: User
Tags samaccountname

Modify the ad architecture so that the Avatar can be displayed in the gal, you need to make it copy in the Global Catalog (GC,

By default, the "thumbnailphoto" attribute value of the object is not copied in GC. You can modify the ad architecture to implement this function.

1. Open cmd as a manager and run regsvr32 schmmgmt. DLL to register the ad architecture Management Unit, as shown in:

2. Open the MMC console and add the ad architecture Management Unit

3. Expand the "attributes" node in the Active Directory architecture management unit and locate "thumbnailphoto ".

4. Open the "thumbnailphoto" attribute dialog box and select "Copy this attribute to Global Catalog" on the "General" option card ".

Save the modification. You can modify the configurations in the following ways.

Tip:

We need to prepare some photos. The size of the photos should not exceed 30 kb, so the size should be well controlled. Generally, the size of the photos is about 96X96, which is too big and meaningless, because these photos exist in the Active Directory, if they are too large, the active database will increase, thus affecting replication.

1. Modify the powershell script

Script 1:

 
$ Samname = read-host"Enter a username"
 
 
$ Root = [ADSI]'Gc: // dc = UC, Dc = local'
 
$ Searcher = new-Object System. directoryservices. directorysearcher ($ root)
 
$ Searcher.Filter="(& (Objectclass = user) (samaccountname = $ samname ))"
 
$ User = $ searcher. findall ()
 
$ Userdn = $ user [0]. Path
 
$ Userdn = $ userdn. Trim ("GC")
 
$ Userdn ="LDAP"+ $ Userdn
 
 
 
FunctionSelect-filedialog
 
{
 
Param([String] $ title, [String] $ directory, [String] $ filter ="All files (*. *) | *.*")
[System. reflection. Assembly]: loadwithpartialname ("System. Windows. Forms") | Out-Null
 
$ Objform = new-Object System. Windows. Forms. openfiledialog
 
$ Objform. initialdirectory = $ directory
 
$ Objform. Filter = $ Filter
 
$ Objform. Title = $ title
 
$ Objform. showHelp = $ true
 
$ Show = $ objform. showdialog ()
 
If ($ show-EQ "OK")
 
{
 
Return $ objform. filename
 
}
 
Else
 
{
 
Write-Error"Operation canceled by user ."
}
 
}
 
 
 
$ Photo = select-filedialog-title"Select a photo"-Directory"% USERPROFILE %"-Filter"JPG images (*. jpg) | *. jpg"
 
 
 
$ User = [ADSI] ($ userdn)
 
[Byte [] $ file = Get-content $ photo-encoding byte
 
 
 
# Clear previous image if exist
 
$ User. properties ["Thumbnailphoto"]. Clear ()
 
 
 
# Write the image to the user's thumbnailphoto attribute by converting the byte [] to base64string
$ User. properties ["Thumbnailphoto"]. Add ([system. Convert]: tobase64string ($ file ))
 
 
 
# Commit the changes to AD
 
$ User. commitchanges ()
 
 
 
 

Script 2:

 
# Add-adphoto powershell V1 compatibile script for updating
 
# User thumbnailphoto attribute. resizes input photo to recommended
# Dimensions and size. Only updates for the currently logged in user.
 
# This is a script for user self service.
 
 
 
# Author: Nathan Linley
 
# Site: http://myitpath.blogspot.com
 
 
 
$ Infile = $ ARGs [0]
 
$ Aspect = $ ARGs [1]
 
 
 
FunctionUsage {
 
Write-host"Usage: Add-adphoto filename [aspect]"
 
Write-host"Provide the name of an image file in your current directory ."
Write-host"If you wish to preserve the aspect ratio of the image, type"
 
Write-host"1 after your file name. images are resized to the recommended"
 
Write-host"96X96, converted to jpg and set to 70% quality to limit size ."
 
Exit
 
 
 
}
 
$ Imagefile = (PWD). Path +"\" + $ Infile
 
$ Imagefileout = (PWD). Path + "\ adout.jpg"
 
 
 
######################################## ######################################
 
# Check to see if the argument for filename was provided, and that it exists ###
######################################## ######################################
 
If ([String]: isnullorempty ($ infile)-or-not (test-path $ imagefile )){
 
& Usage
 
}
 
 
 
 
 
###############################
 
# Remove any old converted file #
 
###############################
 
If (test-path $ imagefileout ){
 
Del-path $ imagefileout-erroraction "silentlycontinue"
 
}
 
 
 
$ Image = new-object-comobject WIA. imagefile
 
$ Imageprocessor = new-object-comobject WIA. imageprocess
 
 
 
 
######################################## ##################
 
# Try loading the file, if its not an image this will fail #
 
######################################## ##################
 
$ Image. LoadFile ($ imagefile)
 
 
 
If (-not $ ?) {& Usage}
 
 
 
 
 
######################################## #####################
 
# Create filters, set Aspect Ratio setting, change dimensions #
 
# To Max 96 pixels, convert to jpg and set quality #
 
######################################## #####################
 
$ Scale = $ imageprocessor. filterinfos. Item ("Scale"). Filterid
$ Imageprocessor. Filters. Add ($ scale)
 
$ Qual = $ imageprocessor. filterinfos. Item ("Convert"). Filterid
 
$ Imageprocessor. Filters. Add ($ qual)
 
 
 
If ([String]: isnullorempty ($ aspect)-or [String] $ aspect-ne "1"){
 
$ Imageprocessor. Filters. Item (1). properties. Item ("preserveaspectratio") = $ False
 
} Else {
 
$ Imageprocessor. Filters. Item (1). properties. Item ("preserveaspectratio") = $ True
 
}
 
 
 
$ Imageprocessor. Filters. Item (1). properties. Item ("maximumheight") = 96
$ Imageprocessor. Filters. Item (1). properties. Item ("maximumwidth") = 96
 
$ Imageprocessor. Filters. Item (2). properties. Item ("formatid") ="{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
 
 
 
######################################## ############################
 
# Drop image quality until it meets the size recommendation of 10kb #
 
######################################## ############################
 
$ Quality = 80
 
Do {
 
Remove-item-path $ imagefileout-erroraction "silentlycontinue"
 
$ Imageprocessor. Filters. Item (2). properties. Item ("Quality") = $ Quality
$ Image = $ imageprocessor. Apply ($ image)
 
$ Image. SaveFile ($ imagefileout)
 
[Byte [] $ imagedata = Get-content $ imagefileout-encoding byte
 
$ Quality-= 10
 
} While ($ imagedata. Length-GT 10kb)
 
 
 
 
 
######################################## #############################
 
# Find domain, and account distinguished name. Open user object, add #
 
# Thumbnailphoto data and save.
 
######################################## #############################
 
$ De = new-object directoryservices. directoryentry ("LDAP ://"+ $ ENV: logonserver. substring (2 ))
$ DS = new-object directoryservices. directorysearcher ($ de)
 
$ Ds. Filter = "(& (objectclass = user) (samaccountname ="+ $ ENV: username +"))"
 
$ Myaccount = $ Ds. findone ()
 
$ De = new-object directoryservices. directoryentry ($ myaccount. Path)
 
$ De. properties ["thumbnailphoto"]. Clear ()
 
$ De. properties ["thumbnailphoto"]. Add ($ imagedata) | out-Null
 
$ De. setinfo ()
 
Write-host "photo has been uploaded"

2. Modify VBScript

The username in my ad is Michael, and the photo is Michael.

In AD, the user name is Michael, and in AD properties, the value of the corresponding name field is name, which prevails. This is because the property of the script search is also the name property.

The photo is very simple. The user name is the file name.

ConstForreading = 1
 
 
 
'Directory of Image Storage
 
Indir ="C: \ photo" 
 
SetFSO = Createobject ("Scripting. FileSystemObject")
 
SetOiads = GetObject ("LDAP: // rootdse")
 
Strdefaultnc = oiads.Get("Defaultnamingcontext")
 
SetTheconn = Createobject ("ADODB. Connection")
 
Theconn. provider ="Adsdsoobject" 
 
Theconn. Open"Ads provider" 
SetThecmd = Createobject ("ADODB. Command")
 
Thecmd. activeconnection = theconn
 
SetObjrecordset = Createobject ("ADODB. recordset")
 
For EachTfileInFSO. getfolder (indir). Files
 
Tname = tfile. Name
 
Tname = left (tname, Rev (tname,".")-1)
 
Strquery ="<LDAP ://"& Strdefanc NC &"> ;"&"(& (Objectclass = person) (name ="& Tname &"); Name, adspath; subtree" 
 
Thecmd. commandtext = strquery
SetObjrs = thecmd. Execute
 
IfObjrs. recordcount = 0Then 
 
Msgbox"Can't find account"& Tname
 
Else 
 
SetObjuser = GetObject (objrs ("Adspath"))
 
Objuser. Put"Thumbnailphoto", Readbytearray (tfile. Path)
 
Objuser. setinfo
 
End If 
 
Next 
 
 
 
 
 
FunctionReadbytearray (strfilename)
ConstAdtypebinary = 1
 
DimBin
 
SetBin = Createobject ("ADODB. Stream")
 
Bin. type = adtypebinary
 
Bin. Open
 
Bin. loadfromfile strfilename
 
Readbytearray = bin. Read
 
End Function

3. C #CodeModify

 
Directoryentry Container =NewDirectoryentry (ldap_uri + users_dir );
 
Directoryentry user = container. Children. Add ("Cn ="+ Username,"User");
 
 
// Set other property's of the user object:
 
/// User. properties ["XXX"]. value = "yyy ";
 
////...
 
 
 
Byte[] Buffer;
 
Filestream =NewFilestream (@ "C: \ photo.jpg", Filemode. Open, fileaccess. Read );
 
 
 
Try{
 
IntLength = (Int) Filestream. length;// Get file length
Buffer =New Byte[Length];// Create buffer
 
IntCount;// Actual number of bytes read
 
IntSum = 0;// Total number of bytes read
 
 
 
// Read until read method returns 0 (end of the stream has been reached)
 
While(COUNT = filestream. Read (buffer, sum, length-sum)> 0)
 
Sum + = count;// Sum is a buffer offset for next reading
 
}
 
 
Finally{
 
Filestream. Close ();
 
}
 
 
 
User. properties ["Thumbnailphoto"]. Value = buffer;
 
 
 
User. commitchanges ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.