AndyMeFul

Set-ThumbnailPhotoGUI.ps1

May 8th, 2014
330
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if ([threading.thread]::CurrentThread.ApartmentState.ToString() -eq 'MTA') {
  2.     Write-Error "This script requires PowerShell to run in Single-Threaded Apartment. Please run powershell.exe with parameter -STA"
  3.     Exit
  4. }
  5.  
  6. # Import XAML GUI
  7. Add-Type -AssemblyName PresentationFramework
  8. [xml]$XAML = '
  9.    <Window
  10.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  11.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  12.        Title="Change AD Photo Thumbnail" Height="312" Width="525" ResizeMode="NoResize">
  13.        <Grid x:Name="list1" Margin="0" Height="280">
  14.             <Grid.ColumnDefinitions>
  15.                 <ColumnDefinition Width="148*"/>
  16.                 <ColumnDefinition Width="86*"/>
  17.                 <ColumnDefinition Width="151*"/>
  18.                 <ColumnDefinition Width="134*"/>
  19.             </Grid.ColumnDefinitions>
  20.             <Grid.RowDefinitions>
  21.                 <RowDefinition Height="53*"/>
  22.                 <RowDefinition Height="142*"/>
  23.                 <RowDefinition Height="85"/>
  24.             </Grid.RowDefinitions>
  25.            <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"/>
  26.            <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"/>
  27.            <Label Content="Surname:" Height="26" Margin="3,40,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
  28.            <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"/>
  29.            <Label Content="UsrPName:" Height="25" Margin="3,71,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
  30.            <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"/>
  31.            <Label Content="Firstname:" Height="26" Margin="3,9,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
  32.            <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"/>
  33.            <Border BorderBrush="Black" BorderThickness="1" Margin="10,9,10,16" Grid.Row="1">
  34.                 <Image x:Name="imgThumb" Margin="2" Grid.Row="1" OpacityMask="#FFF0F0F0"/>
  35.            </Border>
  36.             <Button x:Name="btnImg" Content="Change photo..." Height="32" Margin="16.666,12,19.334,0" VerticalAlignment="Top" IsEnabled="False" BorderThickness="1" Grid.Row="2"/>
  37.            <Label Content="DName:" Height="25" Margin="3,101,8,0" HorizontalContentAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" BorderBrush="Black" BorderThickness="0"/>
  38.            <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"/>
  39.            <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"/>
  40.            <StatusBar Height="30" Grid.Row="2" VerticalAlignment="Bottom" Grid.ColumnSpan="4" Background="#FFF0F0F0" Margin="0">
  41.                 <Label x:Name="lblStatus" Content="Ready.." Width="509" FontSize="10.667" VerticalAlignment="Top"/>
  42.            </StatusBar>
  43.        </Grid>
  44.    </Window>
  45. '
  46.  
  47. $global:userinfo = @{
  48.     dn = $null
  49.     photo = [byte[]]$null
  50. }
  51.  
  52. function Invoke-FileBrowser {
  53.     param([string]$Title,[string]$Directory,[string]$Filter="jpeg (*.jpg)|*.jpg")
  54.     [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  55.     $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
  56.     $FileBrowser.InitialDirectory = $Directory
  57.     $FileBrowser.Filter = $Filter
  58.     $FileBrowser.Title = $Title
  59.     $Show = $FileBrowser.ShowDialog()
  60.     If ($Show -eq "OK") { Return $FileBrowser.FileName }
  61. }
  62. function Get-LDAPUsers($sam) {
  63.     # Msg
  64.     $lblStatus.Content = "Searching..."
  65.  
  66.     # Go
  67.     $activeDomain = New-Object DirectoryServices.DirectoryEntry
  68.     $domain = $activeDomain.distinguishedName
  69.     $searcher = [System.DirectoryServices.DirectorySearcher]"[ADSI]LDAP://$domain"
  70.     $searcher.filter = "(&(samaccountname=$sam)(objectClass=user)(objectClass=person))"
  71.     $result = $searcher.findall()
  72.    
  73.     # Interpret results  
  74.     if ($result.count -gt 1) {
  75.         $lblStatus.Content = "More than one user found. Please refine your search."
  76.     } elseif ($result.count -eq 1) {
  77.         # Get info
  78.         $result = $searcher.findone().getDirectoryEntry()
  79.         [pscustomobject]@{
  80.             UserPrincipalName= $result.userprincipalname[0]
  81.             DistinguishedName= $result.distinguishedname[0]
  82.             samAccountName = $result.samaccountname[0]
  83.             Givenname = $result.givenname[0]
  84.             Surname = $result.sn[0]
  85.             ThumbnailPhoto = $result.thumbnailphoto
  86.         }
  87.  
  88.         # Msg
  89.         $lblStatus.Content = "Found user succesfully."
  90.     } else {
  91.         $lblStatus.Content = "No users found. Please refine your search."
  92.     }
  93. }
  94. function Update-WPFForm($sam) {
  95.    $selecteduser = Get-LDAPUsers $sam
  96.    if ($selecteduser) {
  97.         $global:userinfo.dn = $selecteduser.DistinguishedName
  98.         $global:userinfo.photo = [byte[]]$($selecteduser.thumbnailphoto)
  99.        
  100.         $lblGivenName.Content = $selecteduser.GivenName
  101.         $lblSN.Content = $selecteduser.SurName
  102.         $lblUPN.Content = $selecteduser.UserPrincipalName
  103.         $txtSearch.Text = $selecteduser.SamAccountName
  104.         $imgThumb.Source = $global:userinfo.photo
  105.         $btnImg.IsEnabled = $true
  106.  
  107.         # Check and concat the DN
  108.         $dn = $global:userinfo.dn
  109.         if (($global:userinfo.dn).length -gt 38) {
  110.             $dn = "$(($dn).substring(0,38)).."
  111.             $lblDN.Content = $dn
  112.             $lblDN.Tooltip = $global:userinfo.dn
  113.         } else {
  114.             $lblDN.Content = $dn
  115.         }
  116.     }
  117. }
  118.  
  119. #region GUI
  120.     $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  121.     $window=[Windows.Markup.XamlReader]::Load($reader)
  122.  
  123.     $btnSearch = $window.FindName("btnSearch")
  124.     $btnImg = $window.FindName("btnImg")
  125.     $btnApply = $window.FindName("btnApply")
  126.     $txtSearch = $window.FindName("txtSearch")
  127.     $lblGivenName = $window.FindName("lblGivenName")
  128.     $lblSn = $window.FindName("lblSn")
  129.     $lblUPN = $window.FindName("lblUPN")
  130.     $lblDN = $window.FindName("lblDN")
  131.     $imgThumb = $window.FindName("imgThumb")
  132.     $lblStatus = $window.FindName("lblStatus")
  133. #endregion GUI
  134.  
  135. #region Events
  136.     #region btnSearchClick
  137.         $btnSearch_click = $btnSearch.add_Click
  138.         $btnSearch_click.Invoke({
  139.             Update-WPFForm($txtSearch.Text)
  140.         })
  141.     #endregion btnSearchClick
  142.     #region btnImgClick
  143.         $btnImg_click = $btnImg.add_Click
  144.         $btnImg_click.Invoke({
  145.             $file = Invoke-FileBrowser -Title "Choose a thumbnail photo" -Filter "Jpeg pictures (*.jpg)|*.jpg"
  146.             if ($file) {
  147.                 if ((Get-Item $file).length -gt 10kB) {
  148.                     $lblStatus.Content = "Picture too large. Maximum size is 10 kB."
  149.                 } else {
  150.                     $global:userinfo.photo = [byte[]](Get-Content $file -encoding byte)
  151.                     $imgThumb.Source = $global:userinfo.photo
  152.                     $btnApply.IsEnabled = $true
  153.                 }
  154.             }
  155.         })
  156.     #endregion btnImgClick
  157.     #region btnApplyClick
  158.         $btnApply_click = $btnApply.add_Click
  159.         $btnApply_click.Invoke({
  160.             $user = New-Object DirectoryServices.DirectoryEntry "LDAP://$($global:userinfo.dn)"
  161.             $user.put("thumbnailPhoto", $global:userinfo.photo)
  162.             $user.setinfo()
  163.             $btnApply.IsEnabled = $false
  164.             Write-Verbose "User $($global:userinfo.dn) updated."
  165.         })
  166.     #endregion btnApplyClick
  167.     #region textChange
  168.         $txtSearch_Change = $txtSearch.add_KeyDown
  169.         $txtSearch_Change.Invoke({
  170.             if ($_.Key -ieq 'Return') {
  171.                 Update-WPFForm($txtSearch.Text)
  172.             }
  173.         })
  174.     #endregion textChange
  175.     #region clearOnClick
  176.         $txtSearch_click = $txtSearch.add_GotFocus
  177.         $txtSearch_click.Invoke({
  178.             $txtSearch.Clear()
  179.         })
  180.     #endregion
  181. #endregion Events
  182.  
  183. $window.ShowDialog() | Out-Null
RAW Paste Data