PowerShell Function: Remove-ADPhoto

Since we now have a function to add a photo to a User Account object in Active Directory and one to show the photo, anybody care to guess what the Remove-ADPhoto function does?

If you didn’t answer something along the lines of “removes the photo from the User Account object in Active Directory”, you’re in the wrong place.

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Remove-ADPhoto
    ##  PURPOSE........:  Removes the picture stored in an Active Directory
    ##                    account.
    ##  REQUIREMENTS...:  PowerShell v2.0, Windows Server 2008 or newer Active
    ##                    Directory.
    ##  NOTES..........:
    ##--------------------------------------------------------------------------
    Function Remove-ADPhoto {
        <#
        .SYNOPSIS
         Removes the picture stored in an Active Directory account.
        .DESCRIPTION
         Clears the thumbnailPhoto attribute of the specified user's Active
         directory account
        .PARAMETER UserName
         The User logon name for the account the picture will be removed from.
         Alias: -un

        .EXAMPLE
         C:\PS>Remove-ADPhoto user1

         This example will clear the thumbnailPhoto attribute in Active
         Directory for the user account with the User logon name of "user1".

        .NOTES
         NAME......:  Remove-ADPhoto
         AUTHOR....:  Joe Glessner
         LAST EDIT.:  29NOV11
         CREATED...:  28NOV11
        .LINK
         https://joeit.wordpress.com/
        #>

        [CmdletBinding()]
            Param (
                [Parameter(Mandatory=$True,
                    #ValueFromPipeline=$True,
                    #ValueFromPipelineByPropertyName=$True,
                    Position=0)]
                [Alias('un')]
                [String]$UserName
            )#End: Param

        ##----------------------------------------------------------------------
        ##  Search AD for the user, set the path to the user account object.
        ##----------------------------------------------------------------------
        $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
        $Searcher.Filter = "(&(ObjectClass=User)(SAMAccountName= $UserName))"
        $FoundUser = $Searcher.findOne()
        $P = $FoundUser | Select path
        Write-Verbose "Retrieving LDAP path for user $UserName ..."
        If ($FoundUser -ne $null) {
            Write-Verbose $P.Path
        }#END: If ($FoundUser -ne $null)
        Else {
            Write-Warning "User $UserName not found in this domain!"
            Write-Warning "Aborting..."
            Break;
        }#END: Else
        $User = [adsi]$P.path

        ##----------------------------------------------------------------------
        ##  Attach the picture to the AD account object (remove it if the
        ##  -remove switch is set).
        ##----------------------------------------------------------------------
        Write-Verbose "Removing photo from user account $UserName ..."
        $User.Properties["thumbnailPhoto"].Clear()
        #$User.Properties["jpegPhoto"].Clear()
        $User.CommitChanges()
        Write-Verbose "Attribute cleared!"
    }#END: Function Remove-ADPhoto

 

Advertisements

PowerShell Function: Show-ADPhoto

Now that we can add photos to User Account objects in Active Directory, we might also like to view them. Which is where the Show-ADPhoto function comes into play:

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Show-ADPhoto
    ##  PURPOSE........:  Shows the photo stored in in an Active Directory User
    ##                    Account.
    ##  REQUIREMENTS...:  PowerShell v2.0, Windows Server 2008 or newer Active
    ##                    Directory.
    ##  NOTES..........:
    ##--------------------------------------------------------------------------
    Function Show-ADPhoto {
        <#
        .SYNOPSIS
         Shows the photo stored in in an Active Directory User Account.
        .DESCRIPTION
         Reads the ThumbnailPhoto attribute of the specified user's Active
         Directory account, and displays the returned photo in a form window.
        .PARAMETER UserName
         The User logon name of the Active Directory user to query.

        .EXAMPLE
         C:\PS> Show-ADPhoto user1

         Displays the photo stored in the Active Directory user account with
         the User logon name of "user1".

        .NOTES
         NAME......: Show-ADPhoto
         AUTHOR....: Joe Glessner
         LAST EDIT.: 28NOV11
         CREATED...: 28NOV11
        .LINK
         https://joeit.wordpress.com/
        #>
        [CmdletBinding()]
            Param (
                [Parameter(Mandatory=$True,
                    #ValueFromPipeline=$True,
                    #ValueFromPipelineByPropertyName=$True,
                    Position=0)]
                [Alias('un')]
                [String]$UserName
            )#End: Param

        ##----------------------------------------------------------------------
        ##  Search AD for the user, set the path to the user account object.
        ##----------------------------------------------------------------------
        $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
        $Searcher.Filter = "(&(ObjectClass=User)(SAMAccountName= $UserName))"
        $FoundUser = $Searcher.findOne()
        $P = $FoundUser | Select path
        Write-Verbose "Retrieving LDAP path for user $UserName ..."
        If ($FoundUser -ne $null) {
            Write-Verbose $P.Path
        }#END: If ($FoundUser -ne $null)
        Else {
            Write-Warning "User $UserName not found in this domain!"
            Write-Warning "Aborting..."
            Break;
        }#END: Else
        $User = [ADSI]$P.path

        ##----------------------------------------------------------------------
        ##  Build a form to display the image
        ##----------------------------------------------------------------------
        $Img = $User.Properties["thumbnailPhoto"].Value
        #$Img = $User.Properties["jpegPhoto"].Value
        [VOID][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        $Form = New-Object Windows.Forms.Form
        $Form.Text = "Image stored in AD for $UserName"
        $Form.AutoSize = "True"
        $Form.AutoSizeMode = "GrowAndShrink"
        $PictureBox = New-Object Windows.Forms.PictureBox
        $PictureBox.SizeMode = "AutoSize"
        $PictureBox.Image = $Img
        $Form.Controls.Add($PictureBox)
        $Form.Add_Shown({$Form.Activate()})
        $Form.ShowDialog()
    }#END: Function Show-ADPhoto

 

%d bloggers like this: