How to Use Powershell to Mail Merge Outlook Signature for Users out of Active Directory

Script 1

#Get Active Directory information for current user

$UserName = $env:username

$Filter = “(&(objectCategory=User)(samAccountName=$UserName))”

$Searcher = New-Object System.DirectoryServices.DirectorySearcher

$Searcher.Filter = $Filter

$ADUserPath = $Searcher.FindOne()

$ADUser = $ADUserPath.GetDirectoryEntry()

$ADDisplayName = $ADUser.name

$ADTitle = $ADUser.title

$ADOffice = $ADUser.physicalDeliveryOfficeName

$script:ADMobileNumber = $script:ADUser.mobile

$ADTelePhoneNumber = $ADUser.telephoneNumber

$ADExtension1 = $ADUser.extensionAttribute1

$ADExtension2 = $ADUser.extensionAttribute2

$ADExtension3 = $ADUser.extensionAttribute3

 

#Additional Variables

$AppData=(Get-Item env:appdata).value

$SigPath = ‘\Microsoft\Signatures’

$LocalSignaturePath = $AppData+$SigPath

$SignatureName = '%signaturename%'

$DomainName = '%domainname%'

$fulladdetails = $ADDisplayName+$ADExtension1+$ADTitle+$ADOffice+$script:ADMobileNumber+$ADTelePhoneNumber

 

#Check if signature directory exists and, if not, update it

If (Test-Path $LocalSignaturePath)

{}

Else

{New-Item $LocalSignaturePath -type directory}

 

Write-host $fulladdetails

 

#Check if  Signature has changed

If ("$fulladdetails" -eq "$SigChkDetails")

{ Exit }

Else

{  }

 

#Delete old signature files

Remove-Item "$LocalSignaturePath\$ADDisplayName.htm" -Recurse -Force

 

#Copy over signature template

$SigSource = “\\path\to\signature\source"

$filename = "\\path\to\signature\template.htm"

$filename2 = "\\path\to\logo.jpg"

 

Copy-Item $filename $LocalSignaturePath -Recurse -Force

Copy-Item $filename2 $LocalSignaturePath -Recurse -Force
 

#Modify Signature and Insert Variables

(Get-Content $LocalSignaturePath\template.htm) -replace 'FullName', $ADDisplayName | Set-Content $LocalSignaturePath\template.htm

(Get-Content $LocalSignaturePath\template.htm) -replace 'PositionTitle', $ADTitle | Set-Content $LocalSignaturePath\template.htm

(Get-Content $LocalSignaturePath\template.htm) -replace 'PhoneNumber', $ADTelePhoneNumber | Set-Content $LocalSignaturePath\template.htm

 

If(!$script:ADMobileNumber -or !$ADExtension2){

(Get-Content $LocalSignaturePath\template.htm) -replace '<b>M</b> MobileNumber', $NULL | Set-Content $LocalSignaturePath\template.htm}

ELSE

{(Get-Content $LocalSignaturePath\template.htm) -replace 'MobileNumber', $script:ADMobileNumber | Set-Content $LocalSignaturePath\template.htm}

 

If(!$ADExtension1){

(Get-Content $LocalSignaturePath\template.htm) -replace ', Qualification', $NULL | Set-Content $LocalSignaturePath\template.htm}

ELSE

{(Get-Content $LocalSignaturePath\template.htm) -replace 'Qualification', $ADExtension1 | Set-Content $LocalSignaturePath\template.htm}

 

If($ADOffice -ne 'Singapore'){

If(!$ADExtension3){

(Get-Content $LocalSignaturePath\template.htm) -replace 'ImageRow', '<img src="./logo.jpg" width="259" height="74" border="0" />' | Set-Content $LocalSignaturePath\template.htm}

}ELSE

{(Get-Content $LocalSignaturePath\template.htm) -replace 'ImageRow', $null | Set-Content $LocalSignaturePath\template.htm}



 

Rename-Item -Path $LocalSignaturePath\template.htm -NewName "$ADDisplayName.htm"

 

#Set company signature as default for New messages

[Void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word")

$MSWord = New-Object -com word.application

$EmailOptions = $MSWord.EmailOptions

$EmailSignature = $EmailOptions.EmailSignature

$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries

$EmailSignature.NewMessageSignature=$ADDisplayName

$MSWord.Quit()

 

#Set company signature as default for Reply messages

[Void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word")

$MSWord = New-Object -com word.application

$EmailOptions = $MSWord.EmailOptions

$EmailSignature = $EmailOptions.EmailSignature

$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries

$EmailSignature.ReplyMessageSignature=$ADDisplayName

$MSWord.Quit() 

Script 2

<#
.SYNOPSIS
Sets your email signature according to company policy

.DESCRIPTION
This script is designed to be run as a logon script, and it will create the proper files for the Outlook
email signature in Outlook 2010/2013/2016. It executes an AD query, populates the proper fields, then pumps
data into a word document which is saved as a rtf, txt, and htm. It then forcefully sets the registry settings
so users cant change it.
Requires:
.INPUTS
.OUTPUTS
%appdata%\Microsoft\Signatures\v2.txt
%appdata%\Microsoft\Signatures\v2.htm
%appdata%\Microsoft\Signatures\v2.rtf
%appdata%\Microsoft\Signatures\v2_files
.NOTES
  Version:        1.0
  Author:         [email protected]
  Creation Date:  11/11/2017
  Purpose/Change: 
.EXAMPLE
.ChangeLog
#>

function Create-Log
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strComputerName
    )

    $strLogLocation = Get-LogLocation -strComputerName $strComputerName
    if ($(test-path $strLogLocation) -eq $false)
    {
        New-Item -Path $strLogLocation -ItemType File | Out-Null
    }
}

function Get-LogLocation
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strComputerName
    )
    if ($(test-path $($env:appdata + "\Email-SignatureScript")) -eq $false)
    {
        New-Item -Path $($env:appdata + "\Email-SignatureScript") -ItemType directory | Out-Null
    }

    $strLogLocation = $env:appdata + "\Email-SignatureScript\" + $strComputerName + ".txt"
    Return $strLogLocation
}

function Log-This
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strErrorMessage
    )

    ac -path $strLogLocation -Value "$strErrorMessage on $(get-date)"
}

function Set-CompanyInfo
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    [string]$strADCompany
    )
    try
    {
        $objCompanyDetails = New-Object System.Object
        $objHyperlink = "www.google.com", "https://www.Google.com"
        $strPicture = "$env:appdata\Microsoft\Stationery\google.png"
        $Signame = "google Signature v1"

        Switch -regex ($strADCompany)
        {
            "\b(Google)\b"
            {
             
                $objHyperlink = "www.google.com", "https://www.Google.com"
                $strPicture = "$env:appdata\Microsoft\Stationery\google.png"
                $Signame = "google Signature v1"
            }
            "\b(Apple)\b" 
            {
                $objHyperlink = "http://www.Apple.com", "www.Apple.com"
                $strPicture = "$env:appdata\Microsoft\Stationery\Apple.png"
                $Signame = "Apple Signature v1"
            }
            "\b(Amazon)\b"
            {
                $objHyperlink = "http://www.Amazon.com", "www.Amazon.com"
                $strPicture = "$env:appdata\Microsoft\Stationery\Amazon.png"
                $Signame = "Amazon Signature v1"
            }
            "\b(Microsoft)\b" 
            {
                $objHyperlink = "http://www.Microsoft.com", "www.Microsoft.com"
                $strPicture = "$env:appdata\Microsoft\Stationery\Microsoft.png"
                $Signame = "Microsoft Signature v1"
            }
            default {}
        }

        $objCompanyDetails | Add-Member -MemberType NoteProperty -Name "Hyperlink" -Value $objHyperlink
        $objCompanyDetails | Add-Member -MemberType NoteProperty -Name "Picture" -Value $strPicture
        $objCompanyDetails | Add-Member -MemberType NoteProperty -Name "Signame" -Value $Signame
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objCompanyDetails
}

function Set-CountryInfo
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    [string]$strCountry,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    [string]$strADCompany
    )
    try
    {
        $objCountryDetails = New-Object System.Object
        $intFontSize = $null
        $strLine1 = $null
        $strLine1 = $null

        Switch ($strCountry)
        {
            "United Kingdom"
            {
                if ($strADCompany -match "\b(google)\b" -or $strADCompany -match "\b(Google)\b")
                {
                    $intFontSize = 8
                    $strLine1 = "COMPANY NAME Registered in Scotland number #######. Registered office address: ############ $nl"
                    $strLine2 = "Company Name Is a subsidiary of Company Name"
                }
                elseif ($strADCompany -match "\b(Amazon\b")
                {
                    $intFontSize = 8
                    $strLine1 = "COMPANY NAME Registered in Scotland number #######. Registered office address: ############ $nl"
                    $strLine2 = "Company Name Is a subsidiary of Company Name"
                }
            }
            "Australia" 
            {
                $intFontSize = 8
                $strLine1 = "COMPANY NAME Registered in Australia. Registered office address: ADDRESS$nl"
                $strLine2 = "Company Name. Is a subsidiary of Company Name"
            }
            "Canada" 
            {
                $intFontSize = 8
                $strLine1 = "Registered in Canada. Registered office address: ADDRESS $nl"
                $strLine2 = "Company Name Is a subsidiary of Company Name."
            }
            default {}
        }

        $objCountryDetails | Add-Member -MemberType NoteProperty -Name "FontSize" -Value $intFontSize
        $objCountryDetails | Add-Member -MemberType NoteProperty -Name "Line1" -Value $strLine1
        $objCountryDetails | Add-Member -MemberType NoteProperty -Name "Line2" -Value $strLine2
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    if ( $intFontSize -eq $null)
    {
        $objCountryDetails = $null
    }
    Return $objCountryDetails
}

function Add-Name
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADDisplayName,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objNameStyle
    )

    try
    {
        $objSelection.ParagraphFormat.SpaceAfter = 0
        $objSelection.ParagraphFormat.SpaceBefore = 0
        if ($objNameStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objNameStyle.Size
        }
        if ($objNameStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objNameStyle.Bold
        }
        if ($objNameStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objNameStyle.Italic
        }
        if ($objNameStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objNameStyle.Color
        }
        if ($objNameStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objNameStyle.Name
        }
        $objSelection.TypeText($strADDisplayName)
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}

