Outlook Signature issue

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6043

    by jrmccown_4 at 2013-02-28 14:59:02

    I have the following script to import attributes from AD and place them into an Outlook Signature file. The Display Name needs to be in lower case and I cannot get that to work. What am i missing?

    ###########################################################################”
    #
    # NAME: ogsig.ps1
    #
    # AUTHOR: Dustin Barna
    #
    ###########################################################################”

    #Custom variables
    $CompanyName = ‘og’
    $DomainName = ‘newstarfresh.com’

    $SigSource = “\\$DomainName\netlogon\Signatures\$CompanyName”
    $ForceSignatureNew = ’1' #When the signature are forced the signature are enforced as default signature for new messages the next time the script runs. 0 = no force, 1 = force
    $ForceSignatureReplyForward = ’0' #When the signature are forced the signature are enforced as default signature for reply/forward messages the next time the script runs. 0 = no force, 1 = force

    #Environment variables
    $AppData=(Get-Item env:appdata).value
    $SigPath = ‘\Microsoft\Signatures’
    $LocalSignaturePath = $AppData+$SigPath
    $RemoteSignaturePathFull = $SigSource+’\'+$CompanyName+’.docx’

    #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.DisplayName
    $ADEmailAddress = $ADUser.mail
    $ADTitle = $ADUser.title
    $ADTelePhoneNumber = $ADUser.TelephoneNumber
    $ADFax = $ADUser.facsimiletelephonenumber
    $ADMobile = $ADUser.mobile
    $ADstreetAddress = $ADUser.streetAddress

    #Setting registry information for the current user
    $CompanyRegPath = “HKCU:\Software\”+$CompanyName

    if (Test-Path $CompanyRegPath)
    {}
    else
    {New-Item -path “HKCU:\Software” -name $CompanyName}

    if (Test-Path $CompanyRegPath’\Outlook Signature Settings’)
    {}
    else
    {New-Item -path $CompanyRegPath -name “Outlook Signature Settings”}

    $SigVersion = (gci $RemoteSignaturePathFull).LastWriteTime #When was the last time the signature was written
    $ForcedSignatureNew = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).ForcedSignatureNew
    $ForcedSignatureReplyForward = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).ForcedSignatureReplyForward
    $SignatureVersion = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).SignatureVersion
    Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name SignatureSourceFiles -Value $SigSource
    $SignatureSourceFiles = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).SignatureSourceFiles

    #Forcing signature for new messages if enabled
    if ($ForcedSignatureNew -eq ’1')
    {
    #Set company signature as default for New messages
    $MSWord = New-Object -com word.application
    $EmailOptions = $MSWord.EmailOptions
    $EmailSignature = $EmailOptions.EmailSignature
    $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
    $EmailSignature.NewMessageSignature=$CompanyName
    $MSWord.Quit()
    }

    #Forcing signature for reply/forward messages if enabled
    if ($ForcedSignatureReplyForward -eq ’1')
    {
    #Set company signature as default for Reply/Forward messages
    $MSWord = New-Object -com word.application
    $EmailOptions = $MSWord.EmailOptions
    $EmailSignature = $EmailOptions.EmailSignature
    $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
    $EmailSignature.ReplyMessageSignature=$CompanyName
    $MSWord.Quit()
    }

    #Copying signature sourcefiles and creating signature if signature-version are different from local version
    if ($SignatureVersion -eq $SigVersion){}
    else
    {
    #Copy signature templates from domain to local Signature-folder
    Copy-Item “$SignatureSourceFiles\*” $LocalSignaturePath -Recurse -Force

    $ReplaceAll = 2
    $FindContinue = 1
    $MatchCase = $False
    $MatchWholeWord = $True
    $MatchWildcards = $False
    $MatchSoundsLike = $False
    $MatchAllWordForms = $False
    $Forward = $True
    $Wrap = $FindContinue
    $Format = $False

    #Insert variables from Active Directory to rtf signature-file
    $MSWord = New-Object -com word.application
    $fullPath = $LocalSignaturePath+’\'+$CompanyName+’.docx’
    $MSWord.Documents.Open($fullPath)

    $FindText = “DisplayName”
    $ReplaceText = $ADDisplayName.ToString().ToLower()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “Title”
    $ReplaceText = $ADTitle.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “TelephoneNumber”
    $ReplaceText = $ADTelePhoneNumber.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “FacsimileNumber”
    $ReplaceText = $ADFax.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “streetAddress”
    $ReplaceText = $ADstreetAddress.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “CellNumber”
    $ReplaceText = $ADMobile.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $MSWord.Selection.Find.Execute(“Email”)

    $MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, “mailto:”+$ADEmailAddress.ToString(), $missing, $missing, $ADEmailAddress.ToString())

    $MSWord.ActiveDocument.Save()
    $saveFormat = [Enum]], “wdFormatHTML”);
    [ref]$BrowserLevel = “microsoft.office.interop.word.WdBrowserLevel” -as [type]

    $MSWord.ActiveDocument.WebOptions.OrganizeInFolder = $true
    $MSWord.ActiveDocument.WebOptions.UseLongFileNames = $true
    $MSWord.ActiveDocument.WebOptions.BrowserLevel = $BrowserLevel::wdBrowserLevelMicrosoftInternetExplorer6
    $path = $LocalSignaturePath+’\'+$CompanyName+”.htm”
    $MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)

    $saveFormat = [Enum]], “wdFormatRTF”);
    $path = $LocalSignaturePath+’\'+$CompanyName+”.rtf”
    $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)

    $saveFormat = [Enum]], “wdFormatText”);
    $path = $LocalSignaturePath+’\'+$CompanyName+”.rtf”
    $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)

    $path = $LocalSignaturePath+’\'+$CompanyName+”.txt”
    $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$SaveFormat::wdFormatText)
    $MSWord.ActiveDocument.Close()

    $MSWord.Quit()

    }

    #Stamp registry-values for Outlook Signature Settings if they doesn`t match the initial script variables. Note that these will apply after the second script run when changes are made in the “Custom variables”-section.
    if ($ForcedSignatureNew -eq $ForceSignatureNew){}
    else
    {Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name ForcedSignatureNew -Value $ForceSignatureNew}

    if ($ForcedSignatureReplyForward -eq $ForceSignatureReplyForward){}
    else
    {Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name ForcedSignatureReplyForward -Value $ForceSignatureReplyForward}

    if ($SignatureVersion -eq $SigVersion){}
    else
    {Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name SignatureVersion -Value $SigVersion}

    by DonJ at 2013-02-28 15:01:44

    Would you mind editing your post and adding the CODE or POWERSHELL tags? As-is, the script is about impossible to make out... there are toolbar buttons for the tags, if you need.

    Also... that's a LOT o' code for one question. Can you maybe trim it down to just the bit you're fighting with?

    by jrmccown_4 at 2013-02-28 16:06:37

    here you go:
    I cannot get the name field (weather i use DisplayName or givenName and sn) to be in lower case as desired. I am also haveing the same issue with streetAddress field.
    From what i understand the ToLower() tag should do the trick, but i must be missing something.
    Thanks in advance.

    $FindText = “givenName”
    $ReplaceText = $ADgivenName.ToString().ToLower()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “sn”
    $ReplaceText = $ADsn.ToString().ToLower()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “Title”
    $ReplaceText = $ADTitle.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “TelephoneNumber”
    $ReplaceText = $ADTelePhoneNumber.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “FacsimileNumber”
    $ReplaceText = $ADFax.ToString()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    $FindText = “streetAddress”
    $ReplaceText = $ADstreetAddress.ToString().ToLower()
    $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )

    by DonJ at 2013-02-28 16:13:43

    Ah, that's better. So, ToLower() is a method, and you're absolutely right, it should. But we need to see what it's up to. Try this modification:


    $ReplaceText = $ADgivenName.ToString().ToLower()
    Write-Verbose "Replacement text is $ReplaceText"

    And at the very tippy-top of your script, add $VerbosePreference='Continue'. Run it, and that'll show you what (if anything) ToLower() is generating. If the verbose response is all lowercase, than ToLower() is working, and the problem lies elsewhere. If the verbose response is not all lowercase, then we'll need to dig a little deeper. I'd want to see the result of


    $ADGivenName | Get-Member
    $ADGivenName.ToString()
    $ADGivenName.ToString() | Get-Member

    Sometimes the ToString() method doesn't do what you think. An alternative is


    ($ADGivenName | Out-String).ToLower()

    Which absolutely produces a string of some kind.

    by jrmccown_4 at 2013-02-28 16:43:00

    Verbose mode states that it is changing the name correctly. I need to run the verbose mode for my users because that is where the problem continues.
    Thanks for the help.

    by ps_gregg at 2013-02-28 21:37:44

    In your original code, you need to set the variable MatchCase to True

    $MatchCase = $True

    When it's set to false, Word will use it's built in auto-correct feature to correct the sentence that you are replacing. Setting it to true requires you to match your 'find' text with case-sensitivity (which I don't think will be a problem since you are using a docx as your source template), and it will force Word to retain the case-sensitivity of the new text ($ReplaceText) and ignore the auto-correct feature.

    Hope that helps
    -Gregg

    by DonJ at 2013-03-01 07:29:16

    That was SO gonna be my next guess.

    by jrmccown_4 at 2013-03-01 08:21:17

    Thank you for all the responses. I think that is going to work. I will know more after testing.
    The issue i am having now is that the script is not replacing existing signatures or making this one the default for both "new emails" and "replies/forwards".
    $SigSource = “\\$DomainName\netlogon\Signatures\$CompanyName”
    $ForceSignatureNew = ’1' #When the signature are forced the signature are enforced as default signature for new messages the next time the script runs. 0 = no force, 1 = force
    $ForceSignatureReplyForward = ’0' #When the signature are forced the signature are enforced as default signature for reply/forward messages the next time the script runs. 0 = no force, 1 = force

    $SigVersion = (gci $RemoteSignaturePathFull).LastWriteTime #When was the last time the signature was written
    $ForcedSignatureNew = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).ForcedSignatureNew
    $ForcedSignatureReplyForward = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).ForcedSignatureReplyForward
    $SignatureVersion = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).SignatureVersion
    Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name SignatureSourceFiles -Value $SigSource
    $SignatureSourceFiles = (Get-ItemProperty $CompanyRegPath’\Outlook Signature Settings’).SignatureSourceFiles

    #Forcing signature for new messages if enabled
    if ($ForcedSignatureNew -eq ’1')
    {
    #Set company signature as default for New messages
    $MSWord = New-Object -com word.application
    $EmailOptions = $MSWord.EmailOptions
    $EmailSignature = $EmailOptions.EmailSignature
    $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
    $EmailSignature.NewMessageSignature=$CompanyName
    $MSWord.Quit()
    }

    #Forcing signature for reply/forward messages if enabled
    if ($ForcedSignatureReplyForward -eq ’1')
    {
    #Set company signature as default for Reply/Forward messages
    $MSWord = New-Object -com word.application
    $EmailOptions = $MSWord.EmailOptions
    $EmailSignature = $EmailOptions.EmailSignature
    $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
    $EmailSignature.ReplyMessageSignature=$CompanyName
    $MSWord.Quit()
    }

    #Copying signature sourcefiles and creating signature if signature-version are different from local version
    if ($SignatureVersion -eq $SigVersion){}
    else
    {

    Any ideas on what i need to add or change?

    by DonJ at 2013-03-01 08:32:16

    You might want to cross-post this on our Exchange forum and see if someone who monitors that might know...

    by jrmccown_4 at 2013-03-01 09:16:06

    I was able to make this work by moving that part of the script to the end.
    I think we are in good shape.
    Thanks to everyone for the help

You must be logged in to reply to this topic.