Powershell help

Soldato
Joined
5 Jul 2003
Posts
16,206
Location
Atlanta, USA
Good afternoon everyone,
Im after a bit of help with a script im trying to get working.
The script in itself is designed to create an Outlook signatures based on a users AD properties. Its based upon the template MS have provided.


The script itself, i have working how i want it to as an end result, however ive got a few issues/problems that i could do with some input on.

-- Unless I enable registry editing, the script wont run in its entirety. If I enable just silent editing in a GPO, the sig will create but not go as default - how do i get around this?
-- If I've run the script before with a sig name of X unless I change the name to Y it wont work again - is there a way to get it to force overwriting any variables/files/regkeys that are already there.

My first thought to resolve the first point was to use a variant of 'runas' however wouldn't that then return the AD properties for the 'runas' user rather than the user whos logging on?
& as a final point assuming the first two get resolved, is there a way to change the output of the powershell script so that it doesn't show a mountain of code, just a few steps saying "doing XYZ", "now doing ABC", ect; ?


Thanks in advance all and i appreciate any help you can give me with this.

Code:
#Custom variables 
$CompanyName = ‘COMPANY_NAME’ 
$DomainName = ‘COMPANY_DOMAIN’  
$SigSource = “UNC_TO_SOURCE” 

#Settings - Customisable
$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 = ’1' 
#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 
#Active Directory Variables
$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
$ADWebPage = $ADUser.wWWHomePage
 
#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() 
$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 = “MobileNumber” 
$ReplaceText = $ADMobile.ToString() 
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,    $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,    $Format, $ReplaceText, $ReplaceAll    )

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

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

$FindText = “FaxNumber” 
$ReplaceText = $ADFax.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]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “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]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatRTF”); 
$path = $LocalSignaturePath+’\'+$CompanyName+”.rtf” 
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat) 
 
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “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}
 
That's actually genuis now ive thought about it! :)
Just need to find an exception that reads the body of the mail.

##EDIT##
Nope, no exceptions for the body of the mail in there. :(
 
I think ive managed to get it working by bypassing EMC and using cmdlets to set some HT rules.
Bizzarely, it wont 'filter' based on a header insert, but will on body content...
 
Back
Top Bottom