function Add-Title
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADTitle,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objTitleStyle
    )

     try
    {
        if ($objTitleStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objTitleStyle.Size
        }
        if ($objTitleStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objTitleStyle.Bold
        }
        if ($objTitleStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objTitleStyle.Italic
        }
        if ($objTitleStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objTitleStyle.Color
        }
        if ($objTitleStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objTitleStyle.Name
        }
        $objSelection.TypeText($strADTitle)
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}

function Add-Tel
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADTelePhoneNumber,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objTelStyle
    )

     try
    {
        if ($objTelStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objTelStyle.Size
        }
        if ($objTelStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objTelStyle.Bold
        }
        if ($objTelStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objTelStyle.Italic
        }
        if ($objTelStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objTelStyle.Color
        }
        if ($objTelStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objTelStyle.Name
        }
        $objSelection.TypeText("Tel: $strADTelePhoneNumber")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}
function Add-Tel2
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADTelePhoneNumber2,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objTelStyle
    )

     try
    {
        if ($objTelStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objTelStyle.Size
        }
        if ($objTelStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objTelStyle.Bold
        }
        if ($objTelStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objTelStyle.Italic
        }
        if ($objTelStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objTelStyle.Color
        }
        if ($objTelStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objTelStyle.Name
        }
        $objSelection.TypeText("Tel: $strADTelePhoneNumber")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}

function Add-Mobile
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADMobile,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objMobileStyle
    )

     try
    {
        if ($objMobileStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objMobileStyle.Size
        }
        if ($objMobileStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objMobileStyle.Bold
        }
        if ($objMobileStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objMobileStyle.Italic
        }
        if ($objMobileStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objMobileStyle.Color
        }
        if ($objMobileStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objMobileStyle.Name
        }
        $objSelection.TypeText("Mob: $strADMobile")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}
function Add-Mobile2
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADMobile2,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objMobileStyle
    )

     try
    {
        if ($objMobileStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objMobileStyle.Size
        }
        if ($objMobileStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objMobileStyle.Bold
        }
        if ($objMobileStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objMobileStyle.Italic
        }
        if ($objMobileStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objMobileStyle.Color
        }
        if ($objMobileStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objMobileStyle.Name
        }
        $objSelection.TypeText("Mob: $strADMobile2")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}

function Add-Fax
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADFax,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objFaxStyle
    )

     try
    {
        if ($objFaxStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objFaxStyle.Size
        }
        if ($objFaxStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objFaxStyle.Bold
        }
        if ($objFaxStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objFaxStyle.Italic
        }
        if ($objFaxStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objFaxStyle.Color
        }
        if ($objFaxStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objFaxStyle.Name
        }
        $objSelection.TypeText("Fax: $strADFax")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objSelection
}

function Add-CompanySpecific
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    $objDocument,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objCompanyInfo,

    [parameter(Mandatory=$true,
            Position=4)]
    [validatenotnullorempty()]
    [int]$intPSVersion,

    [parameter(Mandatory=$true,
            Position=5)]
    $objBanner,

    [parameter(Mandatory=$true,
            Position=5)]
    $bolBanner
    
    )
    try
    {
        ##$objLink = $objDocument.Hyperlinks.Add($objSelection,$objCompanyInfo.Hyperlink[1],$null,$null,$objCompanyInfo.Hyperlink[0])
        if ($intPSVersion -eq 2)
        {
            $objRange = $objSelection.Range
            $objDocument.Hyperlinks.Add($objRange, $objCompanyInfo.Hyperlink[1], $False, $False, $objCompanyInfo.Hyperlink[0]) 
            $objSelection.TypeParagraph()
            $objSelection.InlineShapes.AddPicture($objCompanyInfo.Picture)
            $objSelection.TypeParagraph()
            if ($bolBanner -eq $true)
            {
                ##Email Signature addition as per ticket #29851
                if ($objBanner.banners -ne $null -and $(get-date) -lt $objBanner.End_Date -and $(get-date) -gt $objBanner.Start_Date)
                {
                    $objSelection.InlineShapes.AddPicture($objBanner.File_Location)
                    $objSelection.TypeParagraph()
                }
            }
        }
        else
        {
            $objRange = $objSelection.Range()
            $objDocument.Hyperlinks.Add($objRange, $objCompanyInfo.Hyperlink[1], $False, $False, $objCompanyInfo.Hyperlink[0]) 
            $objSelection.TypeParagraph()
            $objSelection.InlineShapes.AddPicture($objCompanyInfo.Picture)
            $objSelection.TypeParagraph()
            ##Email Signature addition as per ticket #29851
            if ($bolBanner -eq $true)
            {
                if ($objBanner.banners -ne $null -and $(get-date) -lt $objBanner.End_Date -and $(get-date) -gt $objBanner.Start_Date)
                {
                    $objSelection.InlineShapes.AddPicture($objBanner.File_Location)
                    $objSelection.TypeParagraph()
                }
            }
        }
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$intPSVersion"
    }
    Return $objSelection
}

