HTML signature in EXOL for multiple users.

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 2 years, 3 months ago.

  • Author
    Posts
  • #23927
    Profile photo of Byron Grogan
    Byron Grogan
    Participant

    Just trying to understand what is happening in the below cmdlet which was given to me by an engineer from Microsoft. slightly convoluted.
    As far as my understanding, it gets all mailboxes of type 'UserMailbox' and foreach file in path "C:\signatures" + (this is where i get confused).

    Get-Mailbox -ResultSize unlimited -Filter {RecipientTypeDetails -eq 'UserMailbox'} | foreach{ $file= "C:\signatures\" + ($_.displayname) + ".htm"; Set-MailboxMessageConfiguration -identity $_.displayname -SignatureHtml "$(Get-Content -Path $file -ReadCount 0))"}
  • #23931
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Hi Byron,

    I will attempt to explain what is happening and provide an alternative way to write the same (more script style).

    1. You are right this will return all user mailboxes.

    Get-Mailbox -ResultSize unlimited -Filter {RecipientTypeDetails -eq 'UserMailbox'}
    

    2. The Get-Mailbox cmdlet will return objects for each mailbox which have properties like DisplayName, etc. Each object gets piped to the next cmdlet in the pipeline using the pipeline character (|).

    3. foreach is the next cmdlet in your pipeline. foreach will receive one object at a time and process it in the script block provided { .. }. An automatic variable $_ is being made available during script block execution for you to access the object properties and methods.

    foreach {
    
      $_ # Automatic variable. See https://technet.microsoft.com/en-us/library/hh847768.aspx
    }
    

    4. A couple of things are happening inside the scriptblock.

    a. The DisplayName property of a mailbox will be accessed via the $_ (automatic variable) and embedded into the string being constructed. The full string will be assigned to a temporary variable $file.

    $file= "C:\signatures\" + ($_.displayname) + ".htm"
    

    b. You can see the Get-Content cmdlet at the end. This read the HTM file specified in $file variable from the disk and will assign it to the SignatureHtml parameter of Set-MailboxMessageConfiguration before its invocation.

    Set-MailboxMessageConfiguration -identity $_.displayname -SignatureHtml "$(Get-Content -Path $file -ReadCount 0))"
    

    Alternative version as script:

    $Mailboxes = Get-Mailbox -ResultSize unlimited -Filter { RecipientTypeDetails -eq 'UserMailbox' }
    
    foreach ($Mailbox in $Mailboxes) {
    
        $File = "C:\signatures\" + ($Mailbox.DisplayName) + ".htm"
        $FileContent = Get-Content -Path $File -ReadCount 0
        Set-MailboxMessageConfiguration -Identity $Mailbox.DisplayName -SignatureHtml $FileContent
    }
    

    I hope above makes sense.

    Regards
    Daniel

  • #23932
    Profile photo of Byron Grogan
    Byron Grogan
    Participant

    Daniel, I really appreciate your help! your script is more comprehensive! would I be right in saying that I must name all the signature files using the DisplayName property value of each mailbox, respectively?

    also is the line below basically adding the DisplayName property and file extension .htm to each file in the folder?

    $File = "C:\signatures\" + ($Mailbox.DisplayName) + ".htm"
  • #23935
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Hi Byron,

    The script expects HTM files which match the DisplayName property which will most likely be Firstname Lastname.

    PS C:\> Get-ChildItem C:\Signatures
    
    
        Directory: C:\Signatures
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----       03/04/2015     17:22             12 Byron Grogan.htm
    -a----       03/04/2015     17:22             12 Daniel Krebs.htm
    

    The oneliner you have received from the Microsoft engineer did not check if the HTM file actually exists. I have updated the script to include a simple check using If and Test-Path.

    $Mailboxes = Get-Mailbox -ResultSize unlimited -Filter { RecipientTypeDetails -eq 'UserMailbox' }
     
    foreach ($Mailbox in $Mailboxes) {
     
        $File = "C:\signatures\" + ($Mailbox.DisplayName) + ".htm"
        if (Test-Path -Path $File) {
    
            $FileContent = Get-Content -Path $File -ReadCount 0
            Set-MailboxMessageConfiguration -Identity $Mailbox.DisplayName -SignatureHtml $FileContent
        }
    }
    

    I hope above answers your questions.

    Regards
    Daniel

You must be logged in to reply to this topic.