PoshCode Archive  Artifact [390038f355]

Artifact 390038f35584d9bec3a8ec36dbb5342a638cea4d91d75a02bf8cdc80fb56ad6e:

  • File Get-ADThumbnail.ps1 — part of check-in [d091611844] at 2018-06-10 13:53:54 on branch trunk — Gets the picture stored in the thumbnailPhoto attribute for the specified user/users and saves it to the path specified by the Path parameter. Path should be a folder, not a file. (user: DollarUnderscore size: 3326)

# encoding: ascii
# api: powershell
# title: Get-ADThumbnail
# description: Gets the picture stored in the thumbnailPhoto attribute for the specified user/users and saves it to the path specified by the Path parameter. Path should be a folder, not a file.
# version: 0.1
# type: function
# author: DollarUnderscore
# license: CC0
# function: Get-ADThumbnail
# x-poshcode-id: 5357
# x-archived: 2015-03-24T01:06:51
# x-published: 2015-08-05T14:18:00
#
# Requires the ActiveDirectory-module to run. Import it manually if you are not running PoSh v.3 or newer.
# A short post about it is available at my blog:
# http://dollarunderscore.azurewebsites.net/?p=3221
#
function Get-ADThumbnail
{
    <#
    .SYNOPSIS
    Gets the picture stored in the thumbnailPhoto attribute for the specified user/users.

    .DESCRIPTION
    Gets the picture stored in the thumbnailPhoto attribute for the specified user/users and saves it to the path specified by the Path parameter. Path should be a folder, not a file.

    Requires the ActiveDirectory-module to run. Import it manually if you are not running PoSh v.3 or newer.

    .EXAMPLE
    Get-ADThumbnail -Identity MySamAccountName -Path C:\Temp\

    Downloads the picture of the user with SamAccountName 'MySamAccountName' and saves it in the folder C:\Temp\

    .EXAMPLE
    Get-ADUser -Filter "GivenName -eq 'John'" | Get-ADThumbnail -Path C:\Temp\

    Downloads the picture from all the users with the GivenName 'John' and saves the files to C:\Temp\

    .PARAMETER Identity
    Specify the SamAccountName, DistinguishedName, objectGUID or SID of the user(s) with the picture. Supports pipeline input.

    .PARAMETER Path
    Specify the folder were the picture(s) will be saved. The filename will be "SamAccountName.jpg"

    If omitted, this will default to the current path.

    #>

    [cmdletbinding()]
    param([Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
          [Alias('SamAccountName','DistinguishedName','ObjectGUID','SID')]
          $Identity,
          $Path = '.\')

    BEGIN { }

    PROCESS {
        
        if ((Test-Path $Path -PathType Leaf)) {
            Write-Error "Path should be a folder, not a file."
            return
        }

        foreach ($Ident in $Identity) {

            try {
                $ADUser = Get-ADUser -Identity $Ident -Properties Thumbnailphoto -ErrorAction Stop
            }
            catch {
                Write-Error "Failed to get the user $Ident from Active Directory."
                Continue
            }

            [byte[]] $PictureByteArray = $ADUser | select -ExpandProperty Thumbnailphoto

            if ($PictureByteArray -eq $null) {
                Write-Warning "No thumbnail found for user $Ident."
                Continue
            }

            $PictureFileContent = [System.Text.Encoding]::Default.GetString($PictureByteArray)

            $FileName = "$($ADUser.SamAccountName).jpg"

            $FullPath =  Join-Path $Path $FileName


            Set-Content -Path $FullPath -Value $PictureFileContent

            Remove-Variable PictureByteArray, PictureFileContent, Filename -ErrorAction SilentlyContinue
        }
    }

    END { }
}