整理所有用户服务的属性,并发送邮件
2014-07-17 21:47
330 查看
列出开启服务的用户数量,并用不同颜色显示。并将列表以附件发送邮件。
function SendMail
{
#mail server configuration
$smtpServer = " "
$smtpUser = ""
$smtpPassword = ""
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$MailAddress=""
$MailtoAddress=""
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.To.Add($MailtoAddress)
#set the content
$mail.Subject = "MailBox Data (" + (get-date).ToString("yyyy-MM-dd") + ")"
$mail.Priority = "High"
$mail.Body = "MailBox Data (" + (get-date).ToString("yyyy-MM-dd") + ")"
foreach($filename in $args[0])
{
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
}
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail)
}
write-host -ForegroundColor Red "Get User Data Begin!"
$MailboxList = @();
$i = 0;
$s = get-date;
Get-Mailbox -ResultSize 5000000 | where {
($_.UserAccountControl -band 2) -ne 2} |foreach{
if($_.UserPrincipalName -ne '')
{
$i = $i + 1;
$Mailbox = New-Object PSObject;
$m = Get-CasMailbox $_;
$domain = '';
$size = '';
$pop = 0;
$imap = 0;
$mapi = 0;
$activesync = 0;
$domain = $_.UserPrincipalName.psbase.Substring($_.UserPrincipalName.IndexOf("@")+1);
if($_.ProhibitSendReceiveQuota.psbase.IsUnlimited -eq $True)
{$size = 'Unlimited';}
else
{$size =$_.ProhibitSendReceiveQuota.psbase.Value.ToMB().psbase.ToString();}
if($m.PopEnabled -eq $True)
{$pop = 1;}
if($m.ImapEnabled -eq $True)
{$imap = 1;}
if($m.MapiEnabled -eq $True)
{$mapi = 1;}
if($m.ActiveSyncEnabled -eq $True)
{$activesync = 1;}
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name UserAccount -Value $_.UserPrincipalName;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Domain -Value $domain;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Size -Value $size;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Pop -Value $pop;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Imap -Value $imap;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Mapi -Value $mapi;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name ActiveSync -Value $activesync;
$MailboxList += $Mailbox;
if($i % 100 -eq 0)
{
write-host -ForegroundColor Green ("User Count Now: "+$i);
$e = get-date;
write-host -ForegroundColor Green ("Time: "+ ($e-$s).Hours + ":" + ($e-$s).Minutes + ":" + ($e-$s).Seconds);
}
}
}
write-host -ForegroundColor Green ("User Total Count: "+$i)
write-host -ForegroundColor Red "Get User Data Finished!"
write-host -ForegroundColor Red "Begin Write User Data File!"
$file = (get-date).ToString("yyyy-MM-dd");
$index = 1;
while(Test-Path ("AllUser(" + $file + "-" + $index + ").csv"))
{
$index = $index + 1;
}
while(Test-Path ("AllDomain(" + $file + "-" + $index + ").csv"))
{
$index = $index + 1;
}
$MailboxList | Export-Csv -Path ("AllUser(" + $file + "-" + $index + ").csv") -NoTypeInformation
write-host -ForegroundColor Red "Get User Data Finished!"
write-host -ForegroundColor Red "Get Domain Data Begin!"
$DomainDataList = @();
$i = 0;
$MailboxList | Group-Object -Property Domain | foreach{
$i = $i + 1;
$GroupData = ($_.Group | Measure-Object -Property Pop, Imap, Mapi, ActiveSync -Sum);
$DomainData = New-Object PSObject;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name Domain -Value $_.Name;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name TotalCount -Value $_.Count;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name PopCount -Value $GroupData[0].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name ImapCount -Value $GroupData[1].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name MapiCount -Value $GroupData[2].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name ActiveSyncCount -Value $GroupData[3].Sum;
$DomainDataList += $DomainData;
if($i % 100 -eq 0)
{
write-host -ForegroundColor Green ("Domain Count Now: "+$i);
write-host -ForegroundColor Green ("Time: "+ ($e-$s).Hours + ":" + ($e-$s).Minutes + ":" + ($e-$s).Seconds);
}
}
write-host -ForegroundColor Green ("Domain Total Count: "+$i)
write-host -ForegroundColor Red "Begin Write Domain Data File!"
$DomainDataList | Export-Csv -Path ("AllDomain(" + $file + "-" + $index + ").csv") -NoTypeInformation
write-host -ForegroundColor Red "Get Domain Data Finished!"
$attaches = @()
$attaches += (Get-Location).Path + "AllUser(" + $file + "-" + $index + ").csv"
$attaches += (Get-Location).Path + "AllDomain(" + $file + "-" + $index + ").csv"
SendMail $attaches
write-host -ForegroundColor Red "Send Mail Finished!"
function SendMail
{
#mail server configuration
$smtpServer = " "
$smtpUser = ""
$smtpPassword = ""
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$MailAddress=""
$MailtoAddress=""
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.To.Add($MailtoAddress)
#set the content
$mail.Subject = "MailBox Data (" + (get-date).ToString("yyyy-MM-dd") + ")"
$mail.Priority = "High"
$mail.Body = "MailBox Data (" + (get-date).ToString("yyyy-MM-dd") + ")"
foreach($filename in $args[0])
{
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
}
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail)
}
write-host -ForegroundColor Red "Get User Data Begin!"
$MailboxList = @();
$i = 0;
$s = get-date;
Get-Mailbox -ResultSize 5000000 | where {
($_.UserAccountControl -band 2) -ne 2} |foreach{
if($_.UserPrincipalName -ne '')
{
$i = $i + 1;
$Mailbox = New-Object PSObject;
$m = Get-CasMailbox $_;
$domain = '';
$size = '';
$pop = 0;
$imap = 0;
$mapi = 0;
$activesync = 0;
$domain = $_.UserPrincipalName.psbase.Substring($_.UserPrincipalName.IndexOf("@")+1);
if($_.ProhibitSendReceiveQuota.psbase.IsUnlimited -eq $True)
{$size = 'Unlimited';}
else
{$size =$_.ProhibitSendReceiveQuota.psbase.Value.ToMB().psbase.ToString();}
if($m.PopEnabled -eq $True)
{$pop = 1;}
if($m.ImapEnabled -eq $True)
{$imap = 1;}
if($m.MapiEnabled -eq $True)
{$mapi = 1;}
if($m.ActiveSyncEnabled -eq $True)
{$activesync = 1;}
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name UserAccount -Value $_.UserPrincipalName;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Domain -Value $domain;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Size -Value $size;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Pop -Value $pop;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Imap -Value $imap;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name Mapi -Value $mapi;
Add-Member -InputObject $Mailbox -MemberType NoteProperty -Name ActiveSync -Value $activesync;
$MailboxList += $Mailbox;
if($i % 100 -eq 0)
{
write-host -ForegroundColor Green ("User Count Now: "+$i);
$e = get-date;
write-host -ForegroundColor Green ("Time: "+ ($e-$s).Hours + ":" + ($e-$s).Minutes + ":" + ($e-$s).Seconds);
}
}
}
write-host -ForegroundColor Green ("User Total Count: "+$i)
write-host -ForegroundColor Red "Get User Data Finished!"
write-host -ForegroundColor Red "Begin Write User Data File!"
$file = (get-date).ToString("yyyy-MM-dd");
$index = 1;
while(Test-Path ("AllUser(" + $file + "-" + $index + ").csv"))
{
$index = $index + 1;
}
while(Test-Path ("AllDomain(" + $file + "-" + $index + ").csv"))
{
$index = $index + 1;
}
$MailboxList | Export-Csv -Path ("AllUser(" + $file + "-" + $index + ").csv") -NoTypeInformation
write-host -ForegroundColor Red "Get User Data Finished!"
write-host -ForegroundColor Red "Get Domain Data Begin!"
$DomainDataList = @();
$i = 0;
$MailboxList | Group-Object -Property Domain | foreach{
$i = $i + 1;
$GroupData = ($_.Group | Measure-Object -Property Pop, Imap, Mapi, ActiveSync -Sum);
$DomainData = New-Object PSObject;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name Domain -Value $_.Name;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name TotalCount -Value $_.Count;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name PopCount -Value $GroupData[0].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name ImapCount -Value $GroupData[1].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name MapiCount -Value $GroupData[2].Sum;
Add-Member -InputObject $DomainData -MemberType NoteProperty -Name ActiveSyncCount -Value $GroupData[3].Sum;
$DomainDataList += $DomainData;
if($i % 100 -eq 0)
{
write-host -ForegroundColor Green ("Domain Count Now: "+$i);
write-host -ForegroundColor Green ("Time: "+ ($e-$s).Hours + ":" + ($e-$s).Minutes + ":" + ($e-$s).Seconds);
}
}
write-host -ForegroundColor Green ("Domain Total Count: "+$i)
write-host -ForegroundColor Red "Begin Write Domain Data File!"
$DomainDataList | Export-Csv -Path ("AllDomain(" + $file + "-" + $index + ").csv") -NoTypeInformation
write-host -ForegroundColor Red "Get Domain Data Finished!"
$attaches = @()
$attaches += (Get-Location).Path + "AllUser(" + $file + "-" + $index + ").csv"
$attaches += (Get-Location).Path + "AllDomain(" + $file + "-" + $index + ").csv"
SendMail $attaches
write-host -ForegroundColor Red "Send Mail Finished!"
相关文章推荐
- JIRA-如何为所有用户更新发送的邮件格式为HTML
- 每日学习总结:CSS:Clear属性的用法、重复交易提示框、弹出提示框后页面变形、asp.net调用smtp服务发送邮件问题
- 如何把Domino所有用户接收和发送的邮件都自动地拷贝到一个数据库中
- Exchange 2010导出所有用户中最近3个月未发送邮件的账户
- 易宝典文章——玩转Office 365中的Exchange Online服务 之七 怎样新建Exchange Online用户,并实现邮件发送
- 每日学习总结:CSS:Clear属性的用法、重复交易提示框、弹出提示框后页面变形、asp.net调用smtp服务发送邮件问题
- Python通过smtp服务发送电子邮件给指定用户(适用于Zabbix邮件报警)
- 用户发送邮件是zip格式,但是接收方收到时是txt格式
- javamail邮件服务发送总结
- javamail邮件服务发送总结
- 购物网站20:用户/顾客action----用户列表---用户退出---用户管理---用户登录---用户注册---修改密码---发送邮件
- python整理十七——发送邮件
- 从Discuz扣下来的发送邮件代码,整理成类(支持群发)
- 邮件发送API整理
- Oracle 存储过程中发送邮件,并支持用户验证、中文标题和内容
- 使用需要验证用户的smtp服务器发送邮件
- asp.net 2.0 无SMTP服务也能发送邮件
- Asp.net环境下给用户发送邮件
- Oracle 存储过程中发送邮件,并支持用户验证、中文标题和内容
- 在做mes系统时,利用D7 IdMessage&IdSMTP发送邮件,从盒子上学习了不少的东西,自己整理了一下,想share给新手,学习参考!