function Add-ARInfo
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    $objArStyle
    )
    try
    {
        if ($objArStyle.Size -ne $null)
        {
            $objSelection.Font.Size = $objArStyle.Size
        }
        if ($objArStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objArStyle.Bold
        }
        if ($objArStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objArStyle.Italic
        }
        if ($objArStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objArStyle.Color
        }
        if ($objArStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objArStyle.Name
        }
        $objSelection.TypeText("Please remit all payments to: ")
        $objSelection.TypeParagraph()
        $objSelection.TypeText("**Company Name**")
        $objSelection.TypeParagraph()
        $objSelection.TypeText("**Second Line**")
        $objSelection.TypeParagraph()
        $objSelection.TypeText("PO BOX ******")
        $objSelection.TypeParagraph()
        $objSelection.TypeText("City, State ZIP")
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }
    Return $objSelection
}

function Add-CountrySpecific
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objSelection,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    $objCountryStyle,

    [parameter(Mandatory=$true,
            Position=3)]
    [validatenotnullorempty()]
    $objCountryInfo
    )
    try
    {
        if ($objCountryStyle.Size -ne $null)
        {
            $objSelection.Font.Size = [Int]$objCountryStyle.Size
        }
        if ($objCountryStyle.Bold -ne $null)
        {
            $objSelection.Font.Bold = $objCountryStyle.Bold
        }
        if ($objCountryStyle.Italic -ne $null)
        {
            $objSelection.Font.Italic = $objCountryStyle.Italic
        }
        if ($objCountryStyle.Color -ne $null)
        {
            $objSelection.Font.Color = $objCountryStyle.Color
        }
        if ($objCountryStyle.Name -ne $null)
        {
            $objSelection.Font.Name = $objCountryStyle.Name
        }
        $objSelection.TypeText($objCountryInfo.Line1)
        $objSelection.TypeText($objCountryInfo.Line2)
        $objSelection.TypeParagraph()
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "Company Object: $errItemName - $_"
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$($objSelection.Font.Size)"
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$($objCountryStyle.Size)"
    }
    Return $objSelection
}

function Create-StyleObject
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $intFontSize,

    [parameter(Mandatory=$true,
            Position=2)]
    [AllowNull()]
    $intBold,

    [parameter(Mandatory=$true,
            Position=3)]
    [AllowNull()]
    $intItalic,

    [parameter(Mandatory=$true,
            Position=4)]
    [AllowNull()]
    $strColor,

    [parameter(Mandatory=$true,
            Position=5)]
    [AllowNull()]
    $strName
    )

    $objStyleInfo = New-Object System.Object
    try
    {
        $objStyleInfo | Add-Member -MemberType NoteProperty -Name "Size" -Value $intFontSize
        $objStyleInfo | Add-Member -MemberType NoteProperty -Name "Bold" -Value $intBold
        $objStyleInfo | Add-Member -MemberType NoteProperty -Name "Italic" -Value $intItalic
        $objStyleInfo | Add-Member -MemberType NoteProperty -Name "Color" -Value $strColor
        $objStyleInfo | Add-Member -MemberType NoteProperty -Name "Name" -Value $strName
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "Style Object: $errItemName - $_"
    }

    Return $objStyleInfo

}

function Remove-ComObject
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $comMSWord
    )

    try
    {
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$comMSWord) | Out-Null
    [gc]::Collect()
    [gc]::WaitForPendingFinalizers()
    Remove-Variable comMSWord
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

}

function Create-InfoObject
{
[cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    [string]$strADDisplayName,

    [parameter(
            Position=2)]
    [validatenotnullorempty()]
    [string]$strADTitle,

    [parameter(
            Position=3)]
    $strADTelePhoneNumber,

    [parameter(
            Position=4)]
    $strADTelePhoneNumber2,

    [parameter(
            Position=5)]
    $strADMobile,

    [parameter(
            Position=6)]
    $strADMobile2,

    [parameter(
            Position=7)]
    $strADCompany,

    [parameter(
            Position=8)]
    $strDepartment,

    [parameter(
            Position=9)]
    $strFax,

    [parameter(
            Position=10)]
    $strARCar,

    [parameter(
            Position=11)]
    $strCountry,

    [parameter(
            Position=12)]
    $strImage,

    [parameter(Mandatory=$true,
            Position=13)]
    [string]$strInfoLogLocation,

    [parameter(Mandatory=$true,
            Position=14)]
    [boolean]$Banner

    )

    $objInfo = New-Object System.Object
    try
    {
        $objInfo | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $strADDisplayName
        $objInfo | Add-Member -MemberType NoteProperty -Name "Title" -Value $strADTitle
        $objInfo | Add-Member -MemberType NoteProperty -Name "Tel1" -Value $strADTelePhoneNumber.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Tel2" -Value $strADTelePhoneNumber2.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Mob" -Value $strADMobile.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Mob2" -Value $strADMobile2.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Company" -Value $strADCompany.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Department" -Value $strDepartment.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Fax" -Value $strFax.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "AR" -Value $strARCar.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Country" -Value $strCountry.Value
        $objInfo | Add-Member -MemberType NoteProperty -Name "Image" -Value $strImage
        $objInfo | Add-Member -MemberType NoteProperty -Name "Banner" -Value $Banner
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }

    Return $objInfo
}

