Advertisement
FSCorrupted

Create Signature with Information from AD

Apr 5th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Global Pramateters needed for Signature creation
  2. param
  3. (
  4.     [Parameter(Mandatory = $true,ValueFromPipeline = $True,ValueFromPipelinebyPropertyName = $True)] [string]$Sig1,
  5.     [Parameter(Mandatory = $true,ValueFromPipeline = $True,ValueFromPipelinebyPropertyName = $True)] [string]$Sig2,
  6.     [Parameter(Mandatory = $true,ValueFromPipeline = $True,ValueFromPipelinebyPropertyName = $True)] [string]$TemplatePath
  7. )
  8.  
  9. function Write-log {
  10.     [CmdletBinding()]
  11.  
  12.     param([Parameter(Position = 0)][ValidateNotNullOrEmpty()] [string]$Message,
  13.         [Parameter(Position = 1)] [string]$Logfile = $log
  14.     )
  15.  
  16.     Write-Output "$(Get-Date) $Message" | Out-File -FilePath $LogFile -Append
  17.     Write-Verbose $message
  18. }
  19. function Get-FileEncoding {
  20.   [CmdletBinding()]
  21.   Param (
  22.     [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
  23.     [string]$Path
  24.   )
  25.  
  26.   [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
  27.   #Write-Host Bytes: $byte[0] $byte[1] $byte[2] $byte[3]
  28.  
  29.   # EF BB BF (UTF8)
  30.   if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
  31.   { $Encoding = 'UTF8' }
  32.  
  33.   # FE FF  (UTF-16 Big-Endian)
  34.   elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
  35.   { $Encoding = 'Unicode UTF-16 Big-Endian' }
  36.  
  37.   # FF FE  (UTF-16 Little-Endian)
  38.   elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
  39.   { $Encoding = 'Unicode UTF-16 Little-Endian' }
  40.  
  41.   # 00 00 FE FF (UTF32 Big-Endian)
  42.   elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
  43.   { $Encoding = 'UTF32 Big-Endian' }
  44.  
  45.   # FE FF 00 00 (UTF32 Little-Endian)
  46.   elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff -and $byte[2] -eq 0 -and $byte[3] -eq 0)
  47.   { $Encoding = 'UTF32 Little-Endian' }
  48.  
  49.   # 2B 2F 76 (38 | 38 | 2B | 2F)
  50.   elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76 -and ($byte[3] -eq 0x38 -or $byte[3] -eq 0x39 -or $byte[3] -eq 0x2b -or $byte[3] -eq 0x2f) )
  51.   { $Encoding = 'UTF7'}
  52.  
  53.   # F7 64 4C (UTF-1)
  54.   elseif ( $byte[0] -eq 0xf7 -and $byte[1] -eq 0x64 -and $byte[2] -eq 0x4c )
  55.   { $Encoding = 'UTF-1' }
  56.  
  57.   # DD 73 66 73 (UTF-EBCDIC)
  58.   elseif ($byte[0] -eq 0xdd -and $byte[1] -eq 0x73 -and $byte[2] -eq 0x66 -and $byte[3] -eq 0x73)
  59.   { $Encoding = 'UTF-EBCDIC' }
  60.  
  61.   # 0E FE FF (SCSU)
  62.   elseif ( $byte[0] -eq 0x0e -and $byte[1] -eq 0xfe -and $byte[2] -eq 0xff )
  63.   { $Encoding = 'SCSU' }
  64.  
  65.   # FB EE 28  (BOCU-1)
  66.   elseif ( $byte[0] -eq 0xfb -and $byte[1] -eq 0xee -and $byte[2] -eq 0x28 )
  67.   { $Encoding = 'BOCU-1' }
  68.  
  69.   # 84 31 95 33 (GB-18030)
  70.   elseif ($byte[0] -eq 0x84 -and $byte[1] -eq 0x31 -and $byte[2] -eq 0x95 -and $byte[3] -eq 0x33)
  71.   { $Encoding = 'GB-18030' }
  72.  
  73.   else
  74.   { $Encoding = 'ASCII' }
  75.   return $Encoding
  76. }
  77. function Set-OutlookSig {
  78.     ##################
  79.     #Script Variables#
  80.     ##################
  81.     $UserName = $env:username
  82.     $LocalAppData = $env:LOCALAPPDATA
  83.     $Date = Get-Date
  84.     $log = "$LocalAppData\OutlookSig_$UserName.log"
  85.     Write-log "----------------------------------------------------------------------------------------------------------------------------------------"
  86.     Write-log "----------------------------------------------------------------------------------------------------------------------------------------"
  87.     Write-log "#################################################################################################"
  88.     Write-log "#######################################-Start of Signature Script-#######################################"
  89.     Write-log "#################################################################################################"
  90.     ##############################################################################################
  91.     #Get Office Version
  92.     $OfficeVersion = (Get-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Outlook.Application\CurVer). "(Default)".split('.')[2] + ".0"
  93.     #Get Name of Outlook Signature Folder
  94.     $OutlookSigDirName = (Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\$OfficeVersion\Common\General). "Signatures"
  95.     #Build Path for Outlook Signature
  96.     $OutlookSigLocalPath = "${env:appdata}\Microsoft\$OutlookSigDirName"
  97.  
  98.     ##############################################################################################
  99.  
  100.     #Cleanup old Signatures
  101.     if (!(Test-Path -Path $log)) {
  102.         Remove-Item $OutlookSigLocalPath\*.* -Force
  103.         Write-log "Signatures Folder cleaned" }
  104.  
  105.     #Remove Logfile if its to big
  106.     $logsize = if ((Get-Item $log).Length -gt 5mb) {
  107.         Remove-Item $log -Force -Confirm:$false
  108.         Write-log "Old Logfile Removed" }
  109.     ##############################################################################################
  110.  
  111.     #Write-log "##################################################################################"
  112.     Write-log "Office Version detected: $OfficeVersion"
  113.     Write-log "Outlook Signature Foldername detected: $OutlookSigDirName"
  114.     Write-log "Build Outlook Signature Path: $OutlookSigLocalPath"
  115.      
  116.     ##############################################################################################
  117.  
  118.     #Build Signature Content
  119.     $Sig1Content = "$TemplatePath" + "$Sig1" + ".htm"
  120.     $Sig2Content = "$TemplatePath" + "$Sig2" + ".htm"
  121.  
  122.     Write-log "##################################################################################"
  123.     Write-log "Signature Script Version: $Version"
  124.     Write-log "Create Signature started on: $Date"
  125.     Write-log "Create Signature: $Sig1"
  126.     Write-log "Create Signature: $Sig2"
  127.     Write-log "##################################################################################"
  128.  
  129.  
  130.     #AD Searcher Part
  131.     $Ldap = "dc=domain,dc=local"
  132.     $Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
  133.     $Searcher = [adsisearcher]$Filter
  134.     $searcher.SearchRoot = "LDAP://$Ldap"
  135.     $ADUserPath = $Searcher.FindOne()
  136.     $ADUser = $ADUserPath.GetDirectoryEntry()
  137.  
  138.     ###################################
  139.     #Get required AD User Informations#
  140.     ###################################
  141.  
  142.     #Build AD Displayname with Surname and Givenname
  143.     $ADDisplayName = $ADUser.givenName + $ADUser.sn
  144.      
  145.     #Get AD title if it is Present
  146.     $ADTitle = $ADUser.Title
  147.     if ($ADTitle) { $ADTitleValue = $ADTitle }
  148.  
  149.     #Get AD Department if it is present and split out '^' to a new Value
  150.     $ADDepartment = $ADUser.department
  151.     if ($ADDepartment) { $ADDepartmentValue = $ADDepartment }
  152.     if ($ADDepartment -like '*^*') { $ADDepartmentValue = $ADDepartmentValue.split('^')[-2]; $SecondDepartment = $ADDepartment.split('^')[-1] }
  153.  
  154.     #Get AD Mobile Number if it is Present
  155.     $ADMobile = $ADUser.Mobile
  156.     if ($ADMobile) { $ADMobileValue = "Mobile: " + "$ADMobile" }
  157.  
  158.     #Get AD FAX Number if it is Present
  159.     $ADFax = $ADUser.facsimileTelephoneNumber
  160.     if ($ADFax) { $ADFaxValue = "Fax: " + "$ADFax" }
  161.  
  162.     #Get AD Phone Number if it is Present
  163.     $AdPhone = $ADUser.telephoneNumber
  164.     if ($AdPhone) { $AdPhoneValue = "Tel.: " + "$AdPhone" }
  165.  
  166.     #Get AD E-Mail
  167.     $ADemail = $ADUser.mail
  168.  
  169.     #Get AD Description
  170.     $ADDescription = $ADUser.description
  171.  
  172.     #Write AD Search output to Logfile
  173.     Write-log "-------------------------------------------------------------------------------------------------------"
  174.     Write-log "Read User Informations from AD"
  175.     Write-log "-------------------------------------------------------------------------------------------------------"
  176.     Write-log "User Name: $ADDisplayName"
  177.     Write-log "User Title: $ADTitle"
  178.     Write-log "Department: $ADDepartment"
  179.     Write-log "Mobile Number: $ADMobile"
  180.     Write-log "Fax Number: $ADFax"
  181.     Write-log "Telephone Number: $AdPhone"
  182.     Write-log "Mail: $ADemail"
  183.     Write-log "Description: $ADDescription"
  184.  
  185.     ############################
  186.     #Create the Signature files#
  187.     ############################
  188.  
  189.     #Write Signature Path to Logfile
  190.     Write-log "-------------------------------------------------------------------------------------------------------"
  191.     Write-log "Signatures Folder:  $OutlookSigLocalPath"
  192.     Write-log "-------------------------------------------------------------------------------------------------------"
  193.  
  194.  
  195.     #Get Item Encoding
  196.     $EncodingSig1 = Get-FileEncoding $Sig1Content
  197.     $EncodingSig2 = Get-FileEncoding $Sig2Content
  198.  
  199.     Write-log "Get Template Encodings..."
  200.     Write-log "File Encoding - $Sig1 : $EncodingSig1"
  201.     Write-log "File Encoding - $Sig2 : $EncodingSig2"
  202.  
  203.     #Create the Signature Folder if its not present
  204.     if (!(Test-Path -Path $OutlookSigLocalPath)) { mkdir $OutlookSigLocalPath }
  205.  
  206.     #Get Signature Content and fill with Values from AD Search
  207.    
  208.     #############
  209.     #Signature 1#
  210.     #############
  211.     #Get Signature Content and fill with Values from AD Search
  212.     if($EncodingSig1 -eq 'ASCII'){
  213.     Invoke-Expression ('$Sig_1 = @"' + "`n" + (Get-Content -Path "$Sig1Content" | ForEach-Object { $_ + "`n" }) + "`n" + '"@')
  214.     Write-log "Get Signature Content..."
  215.     #Export Signature with Values to Soignature Path
  216.     $Sig_1 | Out-File "$OutlookSigLocalPath\$Sig1.htm" -Force -Confirm:$false
  217.     Write-log "Out finished Signature template File..."
  218.     }
  219.     Else { 
  220.     Invoke-Expression ('$Sig_1 = @"' + "`n" + (Get-Content -Encoding $EncodingSig1 -Path "$Sig1Content" | ForEach-Object { $_ + "`n" }) + "`n" + '"@')
  221.     Write-log "Get Signature Content..."
  222.     #Export Signature with Values to Soignature Path
  223.     $Sig_1 | Out-File -Encoding $EncodingSig1 "$OutlookSigLocalPath\$Sig1.htm" -Force -Confirm:$false
  224.     Write-log "Out finished Signature template File..."    
  225.     }
  226.  
  227.     #############
  228.     #Signature 2#
  229.     #############
  230.  
  231.     if($EncodingSig2 -eq 'ASCII'){    
  232.     Invoke-Expression ('$Sig_2 = @"' + "`n" + (Get-Content -Path "$Sig2Content" | ForEach-Object { $_ + "`n" }) + "`n" + '"@')
  233.     Write-log "Get Signature Content..."
  234.     #Export Signature with Values to Soignature Path
  235.     $Sig_2 | Out-File "$OutlookSigLocalPath\$Sig2.htm" -Force -Confirm:$false
  236.     Write-log "Out finished Signature template File..."        
  237.     }
  238.     Else {
  239.     Invoke-Expression ('$Sig_2 = @"' + "`n" + (Get-Content -Encoding $EncodingSig2 -Path "$Sig2Content" | ForEach-Object { $_ + "`n" }) + "`n" + '"@')
  240.     Write-log "Get Signature Content..."   
  241.     #Export Signature with Values to Soignature Path   
  242.     $Sig_2 | Out-File -Encoding $EncodingSig2 "$OutlookSigLocalPath\$Sig2.htm" -Force -Confirm:$false
  243.     Write-log "Out finished Signature template File..."        
  244.     }
  245.     #Write to Logfile which Signature was created
  246.     Write-log "Signature: $Sig1.htm created"
  247.     Write-log "Signature: $Sig2.htm created"
  248.     Write-log "##################################################################################################"
  249.     Write-log "########################################-End of Signature Script########################################"
  250.     Write-log "##################################################################################################"
  251. }
  252.  
  253. ##########################################
  254. ###############Script Version#############
  255. ##########################################
  256.  
  257. $Version = "1.4"
  258.  
  259. #Run Script with Parameters
  260. Set-OutlookSig -SigName $Sig1 -SigReplyName $Sig2 -TemplatePath $TemplatePath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement