简体   繁体   English

Powershell 脚本,用于发送从带有特定附件的 CSV/Excel 文件中读取的电子邮件

[英]Powershell script to send out emails read from a CSV/Excel file with a specific attachment

Currently working on a script that gathers all owners email address' within an Azure AD group and exports them as a TXT/CSV file as names the file as the group name for a security audit.目前正在编写一个脚本,该脚本收集 Azure AD 组中的所有所有者 email 地址',并将它们导出为 TXT/CSV 文件,并将该文件命名为安全审核的组名。 For example, GroupName.csv contains, Owner1@domain.com,Owner2@domain.com etc. I want to write a script that will send out emails to the group owners listed in the.csv file but send out an attachment of a list of group members, that are also saved as GroupName.csv, that is already saved in a different folder.例如,GroupName.csv 包含 Owner1@domain.com、Owner2@domain.com 等。我想编写一个脚本,向 .csv 文件中列出的组所有者发送电子邮件,但发送列表的附件组成员,也保存为 GroupName.csv,已保存在不同的文件夹中。 So, each group owner can validate if the group member belongs in that group.因此,每个群组所有者都可以验证群组成员是否属于该群组。 This is what I have found so far online.这是我目前在网上找到的。 Can anyone help me amend it to fit my needs.谁能帮我修改它以满足我的需要。 I'm new to PowerShell so very basic understanding.我是 PowerShell 的新手,所以非常基本的了解。 Apologies if this is hard to understand.如果这很难理解,我们深表歉意。 My organisation uses outlook FYI.我的组织使用 outlook FYI。

\#This is what I’ve written so far to export a list of groupowners and save them as their #groupnames, this works fine, but make any amendments necessary
$cred = Get-Credential Connect-AzureAD -Credential $cred
$groups = Get-AzureADGroup -All $true
ForEach($group in $groups) {
    $resultsarray = @()
    $owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true
    ForEach($owner in $owners) {
        $resultsarray += \[PSCustomObject\] @{" " = $owner.UserPrincipalName}
        $gName = $group.DisplayName[IO.Path]::GetinvalidFileNameChars() |
        ForEach - Object{$gName = $gName.Replace(
                             $_, "_")}[IO.Path] ::GetinvalidFileNameChars() |
        ForEach - Object{$gName = $gName.Replace(
                             '[', "_")}[IO.Path] ::GetinvalidFileNameChars() |
        ForEach - Object{$gName = $gName.Replace(']', "_")}

    #$resultsarray | Export-CSV -Path D :\\Script\\AzureADOwners$gName.csv -Delimiter ',' -NoTypeInformation

    $resultsarray | Out-File -Filepath D:\\Script\\AzureADOwners$gName.txt
    }
}
\#This what I've found online. The path of the CSV's of owners is D:\\Script\\AzureADOwners\\ and all #csv files are named as the GroupName.csv.

Import-Module ImportExcel
$csv_files = Get-ChildItem -Path 'D:\\Script\\AzureADOwners' -Filter '\*.csv'
foreach ($csv_file in $csv_files) {
    $data = Import-Csv -Path $csv_file.FullName
    foreach ($row in $data) {
        $email_address = $row.Column1
        $attachment = D:\Script\AzureADMembers\$csv_file.csv
        Send-MailMessage -To $email_address -Subject 'Test Email' -Body 'This is a test email.' -Attachments $attachment -SmtpServer 'smtp.gmail.com' -Credential(Get-Credential)
    }
}

I tried to reproduce the scenario in my environment:我试图在我的环境中重现该场景:

I used below powershell code:我使用了以下 powershell 代码:

  • First got group owners.From that exported csv of owners and collected mail id of each owner.首先得到组所有者。从中导出 csv 的所有者并收集每个所有者的邮件 ID。
  • For each owner, requested group members and stored in another csv to attacht to the mail对于每个所有者,请求组成员并存储在另一个 csv 中以附加到邮件中

Powershell code: Powershell 代码:

$groups = Get-AzureADGroup -All $true
ForEach($group in $groups) {
    $resultsarray = @()
    $owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true

$owners | foreach-object {
$owner = $_

$u_properties = [pscustomobject] @{
"groupName"=$group.DisplayName
"UserPrincipalName" = $owner.UserPrincipalName 
"Country" = $owner.Displayname 
"Email"=$owner.Mail
"GroupId" =$group.ObjectId
}



$path='D:\ownersFile.csv'
# check
write-host "got $u_properties"

 $u_properties | Export-csv -Path $path -NoTypeInformation -Force -Append


 $ExportedOwnerdata = Import-Csv -Path $path
 }

 #members
 $members = Get-AzureADGroupMember -ObjectId $group.ObjectId


$members | foreach-object {
$member = $_
write-host "got $member_properties"
 
$member_properties = [pscustomobject] @{
"groupName"=$group.DisplayName
"member" = $member.UserPrincipalName 
"memberDisplayName" = $member.Displayname 
}

#stroring mebers in csv in another local file
$mempath='D:\AzureADMembers.csv'
write-host "got $memer_properties"
 $member_properties| Export-csv -Path $mempath -NoTypeInformation -Force -Append

#getting owners data from already exported file
$wholedata= Import-Csv -Path 'D:\ownersFile.csv'
$data.UserPrincipalName  
$data.Email


$FileContent = get-content $mempath
foreach ($data in $wholedata)
{
#getting mail of each owner
$email_address=$data.Email | Sort-Object | Get-Unique
$email_address
$attachment=D:\AzureADMembers.csv
#Send-MailMessage -To $myemail  -Subject 'Test Email' -Body 'This is a test email.' -Attachments $mempath  -SmtpServer 'smtp.gmail.com' -Credential(Get-Credential)


}
}
}

在此处输入图像描述

在此处输入图像描述

owners file with mails:所有者文件与邮件:

在此处输入图像描述

members:成员:

在此处输入图像描述

Reference: send-mailmessage |参考: 发送邮件 | Microsoft learn 微软学习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM