код скрипта
Код:
#Custom variables
$CompanyName = ‘company'
$SigSource = "\\Domain.local\SysVol\Domain.local\Policies\{7A1CC634-AB6E-4254-B254-92480ADDAC89}\User\Scripts\Logon\$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 (1 -установить по умолчанию, 0 - не устанавливать
$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
$LocalSignatureFile = $LocalSignaturePath+'\'+$CompanyName+'.files'
$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()
$ADCName = $ADUser.cn
$ADName = $ADUser.givenName
$ADSurname = $ADUser.sn
$ADEmailAddress = $ADUser.mail
$ADTitle = $ADUser.title
$ADTelePhoneNumber = $ADUser.TelephoneNumber
$ADMobilePhoneNumber = $ADUser.mobile
$ADFaxNumber = $ADUser.facsimileTelephoneNumber
$ADCompany = $ADUser.company
$ADWWW = $ADUser.wWWHomePage
$ADDepartment = $ADUser.department
$ADIPPhone = $ADUser.ipPhone
#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
#присваиваем переменные взяв значения из реестра
$TelephoneNumberREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').TelephoneNumber
$NameREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').Name
$SurnameREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').Surname
$EmailAddressREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').EmailAddress
$ADTitleREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').ADTitle
$ADMobilePhoneNumberREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').MobilePhone
$ADCompanyREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').Company
$ADwwwREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').WWW
$ADFaxNumberREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').Fax
$ADDepartmentREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').Department
$ADCNameREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').CName
$ADIPPhoneREG = (Get-ItemProperty $CompanyRegPath'\Outlook Signature Settings').IPPhone
$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
echo >> 1
$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
$FileExists = Test-Path "$LocalSignatureFile"
if ($SignatureVersion -eq $SigVersion -And $ADCNameREG -eq $ADCName -And $ADMobilePhoneNumberREG -eq $ADMobilePhoneNumber -And $ADCompanyREG -eq $ADCompany -and $ADwwwREG -eq $ADWWW -And $ADFaxNumberREG -eq $ADFaxNumber -And $ADDepartmentREG -eq $ADDepartment -And $TelephoneNumberREG -eq $ADTelePhoneNumber -And $NameREG -eq $ADName -And $SurnameREG -eq $ADSurname -And $EmailAddressREG -eq $ADEmailAddress -And $ADIPPhoneREG -eq $ADIPPhone -And $ADTitleREG -eq $ADTitle -and $FileExists -eq $true) {}
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 = "CName"
$ReplaceText = $ADCName.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = "FaxNumber"
$ReplaceText = $ADFaxNumber.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 = "MobPhone"
$ReplaceText = $ADMobilePhoneNumber.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = "Department"
$ReplaceText = $ADDepartment.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = "Name"
$ReplaceText = $ADName.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = "Surname"
$ReplaceText = $ADSurname.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = "Companyname"
$ReplaceText = $ADCompany.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 = "IPPhone"
$ReplaceText = $ADIPPhone.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.Selection.Find.Execute("site")
$MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, $ADWWW.ToString(), $missing, $missing, $ADWWW.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}
#проверить актуальность тел. номера
if ($TelephoneNumberREG -eq $ADTelePhoneNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name TelephoneNumber -Value $ADTelePhoneNumber}
if ($TelephoneNumberREG -eq $ADTelePhoneNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name TelephoneNumber -Value $ADTelePhoneNumber}
#проверить актуальность ФИО
if ($ADСNameREG -eq $ADСName){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name CName -Value $ADCName}
if ($ADCNameREG -eq $ADCName){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name CName -Value $ADCName}
#проверить актуальность Имени
if ($NameREG -eq $ADName){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Name -Value $ADName}
if ($NameREG -eq $ADName){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Name -Value $ADName}
#проверить актуальность e-mail
if ($EmailAddressREG -eq $ADEmailAddress){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name EmailAddress -Value $ADEmailAddress}
if ($EmailAddressREG -eq $ADEmailAddress){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name EmailAddress -Value $ADEmailAddress}
#проверить актуальность должности
if ($ADTitleREG -eq $ADTitle){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name ADTitle -Value $ADTitle}
if ($ADTitleREG -eq $ADTitle){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name ADTitle -Value $ADTitle}
#проверить актуальность мобильного
if ($ADMobilePhoneNumberREG -eq $ADMobilePhoneNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name MobilePhone -Value $ADMobilePhoneNumber}
if ($ADMobilePhoneNumberREG -eq $ADMobilePhoneNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name MobilePhone -Value $ADMobilePhoneNumber}
#проверить актуальность компании
if ($ADCompanyREG -eq $ADCompany){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Company -Value $ADCompany}
if ($ADCompanyREG -eq $ADCompany){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Company -Value $ADCompany}
#проверить актуальность www
if ($ADwwwREG -eq $ADWWW){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name WWW -Value $ADWWW}
if ($ADwwwREG -eq $ADWWW){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name WWW -Value $ADWWW}
#проверить актуальность skype
if ($ADIPPhoneREG -eq $ADIPPhone){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name IPPhone -Value $ADIPPhone}
if ($ADIPPhoneREG -eq $ADIPPhone){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name IPPhone -Value $ADIPPhone}
#проверить актуальность факса
if ($ADFaxNumberREG -eq $ADFaxNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Fax -Value $ADFaxNumber}
if ($ADFaxNumberREG -eq $ADFaxNumber){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Fax -Value $ADFaxNumber}
#проверить актуальность отдела
if ($ADDepartmentREG -eq $ADDepartment){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Department -Value $ADDepartment}
if ($ADDepartmentREG -eq $ADDepartment){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Department -Value $ADDepartment}
#проверить актуальность фамлиии
if ($SurnameREG -eq $ADSurname){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Surname -Value $ADSurname}
if ($SurnameREG -eq $ADSurname){}
else
{Set-ItemProperty $CompanyRegPath'\Outlook Signature Settings' -name Surname -Value $ADSurname}
|