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

wko9yo5t  于 2023-01-30  发布在  Shell
关注(0)|答案(1)|浏览(244)

当前正在编写一个脚本,该脚本可收集Azure AD组内的所有所有者电子邮件地址,并将其导出为TXT/CSV文件,并将该文件命名为安全审核的组名称。例如,GroupName. csv包含、www.example.com、www.example.com等。我希望编写一个脚本,该脚本将向中列出的组所有者发送电子邮件。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. 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. Apologies if this is hard to understand. My organisation uses 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)
    }
}
vdzxcuhz

vdzxcuhz1#

我尝试在我的环境中重现该方案:

  • 我使用下面的powershell代码:*
  • 首先得到组所有者。从导出的csv所有者和收集的邮件ID的每个所有者。
  • 对于每个所有者,请求组成员并存储在另一个csv中以附加到邮件
    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)

}
}
}

包含邮件的所有者文件:

委员:

参考:send-mailmessage | Microsoft learn

相关问题