function Compare-Info
{
[cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objInfo,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strInfoLogLocation

    )

    $resetSig = $null
    $nl = [Environment]::NewLine
    try
    {
        if (!$(Test-Path $($env:appdata + "\Email-SignatureScript\data.csv")))
        {
            $objInfo | Export-Csv $($env:appdata + "\Email-SignatureScript\data.csv")
        }
        $objOldInfo = Import-Csv $($env:appdata + "\Email-SignatureScript\data.csv")

        $objOldInfo = Remove-NewLine -strLogLocation $strLogLocation -objOldInfo $objOldInfo
        If ($objOldInfo.DisplayName -ne $objInfo.DisplayName)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Name Change"
        }
        If ($objOldInfo.Title -ne $objInfo.Title)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Title Change"
        }
        If ($objOldInfo.Tel1 -ne $objInfo.Tel1)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Tel1 Change"
        }
        If ($objOldInfo.Tel2 -ne $objInfo.Tel2)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Tel2 Change"
        }
        If ($objOldInfo.Mob -ne $objInfo.Mob)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Mob Change"
        }
        If ($objOldInfo.Mob2 -ne $objInfo.Mob2)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Mob2 Change"
        }
        If ($objOldInfo.Company -ne $objInfo.Company)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Company Change"
        }
        If ($objOldInfo.Department -ne $objInfo.Department)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Department Change"
        }
        If ($objOldInfo.Fax -ne $objInfo.Fax)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Fax Change"
        }
        If ($objOldInfo.AR -ne $objInfo.AR)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "AR Change"
        }
        If ($objOldInfo.Country -ne $objInfo.Country)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Country Change"
        }
        If ($objOldInfo.Banner -ne $objInfo.Banner)
        {
            $resetSig = $true
            Log-This -strLogLocation $strLogLocation -strErrorMessage "Banner status change"
        }

        ##I convert to unix time since epoch ehre because compare dates wasn't working right
        if ($objOldInfo.Image -ne $null)
            {
            if ($objOldInfo.Image -like "*/*")
            {
                $resetSig = $true
                Log-This -strLogLocation $strLogLocation -strErrorMessage "Image? Change"
            }
            elseif ($objInfo.Image -gt $objOldInfo.Image)
            #If ($([Math]::Floor([decimal](Get-Date($dtOldImageLastWrite) -uformat "%s"))) -lt $([Math]::Floor([decimal](Get-Date($objInfo.Image) -uformat "%s"))))
            {
                $resetSig = $true
                Log-This -strLogLocation $strLogLocation -strErrorMessage "Image2? Change"
            }
        }
        else
        {
            $resetSig = $true
        }
    }
    catch
    {
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
        $resetSig = $false
    }
    Return $resetSig
}

function Reset-InfoLog
{
[cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objInfo,

    [parameter(Mandatory=$true,
            Position=2)]
    [validatenotnullorempty()]
    [string]$strInfoLogLocation

    )

    try
    {
        if ($(test-path $strInfoLogLocation) -eq $true)
        {
            Rename-Item $strInfoLogLocation -NewName $($env:appdata + "\Email-SignatureScript\$(Get-Date -UFormat "%Y_%m_%d_%M").csv")
        }
        $objInfo | Export-Csv $($env:appdata + "\Email-SignatureScript\data.csv")
    }
    catch
    {
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }
}

Function Get-Banners
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objGroups
    
    )
    $objBanners = New-Object System.Object
    ##Define Banner group and info
    try
    {
        foreach ($group in $objGroups)
        {
            ## This needs to be the Distinguished name of the AD group
            if ($group -eq "CN=,OU=,OU=,DC=,=com" -and $(get-date) -lt $(Get-Date -Month 09 -Day 7 -Hour 17) -and $(get-date) -gt $(Get-Date -Month 08 -Day 01 -hour 0))
            {
                $objBanners | Add-Member -MemberType NoteProperty -Name "Banners" -Value "Name" -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "Start_Date" -Value $(Get-Date -Month 08 -Day 01) -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "End_Date" -Value $(Get-Date -Month 09 -Day 7 -Hour 17) -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "File_Location" -Value "$env:appdata\Microsoft\Stationery\banner1.jpg" -Force
            }
            if ($group -eq "CN=,OU=,OU=,DC=,=com" -and $(get-date) -lt $(Get-Date -Month 11 -Day 15 -Hour 17) -and $(get-date) -gt $(Get-Date -Month 10 -Day 01 -hour 0))
            {
                $objBanners | Add-Member -MemberType NoteProperty -Name "Banners" -Value "Name2" -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "Start_Date" -Value $(Get-Date -Month 10 -Day 01) -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "End_Date" -Value $(Get-Date -Month 11 -Day 15 -Hour 17) -Force
                $objBanners | Add-Member -MemberType NoteProperty -Name "File_Location" -Value "$env:appdata\Microsoft\Stationery\banner2.jpg" -Force
            }
        }
    }
    catch
    {
        $errItemName = $_.Exception.ItemName
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }
    Return $objBanners
}

