Advertisement
JorgTheElder

thumbnailPhoto.psm1

Nov 7th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #WARNING: This module is unsupported and my corrupt your AD user object.
  2. #Jorgie
  3.  
  4. <#
  5.   .Synopsis
  6.   Gets thumnailPhoto from and AD user object and saves it to the specified file
  7.  
  8.   .Parameter sAMAccountName
  9.   The user objects sAMAccountName
  10.  
  11.   .Parameter OutputJpegFile
  12.   The file to save the image (if any) to.
  13.  
  14.   .Parameter Server
  15.   The AD server to talk to, if the user is not in the same domain as the local machine, a DC in the users domain must be provided.
  16.  
  17.   .Example
  18.     Get-ADUsersThumbnailPhoto -sAMAccountName 'jorgie' -OutputJpegFile './jorgie.jpg'
  19.  
  20.   .Example
  21.     Get-ADUsersThumbnailPhoto -sAMAccountName 'jorgie' -OutputJpegFile './jorgie.jpg' -Server 'col.missouri.edu'
  22. #>
  23.  
  24. function Get-ADUsersThumbnailPhoto {
  25.   param (
  26.     [Parameter(Mandatory=$true)]
  27.     [string]
  28.     $sAMAccountName,
  29.     [Parameter(Mandatory=$true)]
  30.     [string]
  31.     $OutputJpegFile,
  32.     [Parameter(Mandatory=$false)]
  33.     [string]
  34.     $Server=$nul
  35.   )
  36.   Begin {
  37.     if(!(Get-Module ActiveDirectory)) {
  38.       Import-Module ActiveDirectory;
  39.       if(!(Get-Module ActiveDirectory)) {
  40.         throw 'Could not import ActiveDirectory module';
  41.       }
  42.     }
  43.   }
  44.  
  45.   Process {
  46.     if($Server -eq $null -or $Server.Length -lt 1) {
  47.       $user = Get-ADUser -LDAPFilter "(sAMAccountName=$sAMAccountName)" -Properties thumbnailPhoto;
  48.     } else {
  49.       $user = Get-ADUser -LDAPFilter "(sAMAccountName=$sAMAccountName)" -Properties thumbnailPhoto -Server $Server;
  50.     }
  51.  
  52.     if($user -eq $null) { throw 'Did not find a user.'; }
  53.  
  54.     if($user.thumbnailPhoto -eq $null) {
  55.       throw 'That user does not seem to have a thumbnilPhoto.';
  56.     }
  57.    
  58.     Set-Content -Encoding Byte -Path $OutputJpegFile -Value $user.thumbnailPhoto;
  59.   }
  60.  
  61.   End {}
  62. }
  63.  
  64.  
  65. #test function
  66. #Get-ADUsersThumbnailPhoto -sAMAccountName 'jorgie' -OutputJpegFile './jorgie.jpg'
  67.  
  68.  
  69. <#
  70.   .Synopsis
  71.   Puts a jpg file into the thumbnailPhoto fields of an AD user object.
  72.  
  73.   .Parameter sAMAccountName
  74.   The user objects sAMAccountName
  75.  
  76.   .Parameter InputJpegFile
  77.   The file to get the image from. The image should be 128x128 or smaller and less than 10Kb in size.
  78.  
  79.   .Parameter Server
  80.   The AD server to talk to, if the user is not in the same domain as the local machine, a DC in the users domain must be provided.
  81.  
  82.   .Example
  83.     Put-ADUsersThumbnailPhoto -sAMAccountName 'jorgie' -InputJpegFile './jorgie.jpg'
  84.  
  85.   .Example
  86.     Put-ADUsersThumbnailPhoto -sAMAccountName 'jorgie' -InputJpegFile './jorgie.jpg' -Server 'my.domain.com'
  87. #>
  88.  
  89. function Set-ADUsersThumbnailPhoto {
  90.   param (
  91.     [Parameter(Mandatory=$true)]
  92.     [string]
  93.     $sAMAccountName,
  94.     [Parameter(Mandatory=$true)]
  95.     [string]
  96.     $InputJpegFile,
  97.     [Parameter(Mandatory=$false)]
  98.     [string]
  99.     $Server=$nul
  100.   )
  101.   Begin {
  102.     if(!(Get-Module ActiveDirectory)) {
  103.       Import-Module ActiveDirectory;
  104.       if(!(Get-Module ActiveDirectory)) {
  105.         throw 'Could not import ActiveDirectory module';
  106.       }
  107.     }
  108.   }
  109.   Process {
  110.     if($Server -eq $null -or $Server.Length -lt 1) {
  111.       $user = Get-ADUser -LDAPFilter "(sAMAccountName=$sAMAccountName)" -Properties thumbnailPhoto;
  112.     } else {
  113.       $user = Get-ADUser -LDAPFilter "(sAMAccountName=$sAMAccountName)" -Properties thumbnailPhoto -Server $Server;
  114.     }
  115.  
  116.     if($user -eq $null) { throw 'Did not find a user.'; }
  117.  
  118.     [Byte[]]$imgBytes = Get-Content -Encoding Byte -Path $InputJpegFile;
  119.     if($imgBytes.Length -gt 10240) { throw 'This script does not support images over 10Kb, the provided image was: ' + $jpgItem.Length + ' bytes.'; }
  120.  
  121.     if($Server -eq $null -or $Server.Length -lt 1) {
  122.       Set-ADUser $user -Replace @{thumbnailPhoto=$imgBytes};
  123.     } else {
  124.       Set-ADUser $user -Replace @{thumbnailPhoto=$imgBytes} -server $Server;
  125.     }
  126.   }
  127.   End {}
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement