Author Posts

April 3, 2015 at 2:06 am

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))"}

April 3, 2015 at 3:43 am

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

April 3, 2015 at 4:41 am

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"

April 3, 2015 at 8:30 am

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