Powershell foreach会创建多个我想在创建后通过电子邮件发送的csv文件如何做到这一点?

问题描述

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)。全名

.'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\*

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...