Powershell foreach 创建多个 csv 文件,我希望在创建后通过电子邮件发送这些文件。如何做到这一点?

Powershell foreach creates multiple csv files that I would like to have emailed once created. How may this be accomplished?

Powershell foreach 创建多个 csv 文件,我希望在创建后通过电子邮件发送这些文件。 如何做到这一点? 我可以创建另一个 foreach 来发送每个新命名的 csv 文件,还是尝试压缩输出以通过电子邮件发送?

#create a connection to the Exchange 2010 server  
.'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -Auto -AllowClobber

#this locates any mailbox that is equal or over 28GB and gets the user to create an individual csv file for that person The amount of created csv files may change at any time.

$mstat= Get-Mailbox -ResultSize unlimited | Get-MailboxStatistics |
    ?{$_.TotalItemSize.Value.ToGB() -ge 28} |
    ForEach-Object {$_.DisplayName}

foreach ($user in $mstat){
#This creates the initial csv file with added columns to be added in later
Get-MailboxStatistics -Identity $user |
    Select DisplayName,
    @{N='Storage Limit Status'; E={$_.StorageLimitStatus}}, 
    @{N="Total Items"; E={"{0:N0}" -f($_.ItemCount)}},
    @{N="Total Size in MB"; E={"{0:N0}" -f($_.TotalItemSize.Value.ToMB())}}, 
    @{N="Total Size in GB"; E={"{0:N1}" -f($_.TotalItemSize.Value.ToGB())}}, 
    Database,'Folder Name',"Items in Folder",”Folder Size in MB” |
    Sort-Object TotalItemSize -Descending | 
    Export-Csv C:\path$user.csv -NoTypeInformation -Force

#This adds the Folder sizes and appends the data to the already created csv file
Get-MailboxFolderStatistics $user |
    ? {$_.ItemsInFolder -gt 0} |
    Sort ItemsInFolder -Descending |
    Select @{N='Folder Name'; E={$_.Name}},
    @{N="Items in Folder"; E={"{0:N0}" -f($_.ItemsInFolder)}},
    @{N=”Folder Size in MB”;E={"{0:N0}" -f($_.FolderSize.ToMb())}} |      
    Export-Csv C:\path$user.csv -NoTypeInformation -Append -force

}

#Email is sent with the following

$From = "Exchange_Mailbox_Limit@email.com"
$To = "address@email.com”
$Attachment = "Path to csv(s) files"
$Subject = "Mailbox Size Getting Close to Limits"
$SMTPServer = "Server IP address"

Send-MailMessage -From $From -to $To -Subject $Subject -SmtpServer $SMTPServer -Attachments $Attachment;

我能够使用变量分别发送所有文件 $files = (ls C:\path\mail).FullName

.'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1' Connect-ExchangeServer -Auto -AllowClobber


$mbox  = (Get-Mailbox).Alias | sort $_.Alias
$mstat = $mbox | Get-MailboxStatistics | ?{$_.TotalItemSize.Value.ToGB() -ge 26} |
            ForEach-Object {$_.DisplayName}
            

foreach ($user in $mstat){

Get-MailboxStatistics -Identity $user |
    Select DisplayName,
    @{N='Storage Limit Status'; E={$_.StorageLimitStatus}}, 
    @{N="Total Items"; E={"{0:N0}" -f($_.ItemCount)}},
    @{N="Total Size in MB"; E={"{0:N0}" -f($_.TotalItemSize.Value.ToMB())}}, 
    @{N="Total Size in GB"; E={"{0:N1}" -f($_.TotalItemSize.Value.ToGB())}}, 
    @{N="Warning Quota Limit (MB)"; E= {"{0:N0}" -f(30720 -($_.TotalItemSize.Value.ToMB()))}},
    @{N="Prohibit Mail Quota Limit (MB)"; E= {"{0:N0}" -f(40960 -($_.TotalItemSize.Value.ToMB()))}},
    'Folder Name',"Items in Folder",”Folder Size in MB” |
    Sort-Object TotalItemSize -Descending | 
    Export-Csv C:\path\Mail$user.csv -NoTypeInformation -Force

Get-MailboxFolderStatistics $user |
    ? {$_.ItemsInFolder -gt 0} |
    Sort ItemsInFolder -Descending |
    Select @{N='Folder Name'; E={$_.Name}},
    @{N="Items in Folder"; E={"{0:N0}" -f($_.ItemsInFolder)}},
    @{N=”Folder Size in MB”;E={"{0:N0}" -f($_.FolderSize.ToMb())}} |      
    Export-Csv C:\path\Mail$user.csv -NoTypeInformation -Append -force

}

Get-PSSession
Remove-PSSession *
$files = (ls C:\path\Mail).FullName



$From = "Mailbox_Limit@email.com"
$To = "email@email.com”
$Attachment = $files
$Subject = "Mailbox Size Getting Close to Limits"
$Body = "<h2>$mstat</h2><br><br>"
$SMTPServer = "x.x.x.x"

Send-MailMessage -From $From -to $To -Subject $Subject -Body ($mstat | Out-String) -SmtpServer $SMTPServer -Attachments $Attachment;

Remove-Item C:\path\Mail\*