function Remove-NewLine
{
[cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strLogLocation,

    [parameter(Mandatory=$true,
            Position=1)]
    [validatenotnullorempty()]
    $objOldInfo
    )

    try
    {
        If ([string]::IsNullOrEmpty($objOldInfo.DisplayName))
        {
            $objOldInfo.DisplayName = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Title))
        {
            $objOldInfo.Title = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Tel1))
        {
            $objOldInfo.Tel1 = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Tel2))
        {
            $objOldInfo.Tel2 = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Mob))
        {
            $objOldInfo.Mob = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Mob2))
        {
            $objOldInfo.Mob2 = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Company))
        {
            $objOldInfo.Company = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Department))
        {
            $objOldInfo.Department = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Fax))
        {
            $objOldInfo.Fax = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.AR))
        {
            $objOldInfo.AR = $null
        }
        If ([string]::IsNullOrEmpty($objOldInfo.Country))
        {
            $objOldInfo.Country = $null
        }
    }
    catch
    {
        Log-This -strLogLocation $strLogLocation -strErrorMessage "$errItemName - $_"
    }
    Return $objOldInfo
}

function Set-RegistryKeys
{
    [cmdletbinding()]
    param(

    [parameter(Mandatory=$true,
            Position=0)]
    [validatenotnullorempty()]
    [string]$strSignatureName
    )

    #Office 2010 
    If (Test-Path HKCU:'\Software\Microsoft\Office\14.0')
    { 
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName -Force  | Out-Null
        }
        else
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'ReplySignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'ReplySignature' -Value $strSignatureName -Force  | Out-Null
        }
        else
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
    } 
 
    #Office 2013 signature 
 
    If (Test-Path HKCU:'\Software\Microsoft\Office\15.0')
    { 
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
        else
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName | Out-Null
        }
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -Value $strSignatureName -Force  | Out-Null
        }
        else
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
    }

    #Office 2016 signature 
    If (Test-Path HKCU:'\Software\Microsoft\Office\16.0')
    {
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'NewSignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName  -Force  | Out-Null
        }
        else
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'NewSignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
        if ($(Get-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'ReplySignature' -ErrorAction SilentlyContinue) -ne $strSignatureName)
        {
            Set-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'ReplySignature' -Value $strSignatureName -Force  | Out-Null
        }
        else
        {
            New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'ReplySignature' -Value $strSignatureName -PropertyType 'String' -Force  | Out-Null
        }
    }
}

##This will prevent the script from running if the c:\windows\NoAutoEmailSig.dat file exists or if it exists in the appdata folder
##The idea for this is we can create the file if there are issues and use the old script to buy us time to troubleshoot.
if ($(Test-Path C:\Windows\NoAutoEmailSig.dat) -eq $true -or $(Test-Path "$env:APPDATA\Email-SignatureScript\NoAutoEmailSig.dat") -eq $true)
{
    ##Populate the string values below with your stuff
    if ($(test-path $("\\SERVERNAME\$env:COMPUTERNAME.txt")) -eq $false -and $(Test-Connection -Count 1 -ComputerName "SERVERNAME" -ErrorAction SilentlyContinue))
    {
        New-Item -Path $("\\SERVERNAME\$env:COMPUTERNAME.txt") -ItemType file | Out-Null
    }
    exit
}
else
{
    if ($(test-path $("\\SERVERNAME\$env:COMPUTERNAME.txt")) -eq $true -and $(Test-Connection -Count 1 -ComputerName "Servername" -ErrorAction SilentlyContinue))
    {
        gci "\\SERVERNAME\$env:COMPUTERNAME.txt" | Remove-Item
    }
}


##I see no reason to run this on a server. I will do wmi fitlering on GPO also
if ($(Get-WmiObject -class Win32_OperatingSystem | select -expandProperty Caption | Out-String) -like "*Server*")
{
    exit
}

#Need connection to network for logging to work properly
if (!$(Test-Connection -Count 1 -ComputerName "SERVERNAME" -ErrorAction SilentlyContinue))
{
    exit
}

##When adding picture Powershell 2.0 requires $objDocument.Range vs 5.0 which needs $objDocument.Range()
$intPSVersion = $PSVersionTable.PSVersion.Major
if ($PSVersionTable.PSVersion.Major -eq 2)
{
    $intPSVersion = 2
}

##This will force the script to run in full if the file %appdata\Email-SignatureScript\ForceUpdate.dat exists
if ($(Test-Path "$env:APPDATA\Email-SignatureScript\ForceUpdate.dat") -eq $true)
{
    Remove-Item "$env:APPDATA\Email-SignatureScript\ForceUpdate.dat" -force
    Remove-Item "$env:APPDATA\Microsoft\Signatures" -Recurse -force
}

##New line character for country info
$nl = [System.Environment]::NewLine

Create-Log -strComputerName $env:COMPUTERNAME

##Set Log Path Variable
$strLogLocation = Get-LogLocation -strComputerName $env:COMPUTERNAME

Log-This -strLogLocation $strLogLocation -strErrorMessage "Script Trigggered"

