您的位置:首页 > 其它

整理所有用户服务的属性,并发送邮件

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!"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