当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Microsoft 365:密码过期通知电子邮件

作者:精品下载站 日期:2024-12-14 21:50:31 浏览:13 分类:玩电脑

Microsoft 365:密码过期通知电子邮件


密码过期通知在维护组织的 Microsoft 365 帐户的安全方面发挥着至关重要的作用。确保用户了解密码即将到期可以帮助防止帐户锁定并鼓励用户创建强而独特的密码。在本指南中,我们将演示如何创建 Microsoft Graph PowerShell 脚本,以便在用户的 Office 365 密码即将过期时自动向用户发送电子邮件通知。

什么是密码过期通知电子邮件?

密码过期通知电子邮件是当用户的密码即将过期时发送给用户的消息。该电子邮件通常包含有关密码到期日期的信息以及有关如何更改密码的说明。对于希望确保用户定期更改密码并确保帐户安全的组织来说,密码到期通知电子邮件是必不可少的工具。

如何在 Microsoft 365 中发送密码过期通知电子邮件?

由于 Microsoft 365 管理中心不再支持密码过期通知(可能是因为 Microsoft 建议禁用密码过期!),我们必须实施基于 Windows PowerShell 脚本的解决方案。要为 Office 365 密码过期通知创建 Microsoft Graph PowerShell 脚本,请执行以下步骤:

先决条件:安装 Microsoft Graph PowerShell 模块。您需要在计算机上安装 Microsoft Graph PowerShell 模块。使用:安装模块 Microsoft.Graph

使用此 PowerShell 脚本向密码即将过期的所有用户发送电子邮件提醒:


# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All", "Mail.Send"

# Set the notification threshold - days before password expires
$NotificationThreshold = 7
$PasswordExpiryThreshold = 90 #By default 90 days Password expiry

# Get all users
$AllUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName,Mail,UserType, AccountEnabled,PasswordPolicies,lastPasswordChangeDateTime