#Environment variables
$strSignatureName = "Reddit Signature v1"
$strAppData=(Get-Item env:appdata).value 
$strSigPath = '\Microsoft\Signatures'
$strLocalSignaturePath = $strAppData+$strSigPath

##Create Sig Folder
If (!(Test-Path -path $strLocalSignaturePath))
{ 
    New-Item $strLocalSignaturePath -Type Directory | Out-Null
} 


##Get AD Info
$strUserName = $env:USERNAME
$strFilter = "(&(objectCategory=User)(samAccountName=$strUserName))" 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.Filter = $strFilter 
$objADUserPath = $objSearcher.FindOne()
if ($objADUserPath -ne $null)
{
    $objADUser = $objADUserPath.GetDirectoryEntry() 
    $strADDisplayName = $objADUser.DisplayName
    $strADTitle = $objADUser.title 
    $strADTelePhoneNumber = $objADUser.TelephoneNumber
    $strADTelePhoneNumber2 = $objADUser.extensionAttribute1 
    $strADMobile = $objADUser.mobile
    $strADMobile2 = $objADUser.extensionAttribute2
    $strADCompany = $objADUser.company
    $strDepartment = $objADUser.Department
    $strFax = $objADUser.facsimileTelephoneNumber
    $strARCar = $objADUser.extensionAttribute10
    $strCountry = $objADUser.co
    $objMemberOf = $objADUser.memberOf
}

#Get banner info and determine if one is needed
$objBanner = Get-Banners -strLogLocation $strLogLocation -objGroups $objMemberOf
if ($objBanner.banners -ne $null -and $(get-date) -lt $objBanner.End_Date -and $(get-date) -gt $objBanner.Start_Date)
{
    $bolBanner = $true
}
else
{
    $bolBanner = $false
}

##Get Company specific info
$objCompanyInfo = Set-CompanyInfo -strLogLocation $strLogLocation -strADCompany $strADCompany
$strSignatureName = $objCompanyInfo.Signame

##FOR TESTING ONLY
##$strCountry = "United Kingdom"
##$strARCar = "AR"

##Timestamp and unix timestamp for the logo (This is killing me...)
$dtLocalLogoTimeStamp = $(get-date $([datetime]$(gci $objCompanyInfo.Picture | select -ExpandProperty LastWriteTime)))
$strTimeSinceEpoch = $(New-TimeSpan -Start ('01/01/1970') -end $dtLocalLogoTimeStamp).Days

