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

[玩转系统] SharePoint Online:用于发送电子邮件的 PowerShell

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

SharePoint Online:用于发送电子邮件的 PowerShell


您是否知道可以使用 PowerShell 从 SharePoint Online 发送电子邮件?这是自动化基于电子邮件的工作流程(例如发送通知或警报)的好方法。在这篇博文中,我们将向您展示如何使用 PowerShell 执行此操作。

我们可以使用 SPUtility 的 SendEmail 函数或 PowerShell cmdlet Send-MailMessage 从 SharePoint Online 发送电子邮件。以下是这两种情况的示例:

PowerShell 在 SharePoint Online 中发送电子邮件:

本博文将向您展示如何使用 PowerShell 从 SharePoint Online 发送电子邮件。我们将演练使用本机 SharePoint 和 PowerShell 方式发送电子邮件的示例脚本!

以下是如何使用 PowerShell 客户端对象模型 (CSOM) 脚本从 SharePoint Online 发送电子邮件:


Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters                                                                 
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$EmailFrom ="[email protected]"
$EmailTo = "[email protected]"
$Subject ="SharePoint Online Storage Report"

#Setup Credentials and connect
$Cred = Get-Credential
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get Storage Usage of All Site collections
$SiteStorage = Get-SPOSite -Detailed | Select Url, StorageUsageCurrent
       
#Setup the site context for SPUtility SendEmail
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup Email
$EmailProperties = New-Object Microsoft.SharePoint.Client.Utilities.EmailProperties
$EmailProperties.From = $EmailTo
$EmailProperties.To = [String[]] $EmailTo
$EmailProperties.Subject = $Subject
$EmailProperties.Body = $SiteStorage | Convertto-html
[Microsoft.SharePoint.Client.Utilities.Utility]::SendEmail($Ctx,$EmailProperties)
$Ctx.ExecuteQuery()

这使用 SPUtility 的 SendMail 方法发送电子邮件,并消耗 SharePoint Online 中所有网站集的存储空间。此处,“发件人”和“收件人”电子邮件是组织内的电子邮件地址。

[玩转系统] SharePoint Online:用于发送电子邮件的 PowerShell

SharePoint Online PowerShell 发送电子邮件:

这次,我们使用 PowerShell 的本机 cmdlet Send-MailMessage 从 SharePoint Online 网站发送电子邮件。使用 PowerShell 在 SharePoint Online 中发送电子邮件是与同事和协作者沟通的一种快速而简单的方法。首先,打开 PowerShell 控制台并键入以下脚本:


Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$EmailFrom ="[email protected]"
$EmailTo =@("[email protected]","[email protected]")
$EmailSubject ="Site Collection Storage Utilization Report"
$SMTP ="smtp.office365.com"

#Setup Credentials and connect
$Cred = Get-Credential
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get Storage Usage of All Site collections
$SiteStorage = Get-SPOSite -Detailed | Select Url, StorageUsageCurrent | Convertto-html | Out-String

#Send Email
Send-MailMessage -from $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $SiteStorage -BodyAsHtml -smtpserver $SMTP -usessl -Credential $Cred -Port 587 

上面的示例展示了如何向多个收件人发送电子邮件并设置电子邮件的主题和正文。您需要为 $AdminSiteURL、$EmailFrom、$EmailTo 和 $EmailSubject 变量指定自己的值。如果您想向电子邮件添加任何附件,请使用以下命令:


#send-mailmessage in powershell with multiple attachments
$Attachments = "D:\Reports\SiteDeleteRpt1.csv","D:\Reports\SiteDeleteRpt2.csv"
  
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHTML -Attachments $Attachments -SmtpServer $SmtpServer -Credential $Cred -Port 587

SharePoint Online:PnP PowerShell 发送电子邮件

我们还可以使用 Send-PnPMail cmdlet 从 SharePoint Online 中的 PowerShell 发送电子邮件。这会从当前站点上下文发送一封电子邮件。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#sharepoint online powershell send email
Send-PnPMail -To [email protected] -Subject "Welcome" -Body "Welcome to Crescent Inc."

此 cmdlet 还支持从租户用户发送电子邮件。


Send-PnPMail -To [email protected] -Subject "Welcome" -Body "Welcome to Crescent Inc." -From [email protected] -Password "Password of the From User"

从 CSV 文件导入 - 格式为 HTML 表 - 并使用 PowerShell 发送电子邮件

在另一个要求中,我必须从 CSV 文件中读取数据,将其格式化为 HTML 表格,然后通过电子邮件发送。下面是执行此操作的 PowerShell 脚本:


#Parameters
$CSVPath  = "C:\Temp\ADGroups.csv"
$EmailTo = "[email protected]"

#Import Data from CSV File
$CSVFile = Import-Csv $CSVPath

#Define CSS Styles
$HeadTag = @"
<style type="text/css">
table {
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate;   
}
 
th {
border-width: 1px; padding: 5px; background-color:#8064a2; font-size: 14pt; font-weight: 600;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: white;
}
 
td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; padding:5px;  
}
 
tr:nth-child(even) { background-color: #e4dfec; }
</style>
"@

#Frame Email Body
$EmailBody = $CSVFile | ConvertTo-Html -Head $HeadTag -PreContent "<h1>Site Users and Groups - Report</h1>" | Out-String

#Send Email
Send-PnPMail -To $EmailTo -Subject "Users and Groups Report" -Body $EmailBody

这是正在运行的电子邮件:

[玩转系统] SharePoint Online:用于发送电子邮件的 PowerShell

PowerShell 可以帮助您快速轻松地发送批量电子邮件,从而节省您的时间和麻烦。

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

取消回复欢迎 发表评论:

关灯