Microsoft Powershell 签名脚本 - 已停止与最新的 Office 365 版本配合使用

Microsoft Powershell 签名脚本 - 已停止与最新的 Office 365 版本配合使用

这是我在这里的第一篇帖子,所以我希望我能将所有的信息传达给您!

首先,我知道有付费解决方案可以做到这一点,但这个解决方案在我工作的公司已经运行了一段时间。因此,我希望它能在我们现场使用的所有版本的 Office 上运行 - 从 2013 到 Office 365。此外,如果在现场维护和创建某些东西,它会变得更有趣,尽管这一切都稍微困难一些。

我们发现的问题是,签名已停止推送给使用最新 Office 365 版本的用户。除此之外,微软不允许我更新到此更高版本,因此我无法在不对某些受影响用户造成重大影响的情况下诊断问题。我猜想这是因为签名屏幕看起来与我的完全不同,但尚未确认 - 今早将把此事作为优先事项。

额外信息 11/10/2022: 文件和修改后的模板文件正在到达 Windows 签名位置内的签名位置,但最新版本的 Office 365 无法识别这一点。如果我在 Office 中手动创建签名,则所有文件都在那里,包括一些额外的文件(富文本和文本格式)以及从脚本创建的文件,但只有手动创建的文件才会出现在 Outlook 中。因此,我相信,这并非完全是脚本的问题,而是 Outlook 中的更改阻止了签名被识别

以下是完整的 PowerShell 代码:

# Gets the path to the user appdata folder
$AppData = (Get-Item env:appdata).value
# This is the default signature folder for Outlook
$localSignatureFolder = $AppData+'\Microsoft\Signatures'
# This is a shared folder on your network where the signature template should be
$templateFilePath = **THE FILE PATH TO THE TEMPLATE SIGNAUTRE**

# Get the current logged in username
$un = $env:username

# The following 5 lines will query AD and get an ADUser object with all information
$filter = "(&(objectCategory=User)(samAccountName=$un))"
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.Filter = $filter
$ADUserPath = $searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()

# Now extract all the necessary information for the signature
$name = $ADUser.DisplayName
$email = $ADUser.mail
$job = $ADUser.title
$landline = $ADUser.telephonenumber
$mobile = $ADUSer.mobile
$office = $ADUser.physicalDeliveryOfficeName
$so = $ADUser.description
$adchangedate = $ADUser.whenChanged

# Placeholder on html file association
$namePlaceHolder = "\[DISPLAY_NAME\]"
$emailPlaceHolder = "\[EMAIL\]"
$jobPlaceHolder = "\[JOB_TITLE\]"
$folderPlaceHolder = "\[FOLDER_PATH\]"
$phonePlaceHolder = "\[TELEPHONE\]"
$SOPlaceHolder = "\[SIGN_OFF\]"

# Save it as <username>.htm
$namefolder = $un + " (" + $email + ")_files";
$namefolder2 = $un + "_Reply (" + $email + ")_files";
$filename = $localSignatureFolder + "\" + $un + " (" + $email + ").htm";
$filename2 = $localSignatureFolder + "\" + $un + "_reply" + " (" + $email + ").htm";
$foldername = $localSignatureFolder + "\" + $namefolder;
$foldername2 = $localSignatureFolder + "\" + $namefolder2;
$templatefolderpath = $templateFilePath + "\Harboro_template_files\*";
$templatefolderpath2 = $templateFilePath + "\Harboro_Reply_template_files\*";

#NO EMAIL ON AD, NO SIGNATURE
if ($email -ne "") 
{

#change contents of template html file to work with the script below, only if template has been changed from the original
#Asign varibale to pint at particlular file
$rawtemp = get-content $templateFilePath"\Harboro_template.htm"
$rawtemp2 = get-content $templateFilePath"\Harboro_Reply_template.htm"
$check1 = ($rawtemp | Select-String -Pattern "Harboro_template_files").count
$check2 = ($rawtemp2 | Select-String -Pattern "Harboro_Reply_template_files").count

if ($check1 -ne 0 -or $check2 -ne 0)
{
#MAIN TEMPLATE CONTENTS
$newContent = $rawtemp -replace "Harboro_template_files","[FOLDER_PATH]"
$newContent | Set-Content -Path $templateFilePath"\Harboro_template.htm" -force
#REPLY TEMPLATE CONTENTS
$newContent2 = $rawtemp2 -replace "Harboro_Reply_template_files","[FOLDER_PATH]"
$newContent2 | Set-Content -Path $templateFilePath"\Harboro_Reply_template.htm" -force
#RESET THE RAWTEMP VARAIBLE WITH NEW FILE CONTENTS
$rawtemp = get-content $templateFilePath"\Harboro_template.htm"
$rawtemp2 = get-content $templateFilePath"\Harboro_Reply_template.htm"
}

#find place holders and convert them to users data from AD variables
if ($so -eq "")
{
$sig = $rawtemp -replace $SOPlaceHolder,""
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $SOPlaceHolder,""
$rawtemp2 = $sig2
}
else {
$sig = $rawtemp -replace $SOPlaceHolder,$so
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $SOPlaceHolder,$so
$rawtemp2 = $sig2
}

$sig = $rawtemp -replace $namePlaceHolder,$name
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $namePlaceHolder,$name
$rawtemp2 = $sig2

$sig = $rawtemp -replace $emailPlaceHolder,$email
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $emailPlaceHolder,$email
$rawtemp2 = $sig2

if ($mobile -ne "")
{
$phone = $landline + "&nbsp;&nbsp;&nbsp;Mobile:&nbsp;&nbsp;" + $Mobile
}
else {
$phone = $landline
}

$sig = $rawtemp -replace $phonePlaceHolder,$phone
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $phonePlaceHolder,$phone
$rawtemp2 = $sig2

$sig = $rawtemp -replace $jobPlaceHolder,$job
$rawtemp = $sig
$sig2 = $rawtemp2 -replace $jobPlaceHolder,$job
$rawtemp2 = $sig2

$sig = $rawtemp -replace $folderPlaceHolder,$namefolder
$sig2 = $rawtemp2 -replace $folderPlaceHolder,$namefolder2

# Determines daylight saving
$adchangedate = write-output $adchangedate
$adchangedate = [DateTime]$adchangedate
if ($adchangedate.IsDaylightSavingTime() -eq $true)
    {
        $adchangedate = $adchangedate.AddHours(1)
    }

#IMAGES FOLDER
#If Signature supporting folder exists checks 
if (Test-Path -Path $foldername) {
        $SourceDocs = Get-ChildItem –Path $templatefolderpath | foreach  {Get-FileHash –Path $_.FullName}
        $DestDocs = Get-ChildItem –Path $foldername | foreach  {Get-FileHash –Path $_.FullName}
        $count = (Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs  -Property hash -PassThru).count
        $SourceDocs2 = Get-ChildItem –Path $templatefolderpath2 | foreach  {Get-FileHash –Path $_.FullName}
        $DestDocs2 = Get-ChildItem –Path $foldername2 | foreach  {Get-FileHash –Path $_.FullName}
        $count2 = (Compare-Object -ReferenceObject $SourceDocs2 -DifferenceObject $DestDocs2  -Property hash -PassThru).count 
        # If changes in folder, then update else do nothing
        if ($count –ne 0){
            Remove-Item -Path $foldername -recurse -force
            New-Item -Path $foldername -ItemType Directory
            Copy-Item -Path $templatefolderpath -Destination $foldername -PassThru -force;
        }
        if ($count2 –ne 0){
            Remove-Item -Path $foldername2 -recurse -force
            New-Item -Path $foldername2 -ItemType Directory
            Copy-Item -Path $templatefolderpath2 -Destination $foldername2 -PassThru -force;
        }
} else {
   # if folder does not exist then create directory
    New-Item -Path $foldername -ItemType Directory
    Copy-Item -Path $templatefolderpath -Destination $foldername -PassThru -force;
    New-Item -Path $foldername2 -ItemType Directory
    Copy-Item -Path $templatefolderpath2 -Destination $foldername2 -PassThru -force;
}

#MAIN SIGNATURE
# Gets the last update time of the template.
if(test-path $templateFilePath){
    $templateLastModifiedDate = [datetime](Get-ItemProperty -Path $templateFilePath -Name LastWriteTime).lastwritetime
}

# Checks if there is a signature and its last update time
if(test-path $filename){
    $sigLastModifiedDate = [datetime](Get-ItemProperty -Path $filename -Name LastWriteTime).lastwritetime
    if((get-date $templateLastModifiedDate) -gt (get-date $sigLastModifiedDate)){
        $sig > $filename
    }
    # If an AD change has been made and the created signature is older, then update signature. 
    elseif(($adchangedate) -gt (get-date $sigLastModifiedDate)){
        $sig > $filename
    }
}else{
    $sig > $filename
}

#REPLY SIGNATURE
#CHECK IF THERE IS A SIGNATURE AND ITS LAST UPDATE TIME
if(test-path $filename2){
    $sigLastModifiedDate = [datetime](Get-ItemProperty -Path $filename2 -Name LastWriteTime).lastwritetime
    if((get-date $templateLastModifiedDate) -gt (get-date $sigLastModifiedDate)){
        $sig2 > $filename2
    }
    # If an AD change has been made and the created signature is older, then update signature. 
    elseif(($adchangedate) -gt (get-date $sigLastModifiedDate)){
    $sig2 > $filename2
    }
}else{
    $sig2 > $filename2
}

#SET DEFAULT SIGNATURE
$MSWord = New-Object -ComObject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignature.NewMessageSignature = $un + " (" + $email + ")" #insert the main signature name
$EmailSignature.ReplyMessageSignature = $un + "_reply" + " (" + $email + ")" #insert the reply signature name
$MSWord.Quit()

}

有任何想法吗?

非常感谢,马修

答案1

对!我终于弄清楚了这件事以及发生了什么。事实上,这与 Outlook 365 中的漫游签名有关 - 这是一项于 2022 年 10 月初启用的新功能。

好消息是它可以通过注册表编辑来关闭(如果需要的话,可以通过 GP 推出)。

注册表位置为:HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Setup\

DWORD(32 位)- 注册名称:DisableRoamingSignaturesTemporaryToggle

修改或双击DisableRoamingSignaturesTemporaryToggle

并输入数值数据 1,然后输入十六进制

单击“确定”,然后单击“应用”。

完整指南

我希望这能像帮助我一样帮助其他人。

相关内容