当前正在编写一个脚本,该脚本可收集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)
}
}
1条答案
按热度按时间vdzxcuhz1#
我尝试在我的环境中重现该方案:
Powershell代码:
包含邮件的所有者文件:
委员:
参考:send-mailmessage | Microsoft learn