powershell 发送包含广告用户组的HTML电子邮件

tez616oj  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(115)

我是Powershell的新手,我想知道是否有人能帮助我。我试图发送带有广告用户组的html电子邮件给他们的经理。
我的问题是:
该表仅包含AD组名称列和说明。我还希望它包含用户的全名和职务。
我的另一个问题是,我还希望脚本在发送之前验证用户或经理的电子邮件是否存在。如果不存在,返回一个错误消息,然后重试。
如果有人能帮助我,我将非常感激。这将帮助我更好地了解和提高自己。
感谢您发送编修。

$UserName = (Read-Host "Username").trim()
$EmailSuperior = (Read-Host "Email address of superior") #or if we can just type de samaccountname it will be perfect

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

# Get group memberships from reference user
$adUser = Get-ADUser -Filter "SamAccountName -eq '$($UserName)'" -Properties Name, Title, Manager, MemberOf

# Define parameters for mailing and send mail to IT-responsible person to review access
$MemberOf = Get-ADPrincipalGroupMembership -Identity $UserName| Get-ADGroup -Properties * | Select name, description | Sort-Object -Property name | ConvertTo-Html -Head $style
$SmtpServer = 'mail.contoso.com'
$FromSender = 'no-reply@contoso.com'
$Subject = 'Review Access: ' + $UserName

# Email Body Set Here
$Body ="
Hello,<br>
<br>
We ask that you verify if these groups are to remain or if any need to be removed with their change of position.<br>
<br>
<br>
$MemberOf
<br>
"

Send-MailMessage -SmtpServer $SmtpServer -From $FromSender -To $EmailSuperior -Bcc $FromSender -Subject $Subject -Encoding "UTF8" -Body $Body -BodyAsHtml
vuktfyat

vuktfyat1#

我喜欢使用pscustomobjects创建如下报告:

# Get group memberships from reference user
$adUser = Get-ADUser -Filter "SamAccountName -eq '$($UserName)'" -Properties Name, Title, Manager, MemberOf

# Define parameters for mailing and send mail to IT-responsible person to review access
$Report = $aduser.MemberOf | 
  Get-ADGroup -Properties description | Foreach {
    [pscustomobject]@{
      UserName    = $adUser.Name
      Title       = $adUser.Title
      GroupName   = $_.Name
      description = $_.description
    }
  } | 
  Sort-Object -Property GroupName | ConvertTo-Html -Head $style

# outputs like this if not converted to html:

UserName     Title       GroupName  description                                                       
--------     -----       ---------  -----------                                                       
John Smith   Employee    Apps_Full  Applications Full Access                                          
John Smith   Employee    Apps_Read  Applications Read Access

并获取经理电子邮件:

$input = Read-Host "Email address or username of superior"
$EmailSuperior = if ($input -notlike '*@*') { 
  Get-ADUser $input -Properties mail | select -ExpandProperty mail
}

相关问题