##Compare new data and previous data to determine if script needs to be run
$strInfoLogLocation = $($($env:appdata + "\Email-SignatureScript\data.csv"))
$objInfo = Create-InfoObject -strLogLocation $strLogLocation -strADDisplayName $strADDisplayName -strADTitle $strADTitle -strADTelePhoneNumber $strADTelePhoneNumber -strADTelePhoneNumber2 $strADTelePhoneNumber2 -strADMobile $strADMobile -strADMobile2 $strADMobile2 -strADCompany $strADCompany -strDepartment $strDepartment -strFax $strFax -strARCar $strARCar -strCountry $strCountry -strImage $strTimeSinceEpoch -strInfoLogLocation $strInfoLogLocation -Banner $bolBanner
$strSigReset = Compare-Info -strLogLocation $strLogLocation -objInfo $objInfo -strInfoLogLocation $strInfoLogLocation
if ($strSigReset)
{
    Reset-InfoLog -strLogLocation $strLogLocation -objInfo $objInfo -strInfoLogLocation $strInfoLogLocation
}
elseif ( $(gci -path $($strLocalSignaturePath + "\")) -eq $null)
{
    Reset-InfoLog -strLogLocation $strLogLocation -objInfo $objInfo -strInfoLogLocation $strInfoLogLocation
}
else
{
    Log-This -strLogLocation $strLogLocation -strErrorMessage "Completed - No need to run"
    Set-RegistryKeys -strSignatureName $strSignatureName | Out-Null
    break
}

##Get Country specific info
$objCountryInfo = Set-CountryInfo -strLogLocation $strLogLocation -strCountry $strCountry -strADCompany $strADCompany

##Create com object and set spacing
$arrWordBefore = Get-Process -Name "WINWORD" -ErrorAction SilentlyContinue
$comMSWord = New-Object -ComObject word.application
$arrWordAfter = Get-Process -Name "WINWORD"
if ($arrWordBefore -ne $null)
{
    $objWord = Compare-Object $arrWordBefore $arrWordAfter | select -ExpandProperty InputObject 
}
else
{
    $objWord = $arrWordAfter
}
$comMSWord.Visible = $false
$objDocument = $comMSWord.Documents.Add()
$objDocument.Paragraphs.SpaceAfter = 0
$objDocument.Paragraphs.SpaceAfter = 0
$objSelection = $comMSWord.Selection 

##Add Name to document
$objNameStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 1 -intItalic $null -strColor $null -strName "Calibri"
$objSelection = Add-Name -strLogLocation $strLogLocation -objSelection $objSelection -strADDisplayName $strADDisplayName -objNameStyle $objNameStyle
Remove-Variable objNameStyle

##Add Title to Document
$objTitleStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
$objSelection = Add-Title -strLogLocation $strLogLocation -objSelection $objSelection -strADTitle $strADTitle -objTitleStyle $objTitleStyle
Remove-Variable objTitleStyle

##Add Telephone if it exists
if ( $strADTelePhoneNumber -ne $null)
{
    $objtelStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-Tel -strLogLocation $strLogLocation -objSelection $objSelection -strADTelePhoneNumber $strADTelePhoneNumber -objTelStyle $objtelStyle
    Remove-Variable objtelStyle
}

##Add Telephone2 if it exists
if ( $strADTelePhoneNumber2 -ne $null)
{
    $objtelStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-Tel -strLogLocation $strLogLocation -objSelection $objSelection -strADTelePhoneNumber $strADTelePhoneNumber2 -objTelStyle $objtelStyle
    Remove-Variable objtelStyle
}

##Add Mobile if it exists
if ( $strADMobile -ne $null)
{
    $objMobileStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-Mobile -strLogLocation $strLogLocation -objSelection $objSelection -strADMobile $strADMobile -objMobileStyle $objMobileStyle
    Remove-Variable objMobileStyle
}

##Add Mobile2 if it exists
if ( $strADMobile2 -ne $null)
{
    $objMobileStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-Mobile2 -strLogLocation $strLogLocation -objSelection $objSelection -strADMobile $strADMobile2 -objMobileStyle $objMobileStyle
    Remove-Variable objMobileStyle
}

##Add Fax if it exists
if ( $strFax -ne $null)
{
    $objFaxStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 11 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-Fax -strLogLocation $strLogLocation -objSelection $objSelection -strADFax $strFax -objFaxStyle $objFaxStyle
    Remove-Variable objFaxStyle
}

##Add Company specific data
$objSelection = Add-CompanySpecific -strLogLocation $strLogLocation -objSelection $objSelection -objDocument $objDocument -objCompanyInfo $objCompanyInfo -intPSVersion $intPSVersion -objBanner $objBanner -bolBanner $bolBanner

##Add AR info if needed
if ( $strARCar -ne $null)
{
    $objARStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 8 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
    $objSelection = Add-ARInfo -strLogLocation $strLogLocation -objSelection $objSelection[2] -objArStyle $objARStyle
    Remove-Variable objARStyle
}

##Add country specific info if needed
if ( $objCountryInfo -ne $null)
{
    if ($strARCar -ne $null)
    {
        $objCountryStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 8 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
        $objSelection = Add-CountrySpecific -strLogLocation $strLogLocation -objSelection $objSelection -objCountryStyle $objCountryStyle -objCountryInfo $objCountryInfo
        Remove-Variable objCountryStyle
    }
    else
    {
        $objCountryStyle = Create-StyleObject -strLogLocation $strLogLocation -intFontSize 8 -intBold 0 -intItalic $null -strColor $null -strName "Calibri"
        $objSelection = Add-CountrySpecific -strLogLocation $strLogLocation -objSelection $objSelection[2] -objCountryStyle $objCountryStyle -objCountryInfo $objCountryInfo
        Remove-Variable objCountryStyle
    }
}

if ($strSigReset)
{
    $objSignatureFiles = gci $strLocalSignaturePath | select -ExpandProperty FullName
    foreach ($strFile in $objSignatureFiles)
    {
        Remove-Item $strFile -force -Recurse
    }
}



#Save HTML
try
{
    $refSaveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML"); 
    $refPath = "$strLocalSignaturePath\$strSignatureName.htm"
    $comMSWord.ActiveDocument.SaveAs([ref][system.object]$refPath, [ref]$refSaveFormat)
}
catch
{
    Log-This -strLogLocation $strLogLocation -strErrorMessage $_
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refPath
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refSaveFormat
}
     
#Save RTF
try
{
    $refSaveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF"); 
    $refPath = "$strLocalSignaturePath\$strSignatureName.rtf"
    $comMSWord.ActiveDocument.SaveAs([ref][system.object]$refPath, [ref]$refSaveFormat)
}
catch
{
    Log-This -strLogLocation $strLogLocation -strErrorMessage $_
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refPath
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refSaveFormat
}
     
#Save TXT
try
{
    $refSaveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatText"); 
    $refPath = "$strLocalSignaturePath\$strSignatureName.txt"
    $comMSWord.ActiveDocument.SaveAs([ref][system.object]$refPath, [ref]$refSaveFormat)
    $comMSWord.ActiveDocument.Close()
    $comMSWord.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($comMSWord)
}
catch
{
    Log-This -strLogLocation $strLogLocation -strErrorMessage $_
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refPath
    Log-This -strLogLocation $strLogLocation -strErrorMessage $refSaveFormat
}

Start-Sleep -Seconds 5

Set-RegistryKeys -strSignatureName $strSignatureName | Out-Null

Log-This -strLogLocation $strLogLocation -strErrorMessage "Completed - $(get-date)"
if ($(Get-Process -Id $objWord.ID -ErrorAction SilentlyContinue) -ne $null)
{
    Stop-Process $objWord -Force
}

Script 3

https://github.com/raymix/PowerShell-Outlook-Signatures

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...