impact/Set-ImpactEmailSignature.ps1

function Set-ImpactEmailSignature {

<#
.SYNOPSIS
    This function sets an Outlook email signature for the current Impact Partnership user.
 
.DESCRIPTION
    This function sets an Outlook email signature for the current Impact Partnership user
    using profile information from Microsoft Graph. Optionally, an Event image can be
    included.
 
    Signatures are generated for both HTML and TXT and placed in the user's
    APPDATA\Microsoft\signatures folder. Registry keys are then generated and placed into the
    user's Outlook profile keys.
 
    An Outlook profile and account must be configured before this function will work properly.
     
    No UI is presented for login for Microsoft Graph. Because of this, the function requires
    being ran on a device joined to Azure Active Directory.
    
.PARAMETER EventImageURL
    This parameter should be a URL to a 250x100 image to display beside the signature.
 
.PARAMETER logging
    This parameter enables logging to the current user's TEMP folder.
 
.EXAMPLE
    PS C:\> Set-ImpactEmailSignature
    Sets a standard email signature.
.EXAMPLE
    PS C:\> Set-ImpactEmailSignature -EventImageURL "http://PathToImage"
    Sets an email signature with an event image.
 
.NOTES
    Author: Cory Purcell
    Last Edit: 2019/02/01
    Version 1.0 - Initial Release
#>


    param(
        [Parameter(Mandatory=$false)]
        [string] $EventImageURL,
        [Parameter(Mandatory=$false)]
        [switch] $logging=$false
    )

    If ($logging) {
        $VerbosePreference="Continue"
        Start-Transcript "$env:temp\Set-ImpactEmailSignature.log" -Force 
    } 

    # Authentication using Get-AuthToken.
    # Azure AD Application Client ID for this function is: 0d6ab402-7f89-4666-af0c-fc2e55fcd7ad
    $User = whoami /upn
    $Password = $null
    $global:authToken = Get-AuthToken -User $User -clientID "0d6ab402-7f89-4666-af0c-fc2e55fcd7ad" -UIMode "Never"

    #Signature Generation
    $MyProfile = Invoke-RestMethod "https://graph.microsoft.com/beta/me/" -Method Get -Headers $AuthToken -UseBasicParsing
    $MyProfile
    
    if($MyProfile.mail -ge 0) {
        # Signature Attributes
        $signame = "ImpactSig"
        $logosrc = "http://impactpartner.com/signatures/signature-logo.png"
    
        $myDisplayName = $MyProfile.displayName
        $myTitle = $MyProfile.jobTitle
        $myEmail = $MyProfile.mail
        [int]$myExtension = $MyProfile.businessPhones[0]
        
        if ($myExtension -lt 2060) {
        $myDirect = "678-784-$myExtension"
        Write-Output "Extension within DID range. Setting direct number to: $myDirect" 
        }
    
        # If there is a DID, apply additional HTML or Plaintext.
        If (!($myDirect -eq $null)) { 
            $myDirectHTML = "<span style=`"color: #1885bf;`">Direct</span> $MyDirect |"
            $myDirectText = "$MyDirect |"
        }
    
        # If there was an Event image defined, apply additional HTML.
        If (!($EventImageURL -like "")) { 
            $eventHTML = "<td style=`"border-left: 1.7px solid #8f8f8f;padding-left:10px;`"><img src=`"$EventImageURL`" height=`"100`" width=`"250`"></td>"
        }

        # HTML Signature
        $sigHTMLContent = (
            "<table border=`"0`">
                <tr>
                    <td>
                        <img src=`"$logosrc`" style=`"height: 55px; width: 87px;`">
                    </td>
                    <td style=`"padding-right:5px;`">
                        <div style=`"font-family: Helvetica, Arial, serif; font-size: 14px; line-height: 20px; font-weight: 700; letter-spacing: .09em; color: #1885bf;`">$myDisplayName</div>
                        <div style=`"font-family: Georgia, serif; font-size: 12px; line-height: 20px; font-weight: 400; letter-spacing: .09em; font-style: italic; color: #8d8f92;`">$myTitle</div>
                        <div style=`"font-family: Helvetica, Arial, serif; font-size: 12px; line-height: 20px; font-weight: 400; letter-spacing: .09em; color: #8d8f92;`">$myDirectHTML 800-380-5040 | ImpactPartner.com</div>
                    </td>
                    $eventHTML
                </tr>
            </table>"

        )

        # Plaintext Signature
        $sigTextContent = (
            "$myDisplayName
            $myTitle
            $myDirectText 800-380-5040 | ImpactPartner.com
            "

        )

        # Signature Deployment
        $signatureDir = "$Env:appdata\Microsoft\signatures"  
        New-Item $signatureDir -ItemType Directory -force >$null
        Get-ChildItem $signatureDir | Remove-Item -Recurse -Force

        # Generate both HTML and Text
        $sigNameHTML = "$signatureDir\\$signame.htm"
        $sigNameText = "$signatureDir\\$signame.txt"
        $sigHTMLContent | out-file $sigNameHTML
        $sigTextContent | out-file $sigNameText

        # Place registry keys
        # Currently applies to Outlook 16. If signatures stop working, this is probably because we're on 17 now.
        if (test-path "HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General") {
            new-Itemproperty -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General -name Signatures -value signatures -propertytype string -force >$null
            new-Itemproperty -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings -name NewSignature -value $SigName -propertytype string -force >$null
            new-Itemproperty -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings -name ReplySignature -value $SigName -propertytype string -force >$null
            new-Itemproperty -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Outlook\Profiles\\Outlook\\9375CFF0413111d3B88A00104B2A6676\\00000002 -name "New Signature" -value $SigName -propertytype string -force >$null
            new-Itemproperty -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Outlook\Profiles\\Outlook\\9375CFF0413111d3B88A00104B2A6676\\00000002 -name "Reply-Forward Signature" -value $SigName -propertytype string -force >$null 
        }
    }
    If ($logging) {
        Stop-Transcript
    }
}