# Iterate through each user
ForEach ($User in $AllUsers) 
{
    # Skip disabled accounts and users with Password never Expire flag
    If (!$User.AccountEnabled -or $User.PasswordPolicies -contains "xDisablePasswordExpiration" -or $User.userType -eq "Guest") {
        continue
    }

    # Get user's password Expiry Date
    $PasswordExpiryDate = $User.lastPasswordChangeDateTime.AddDays($PasswordExpiryThreshold)

    # Calculate the remaining days
    $RemainingDays = ($PasswordExpiryDate - (Get-Date)).Days

    # Check if the remaining days are within the notification threshold
    If ($RemainingDays -le $NotificationThreshold) {
        # Send an email notification to the user
        $EmailBody = "
            Hello $($User.DisplayName),
            <br/><br/>
            Your Office 365 password will expire in $remainingDays days. Please change your password before it expires to avoid any disruption in accessing your account.
            <br/><br/>
            To change your password, follow these steps:<br/>
            <ol>
            <li>Sign in to Office 365 (https://www.office.com)</li>
            <li>Click on your profile picture in the top right corner.</li>
            <li>Select 'View account'.</li>
            <li> Click 'Password'.</li>
            <li> Follow the instructions to change your password.</li>
            </ol>
            <br/>
            Thank you,<br/>
            IT Support Team
        "

    $MailParams = @{
	    Message = @{
		    Subject = "Your Office 365 password will expire soon"
            Importance = "High"
		    Body = @{
			    ContentType = "html"
			    Content = $EmailBody
		    }
		    ToRecipients = @(
			    @{
				    EmailAddress = @{
					    Address = $User.Mail
				    }
                }
		    )
	    }
    }
    # Send the email using Microsoft Graph
    Send-MgUserMail -UserId $User.Mail -BodyParameter $MailParams
    }
}

此脚本将提示输入登录凭据,并遍历组织中的所有用户,计算密码到期日期,并向密码将在指定阈值(在我们的示例中为 7 天)内到期的用户发送电子邮件通知。您可以修改 $NotificationThreshold 变量来调整发送通知时密码到期前的天数。

这是正在运行的脚本:

[玩转系统] Microsoft 365:密码过期通知电子邮件

自动发送密码到期提醒电子邮件

对于无人值守的自动化场景,请按照以下步骤操作:

第1步:创建App ID并授予权限

在 Azure AD 中创建应用 ID 以与 Microsoft Graph 连接:如何连接到 Microsoft Graph PowerShell 模块?确保您向 App ID 授予“User.Read.All”和“Mail.Send”权限。

步骤 2:更新此 PowerShell 脚本中的参数

准备好应用程序 ID 后,更新此 PowerShell 脚本的参数部分。该脚本的工作原理如下:

  • 使用给定的应用程序 ID 和证书指纹连接到 Microsoft Graph
  • 检查所有用户的密码过期日期 - 排除密码永不过期、密码已过期、访客用户等用户。
  • 从给定的电子邮件 ID 向密码在给定阈值(脚本中为 15 天!)内过期的用户发送电子邮件(确保您已创建电子邮件。例如,在我们的示例中,它是:$SenderID=“Helpdesk@ Crescent.com”)。
  • 将消息记录到日志文件中。

#Parameters
$TenantID = "c1c2b123-4240-4775-a19a-6351b30cx2f6"
$ClientID = "3c1541f7-9565-4abe-b398-f47ac91c2c06" #App ID
$CertThumbPrint = "63FDDD207DE01C0EB34C35647D086D3C1520FA7"
#Path for Log File
$LogFilePath = "C:\Scripts\PasswordExpiry.Log"
$SenderID = "[email protected]"
#Notification threshold - days before password expires
$NotificationThreshold = 15
$PasswordExpiryThreshold = 90 #By default 90 days Password expiry

#Function to Add Content to a Log File
Function Write-Log {
 
  [CmdletBinding()]
   
  Param ([Parameter(Mandatory=$true)][string]$Message)  

  Process{
        #Add Message to Log File with timestamp
        "$([datetime]::Now) : $Message" | Out-File -FilePath $LogFilePath -append;
   
        #Write the log message to the screen
        Write-host $([datetime]::Now) $Message
    }
}

#Connect to Microsoft Graph using App
Connect-MgGraph -ClientID $ClientID -TenantId $TenantID -CertificateThumbprint $CertThumbPrint

#Ensure the Parent Folder for Log File
$FolderPath= Split-Path $LogFilePath
If(!(Test-Path -path $FolderPath)) {  
    New-Item -ItemType directory -Path $FolderPath | Out-Null
}

# Get all users
$AllUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName,Mail,UserType, AccountEnabled,PasswordPolicies,lastPasswordChangeDateTime

# Iterate through each user
ForEach ($User in $AllUsers) 
{
    # Skip disabled accounts and users with Password never Expire flag
    If (!$User.AccountEnabled -or $User.PasswordPolicies -contains "DisablePasswordExpiration" -or $User.userType -eq "Guest") {
        continue
    }

    # Get user's password Expiry Date
    $PasswordExpiryDate = $User.lastPasswordChangeDateTime.AddDays($PasswordExpiryThreshold)

    # Calculate the remaining days
    $RemainingDays = ($PasswordExpiryDate - (Get-Date)).Days

    # Check if the remaining days are within the notification threshold
    If ($RemainingDays -le $NotificationThreshold -and $RemainingDays -ge 0) {
        # Send an email notification to the user
        $EmailBody = "
            Hello $($User.DisplayName),
            <br/><br/>
            Your Office 365 password will expire in $remainingDays days. Please change your password before it expires to avoid any disruption in accessing your account.
            <br/><br/>
            To change your password, follow these steps:<br/>
            <ol>
            <li>Sign in to Office 365 (https://www.office.com)</li>
            <li>Click on your profile picture in the top right corner.</li>
            <li>Select 'View account'.</li>
            <li> Click 'Password'.</li>
            <li> Follow the instructions to change your password.</li>
            </ol>
            <br/>
            Thank you,<br/>
            IT Support Team
        "

    $MailParams = @{
	    Message = @{
		    Subject = "Your Office 365 password will expire soon"
            Importance = "High"
		    Body = @{
			    ContentType = "html"
			    Content = $EmailBody
		    }
		    ToRecipients = @(
			    @{
				    EmailAddress = @{
					    Address = $User.Mail
				    }
                }
		    )
	    }
    }
    # Send the email using Microsoft Graph
    Send-MgUserMail -UserId $SenderID -BodyParameter $MailParams
    
    #Write to Log File
    Write-Log "Password Expiration Notification sent to user $($User.Mail) - Password Expires on $PasswordExpiryDate"
    }
}

该脚本可以极大地节省生产力,因为如果没有适当的通知,用户可能会在密码过期时措手不及,从而导致帐户锁定和生产力中断。这些电子邮件将通知用户他们的密码即将过期,应更改密码以避免任何中断。

步骤 3:安排 PowerShell 脚本

您可以创建一个自动化系统,用于向 Office 365 用户发送密码过期通知电子邮件:使用 Windows 任务计划程序计划 PowerShell 脚本或使用 Azure Runbook 自动执行 PowerShell。

通过遵循我们的分步说明,您将能够帮助用户随时了解其密码到期日期并保持对其 Office 365 帐户的安全访问。

密码过期通知电子邮件的最佳实践

以下是组织在实施密码过期通知电子邮件时应遵循的一些最佳实践:

  1. 确保用户在密码到期日期之前收到通知电子邮件。这将使他们有足够的时间更改密码,而不会中断他们的工作。
  2. 提供有关如何更改密码的明确说明。包含用户可用来更改密码的资源链接。
  3. 鼓励用户创建难以猜测或破解的强而复杂的密码。
  4. 考虑实施多重身份验证,为用户帐户添加额外的安全层。
  5. 定期检查您的密码政策,以确保其是最新的并符合行业最佳实践。

结论:

使用用于 Office 365 密码过期通知的 Microsoft Graph PowerShell 脚本,您可以自动执行提醒用户在密码过期之前更新密码的过程。这种主动方法可以帮助用户随时了解其密码到期日期,降低帐户锁定的风险,并为您的组织营造一个安全的环境。实施此 Microsoft Graph PowerShell 解决方案可以增强组织的安全性并保持对 Office 365 资源的无缝访问,同时为所有用户提供安全的环境。

经常问的问题:

如何设置Office 365账户密码永不过期?

要将 Office 365 用户帐户的密码设置为永不过期,可以使用 Azure Active Directory PowerShell 或 Microsoft Graph API PowerShell cmdlet。您还可以通过 Office 365 管理中心中的密码过期策略禁用组织的密码过期(设置 >> 然后单击“组织设置”>>“安全和隐私”>> 选择密码过期策略 >> 选中旁边的框“将用户密码设置为永不过期”)。
详细信息:在 Office 365 中禁用密码过期

Office 365 是否会发送密码过期电子邮件?

Microsoft 365 不再支持密码过期通知!因此,您必须设置解决方法来向用户发送密码过期电子邮件。

Office 365 中的默认密码过期时间是多少?

自上次密码更改日期起 90 天(默认情况下禁用)。但是,全局管理员可以通过 Office 365 管理中心或 PowerShell 配置密码过期时间以满足组织的安全要求。

O365 密码过期后会发生什么?

当 Office 365 当前密码过期时,系统将提示用户在下次登录时更改密码。如果用户在实际过期日期后忽略提示并且不更改密码,他们的帐户将被锁定,直到他们重置了密码(管理员级别的技术支持可以解锁它)。但是,Outlook 等 Microsoft Office 应用程序可能会继续工作,直到缓存中的密码过期。

不再建议密码过期吗?

是的!不再建议将密码过期作为安全措施,因为它弊大于利。每 90 天更改一次密码可能会使黑客/诈骗者/网络犯罪分子在长达三个月内可以使用暴露的弱/旧密码,并且最好要求强密码并使用多重身份验证。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