Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Thumbnail-Gui-Update.ps1
- # updated 2015-05-08
- # Thanks to http://365lab.net/2014/04/21/powershell-gui-application-to-set-thumbnailphoto/
- # Thanks to whoever gave me the resize function :)
- #
- # V1 - Added Resize Function WARNING - Overwrites image
- # V2 - Added Get-Creds No Vaildation
- if ([threading.thread]::CurrentThread.ApartmentState.ToString() -eq 'MTA') {
- Write-Error "This script requires PowerShell to run in Single-Threaded Apartment. Please run powershell.exe with parameter -STA"
- Exit
- }
- # Import XAML GUI
- Add-Type -AssemblyName PresentationFramework
- [xml]$XAML = '
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Change AD Photo Thumbnail" Height="312" Width="525" ResizeMode="NoResize">
- <Grid x:Name="list1" Margin="0" Height="280">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="148*"/>
- <ColumnDefinition Width="86*"/>
- <ColumnDefinition Width="151*"/>
- <ColumnDefinition Width="134*"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="53*"/>
- <RowDefinition Height="142*"/>
- <RowDefinition Height="85"/>
- </Grid.RowDefinitions>
- <TextBox x:Name="txtSearch" Margin="10,15.04,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="363" Text="Search for user by SamAccountName Here.." Grid.ColumnSpan="3" Height="21.96"/>
- <Button x:Name="btnSearch" Content="Search" HorizontalAlignment="Right" VerticalAlignment="Top" Width="111" Margin="0,15.04,10,0" FontFamily="./#Segoe UI" Grid.Column="3" Height="21.96"/>
- <Label Content="Surname:" Height="26" Margin="3,40,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
- <Label x:Name="lblSn" Content="" Height="26" VerticalAlignment="Top" Margin="10,40,10,0" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="Black" BorderThickness="0"/>
- <Label Content="UsrPName:" Height="25" Margin="3,71,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
- <Label x:Name="lblUPN" Content="" Height="25" VerticalAlignment="Top" Margin="10,71,10,0" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="Black" BorderThickness="0"/>
- <Label Content="Firstname:" Height="26" Margin="3,9,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
- <Label x:Name="lblGivenName" Content="" Height="26" VerticalAlignment="Top" Margin="10,9,10,0" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="Black" BorderThickness="0"/>
- <Label Content="DName:" Height="25" Margin="3,101,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
- <Label x:Name="lblDN" Content="" Height="25" VerticalAlignment="Top" Margin="10,101,10,0" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" BorderBrush="Black" BorderThickness="0"/>
- <Border BorderBrush="Black" BorderThickness="1" Margin="10,9,10,16" Grid.Row="1">
- <Image x:Name="imgThumb" Margin="2" Grid.Row="1" OpacityMask="#FFF0F0F0"/>
- </Border>
- <Button x:Name="btnRemoveImg" Content="Remove photo" Height="32" Margin="17,12,19,0" VerticalAlignment="Top" IsEnabled="False" BorderThickness="1" Grid.Row="2"/>
- <Button x:Name="btnImg" Content="Select photo" Height="32" Margin="17,12,19,0" VerticalAlignment="Top" IsEnabled="False" BorderThickness="1" Grid.Row="2" Grid.Column="2"/>
- <Button x:Name="btnApply" Content="Apply" HorizontalAlignment="Right" Margin="0,12,10,0" Width="111" IsEnabled="False" Grid.Row="2" Grid.Column="3" Height="32" VerticalAlignment="Top"/>
- <StatusBar Height="30" Grid.Row="2" VerticalAlignment="Bottom" Grid.ColumnSpan="4" Background="#FFF0F0F0" Margin="0">
- <Label x:Name="lblStatus" Content="Ready.." Width="509" FontSize="10.667" VerticalAlignment="Top"/>
- </StatusBar>
- </Grid>
- </Window>
- '
- #
- # Resize to 100x100 max
- #
- function resize-image{
- Param ( [Parameter(Mandatory=$True)] [ValidateNotNull()] $imageSource,
- [Parameter(Mandatory=$True)] [ValidateNotNull()] $imageTarget,
- [Parameter(Mandatory=$true)][ValidateNotNull()] $quality )
- if (!(Test-Path $imageSource)){throw( "Cannot find the source image")}
- if(!([System.IO.Path]::IsPathRooted($imageSource))){throw("please enter a full path for your source path")}
- if(!([System.IO.Path]::IsPathRooted($imageTarget))){throw("please enter a full path for your target path")}
- if ($quality -lt 0 -or $quality -gt 100){throw( "quality must be between 0 and 100.")}
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- $bmp = [System.Drawing.Image]::FromFile($imageSource)
- #hardcoded canvas size...
- $canvasWidth = 100.0
- $canvasHeight = 100.0
- #Encoder parameter for image quality
- $myEncoder = [System.Drawing.Imaging.Encoder]::Quality
- $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
- $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, $quality)
- # get codec
- $myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|where {$_.MimeType -eq 'image/jpeg'}
- #compute the final ratio to use
- $ratioX = $canvasWidth / $bmp.Width;
- $ratioY = $canvasHeight / $bmp.Height;
- $ratio = $ratioY
- if($ratioX -le $ratioY){
- $ratio = $ratioX
- }
- #create resized bitmap
- $newWidth = [int] ($bmp.Width*$ratio)
- $newHeight = [int] ($bmp.Height*$ratio)
- $bmpResized = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
- $graph = [System.Drawing.Graphics]::FromImage($bmpResized)
- $graph.Clear([System.Drawing.Color]::White)
- $graph.DrawImage($bmp,0,0 , $newWidth, $newHeight)
- #save to file
- $bmpResized.Save($imageTarget,$myImageCodecInfo, $($encoderParams))
- $bmpResized.Dispose()
- $bmp.Dispose()
- }
- $global:userinfo = @{
- dn = $null
- photo = [byte[]]$null
- }
- function Invoke-FileBrowser {
- param([string]$Title,[string]$Directory,[string]$Filter="jpeg (*.jpg)|*.jpg")
- [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
- $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
- $FileBrowser.InitialDirectory = $Directory
- $FileBrowser.Filter = $Filter
- $FileBrowser.Title = $Title
- $Show = $FileBrowser.ShowDialog()
- If ($Show -eq "OK") { Return $FileBrowser.FileName }
- }
- function Get-LDAPUsers($sam) {
- # Msg
- $lblStatus.Content = "Searching..."
- # Go
- $activeDomain = New-Object DirectoryServices.DirectoryEntry
- $domain = $activeDomain.distinguishedName
- $searcher = [System.DirectoryServices.DirectorySearcher]"[ADSI]LDAP://$domain"
- $searcher.filter = "(&(samaccountname=$sam)(objectClass=user)(objectClass=person))"
- $result = $searcher.findall()
- # Interpret results
- if ($result.count -gt 1) {
- $lblStatus.Content = "More than one user found. Please refine your search."
- } elseif ($result.count -eq 1) {
- # Get info
- $result = $searcher.findone().getDirectoryEntry()
- [pscustomobject]@{
- UserPrincipalName= $result.userprincipalname[0]
- DistinguishedName= $result.distinguishedname[0]
- samAccountName = $result.samaccountname[0]
- Givenname = $result.givenname[0]
- Surname = $result.sn[0]
- ThumbnailPhoto = $result.thumbnailphoto
- }
- # Msg
- $lblStatus.Content = "Found user succesfully."
- } else {
- $lblStatus.Content = "No users found. Please refine your search."
- }
- }
- function Update-WPFForm($sam) {
- $selecteduser = Get-LDAPUsers $sam
- if ($selecteduser) {
- $global:userinfo.dn = $selecteduser.DistinguishedName
- $global:userinfo.photo = [byte[]]$($selecteduser.thumbnailphoto)
- $lblGivenName.Content = $selecteduser.GivenName
- $lblSN.Content = $selecteduser.SurName
- $lblUPN.Content = $selecteduser.UserPrincipalName
- $txtSearch.Text = $selecteduser.SamAccountName
- $imgThumb.Source = $global:userinfo.photo
- $btnImg.IsEnabled = $true
- $btnRemoveImg.IsEnabled = $true
- # Check and concat the DN
- $dn = $global:userinfo.dn
- if (($global:userinfo.dn).length -gt 38) {
- $dn = "$(($dn).substring(0,38)).."
- $lblDN.Content = $dn
- $lblDN.Tooltip = $global:userinfo.dn
- } else {
- $lblDN.Content = $dn
- }
- }
- }
- #region GUI
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- $window=[Windows.Markup.XamlReader]::Load($reader)
- $btnSearch = $window.FindName("btnSearch")
- $btnImg = $window.FindName("btnImg")
- $btnRemoveImg = $window.FindName("btnRemoveImg")
- $btnApply = $window.FindName("btnApply")
- $txtSearch = $window.FindName("txtSearch")
- $lblGivenName = $window.FindName("lblGivenName")
- $lblSn = $window.FindName("lblSn")
- $lblUPN = $window.FindName("lblUPN")
- $lblDN = $window.FindName("lblDN")
- $imgThumb = $window.FindName("imgThumb")
- $lblStatus = $window.FindName("lblStatus")
- #endregion GUI
- #region Events
- #region btnSearchClick
- $btnSearch_click = $btnSearch.add_Click
- $btnSearch_click.Invoke({
- Update-WPFForm($txtSearch.Text)
- })
- #endregion btnSearchClick
- #region btnImgClick
- $btnImg_click = $btnImg.add_Click
- $btnImg_click.Invoke({
- $file = Invoke-FileBrowser -Title "Choose a thumbnail photo" -Filter "Jpeg pictures (*.jpg)|*.jpg"
- if ($file) {
- if ((Get-Item $file).length -gt 10kB) {
- $lblStatus.Content = "resizing image"
- #
- # sorry about this, but wasnt sure how to force overwrite using resize-image funciton
- #
- $newName = $file +"_temp.jpg"
- REsize-Image $file $newName 95
- write-host resize $file
- move $newName $file -Force
- $global:userinfo.photo = [byte[]](Get-Content $file -encoding byte)
- $imgThumb.Source = $global:userinfo.photo
- $btnApply.IsEnabled = $true
- $lblStatus.Content = "resized and ready for update - click apply"
- } else {
- $global:userinfo.photo = [byte[]](Get-Content $file -encoding byte)
- $imgThumb.Source = $global:userinfo.photo
- $btnApply.IsEnabled = $true
- }
- }
- })
- #endregion btnImgClick
- #region btnRemoveImage
- $btnRemoveImg_click = $btnRemoveImg.add_Click
- $btnRemoveImg_click.Invoke({
- $username = $creds.username
- $password = $creds.GetNetworkCredential().password
- $user = New-Object DirectoryServices.DirectoryEntry("LDAP://$($global:userinfo.dn)",$username,$password)
- $user.putex(1,"thumbnailPhoto",0)
- $user.setinfo()
- $btnApply.IsEnabled = $false
- Write-Verbose "User $($global:userinfo.dn) updated - image removed."
- Update-WPFForm($txtSearch.Text)
- $lblStatus.Content = "Image Removed - search again or close"
- })
- #endregion btnRemoveImage
- #region btnApplyClick
- $btnApply_click = $btnApply.add_Click
- $btnApply_click.Invoke({
- $username = $creds.username
- $password = $creds.GetNetworkCredential().password
- $user = New-Object DirectoryServices.DirectoryEntry("LDAP://$($global:userinfo.dn)",$username,$password)
- $user.put("thumbnailPhoto", $global:userinfo.photo)
- $user.setinfo()
- $btnApply.IsEnabled = $false
- Write-Verbose "User $($global:userinfo.dn) updated."
- $lblStatus.Content = "Applied - search again or close"
- })
- #endregion btnApplyClick
- #region textChange
- $txtSearch_Change = $txtSearch.add_KeyDown
- $txtSearch_Change.Invoke({
- if ($_.Key -ieq 'Return') {
- Update-WPFForm($txtSearch.Text)
- }
- })
- #endregion textChange
- #region clearOnClick
- $txtSearch_click = $txtSearch.add_GotFocus
- $txtSearch_click.Invoke({
- $txtSearch.Clear()
- })
- #endregion
- #endregion Events
- $creds=Get-Credential # -Message "Enter Admin Password"
- $window.ShowDialog() | Out-Null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement