Powershell管理系列(三十)PowerShell操作之统计邮箱的用户信息

简介:

用户需要统计邮箱用户的具体信息,如登陆名,邮箱地址,公司名,部门等,这些信息可以通过不通的命令查询到,我们如何通过脚本把这些信息汇总到一起,命令如下:

步骤1、在powershell命令行输入如下命令

Add-PSSnapin microsoft.exchange*
$user=Get-User -ResultSize unlimited -RecipientTypeDetails UserMailbox
$userinfo=@()
foreach($i in $user)
{
$mbxstatistics=Get-Mailbox $i.identity|Get-MailboxStatistics
$mbxsarctatistics=if((Get-Mailbox $i.identity).ArchiveDatabase -ne $null) `
{get-mailbox $i.identity -Archive|Get-MailboxStatistics -Archive}
#获取所有启用了存档邮箱的用户信息,如前面不加if判断,会出现运行时提示找不到存档邮箱的报错。
$mbxtotal=Get-Mailbox $i.identity|Select-Object @{n="显示名";e={$_.displayname}},`
@{n="登录名";e={$_.samaccountname}}, `
@{n="邮箱地址";e={$_.PrimarySmtpAddress}}, `
@{n="公司名";e={$i.company}}, `
@{n="部门";e={$i.Department}}, `
@{n="邮箱数量";e={$mbxstatistics.ItemCount}},`
@{n="邮箱大小(MB)";e={$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n="存档邮箱数量";e={$mbxsarctatistics.ItemCount}},`
@{n="存档邮箱大小(MB)";e={$mbxsarctatistics.TotalItemSize.value.tomb()}},`
@{n="挂载的服务器名";e={$mbxstatistics.ServerName}},`
@{n="最后一次登录时间";e={$mbxstatistics.LastLogonTime}},`
@{n="数据库名";e={$mbxstatistics.DatabaseName}}
$userinfo+=$mbxtotal #把每次运行获取到的用户信息存入到userinfo
}
$userinfo #把查询到的信息在屏幕显示出来
$userinfo|Export-Csv -Path c:\mbxinfo.csv -NoTypeInformation -Encoding utf8 #把用户信息导出到c:\mbxinfo.csv

wKioL1biqKyQyPThAAFyjwXnjG8741.png

步骤2、导出的信息如下截图,至此,我们需要的信息就已经全部查询并导出到csv表格

wKiom1bhoLPSzgIJAAJF5JEJzlo318.png

思路二:我们可以继续优化,获取更多的信息,比如手机,座机等,还有获取邮箱的使用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Add-PSSnapin microsoft.exchange*
$user=Get-User -ResultSize unlimited -RecipientTypeDetails UserMailbox
$userinfo=@()
foreach ($i  in  $user)
{
$mbxstatistics=Get-Mailbox $i.identity|Get-MailboxStatistics
$mbxsarctatistics= if ((Get-Mailbox $i.identity).ArchiveDatabase -ne $ null ) `
{ get -mailbox $i.identity -Archive|Get-MailboxStatistics -Archive}
#获取所有启用了存档邮箱的用户信息,如前面不加if判断,会出现运行时提示找不到存档邮箱的报错。
if ((Get-Mailbox $i.identity).UseDatabaseQuotaDefaults) `
{
$mbxtotal=Get-Mailbox $i.identity|Select-Object @{n= "显示名" ;e={$_.displayname}},`
@{n= "登录名" ;e={$_.samaccountname}}, `
@{n= "邮箱地址" ;e={$_.PrimarySmtpAddress}}, `
@{n= "公司名" ;e={$i.company}}, `
@{n= "部门" ;e={$i.Department}}, `
@{n= "邮件数量" ;e={$mbxstatistics.ItemCount}},`
@{n= "已用空间(MB)" ;e={$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "剩余空间(MB)" ;e={$mbxstatistics.DatabaseProhibitSendQuota.value.tomb()-$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "邮箱配额(MB)" ;e={$mbxstatistics.DatabaseProhibitSendQuota.value.tomb()}},`
@{n= "存档邮箱数量" ;e={$mbxsarctatistics.ItemCount}},`
@{n= "存档邮箱大小(MB)" ;e={$mbxsarctatistics.TotalItemSize.value.tomb()}},`
@{n= "挂载的服务器名" ;e={$mbxstatistics.ServerName}},`
@{n= "最后一次登录时间" ;e={$mbxstatistics.LastLogonTime}},`
@{n= "数据库名" ;e={$mbxstatistics.DatabaseName}},`
@{n= "手机号码" ;e={(Get-User $i.Identity).MobilePhone}},`
@{n= "办公室" ;e={(Get-User $i.Identity).office}},`
@{n= "工作电话" ;e={(Get-User $i.Identity).Phone}},`
@{n= "职务" ;e={(Get-User $i.Identity).title}}
}
else
{
$mbxtotal=Get-Mailbox $i.identity|Select-Object @{n= "显示名" ;e={$_.displayname}},`
@{n= "登录名" ;e={$_.samaccountname}}, `
@{n= "邮箱地址" ;e={$_.PrimarySmtpAddress}}, `
@{n= "公司名" ;e={$i.company}}, `
@{n= "部门" ;e={$i.Department}}, `
@{n= "邮件数量" ;e={$mbxstatistics.ItemCount}},`
@{n= "已用空间(MB)" ;e={$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "剩余空间(MB)" ;e={( get -mailbox $i.identity).ProhibitSendQuota.value.tomb()-$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "邮箱配额(MB)" ;e={( get -mailbox $i.identity).ProhibitSendQuota.value.tomb()}},`
@{n= "存档邮箱数量" ;e={$mbxsarctatistics.ItemCount}},`
@{n= "存档邮箱大小(MB)" ;e={$mbxsarctatistics.TotalItemSize.value.tomb()}},`
@{n= "挂载的服务器名" ;e={$mbxstatistics.ServerName}},`
@{n= "最后一次登录时间" ;e={$mbxstatistics.LastLogonTime}},`
@{n= "数据库名" ;e={$mbxstatistics.DatabaseName}},`
@{n= "手机号码" ;e={(Get-User $i.Identity).MobilePhone}},`
@{n= "办公室" ;e={(Get-User $i.Identity).office}},`
@{n= "工作电话" ;e={(Get-User $i.Identity).Phone}},`
@{n= "职务" ;e={(Get-User $i.Identity).title}}
}
$userinfo+=$mbxtotal #把每次运行获取到的用户信息存入到userinfo
}
$userinfo #把查询到的信息在屏幕显示出来
$userinfo|Export-Csv -Path c:\mbxinfo.csv -NoTypeInformation -Encoding utf8 #把用户信息导出到c:\mbxinfo.csv
$UserName =       #定义发送账户名称
$Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
Send-MailMessage -From  -To  -Subject  "全员邮箱使用情况汇总"  -Credential $cred -SmtpServer  "mail.yuntcloud.com"  -Attachments  "c:\mbxinfo.csv"  -Encoding ([System.Text.Encoding]::UTF8)

思路三、统计邮箱情况并将报警信息发送到用户及管理员组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Add-PSSnapin microsoft.exchange*
$user=Get-User -ResultSize unlimited -RecipientTypeDetails UserMailbox
$userinfo=@()
foreach ($i  in  $user)
{
$mbxstatistics=Get-Mailbox $i.identity|Get-MailboxStatistics
$mbxsarctatistics= if ((Get-Mailbox $i.identity).ArchiveDatabase -ne $ null ) `
{ get -mailbox $i.identity -Archive|Get-MailboxStatistics -Archive}
#获取所有启用了存档邮箱的用户信息,如前面不加if判断,会出现运行时提示找不到存档邮箱的报错。
if ((Get-Mailbox $i.identity).UseDatabaseQuotaDefaults) `
{
$mbxtotal=Get-Mailbox $i.identity|Select-Object @{n= "显示名" ;e={$_.displayname}},`
@{n= "登录名" ;e={$_.samaccountname}}, `
@{n= "邮箱地址" ;e={$_.PrimarySmtpAddress}}, `
@{n= "公司名" ;e={$i.company}}, `
@{n= "部门" ;e={$i.Department}}, `
@{n= "邮件数量" ;e={$mbxstatistics.ItemCount}},`
@{n= "已用空间(MB)" ;e={$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "剩余空间(MB)" ;e={$mbxstatistics.DatabaseProhibitSendQuota.value.tomb()-$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "邮箱配额(MB)" ;e={$mbxstatistics.DatabaseProhibitSendQuota.value.tomb()}},`
@{n= "存档邮箱数量" ;e={$mbxsarctatistics.ItemCount}},`
@{n= "存档邮箱大小(MB)" ;e={$mbxsarctatistics.TotalItemSize.value.tomb()}},`
@{n= "挂载的服务器名" ;e={$mbxstatistics.ServerName}},`
@{n= "最后一次登录时间" ;e={$mbxstatistics.LastLogonTime}},`
@{n= "数据库名" ;e={$mbxstatistics.DatabaseName}},`
@{n= "手机号码" ;e={(Get-User $i.Identity).MobilePhone}},`
@{n= "办公室" ;e={(Get-User $i.Identity).office}},`
@{n= "工作电话" ;e={(Get-User $i.Identity).Phone}},`
@{n= "职务" ;e={(Get-User $i.Identity).title}}
   if ( "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -ge 0.1 -and  "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -lt 0.2) `
   {
    $UserName =  "test@yuntcloud.com"       #定义发送账户名称
    $Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
    Send-MailMessage -From  "test@yuntcloud.com"  -to (Get-Mailbox $i.identity).PrimarySmtpAddress -Cc  "mis@yuntcloud.com"  -Subject ($i.DisplayName+ "的邮箱剩余空间不足20%,请清理您的邮箱,否则将可能无法发送邮件" ) -Credential $cred `
    -SmtpServer  "mail.yuntcloud.com"  -BodyAsHtml ($i.DisplayName+ "的邮箱剩余空间不足20%,请清理您的邮箱,否则将可能无法发送邮件" ) -Encoding ([System.Text.Encoding]::UTF8)
   }
   elseif( "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -lt 0.1) `
   {
    $UserName =  "test@yuntcloud.com"       #定义发送账户名称
    $Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
    Send-MailMessage -From  "test@yuntcloud.com"  -to (Get-Mailbox $i.identity).PrimarySmtpAddress -Cc  "mis@yuntcloud.com"  -Subject ($i.DisplayName+ "的邮箱剩余空间不足10%,请清理您的邮箱,否则将可能无法收发邮件" ) -Credential $cred `
    -SmtpServer  "mail.yuntcloud.com"  -BodyAsHtml ($i.DisplayName+ "的邮箱剩余空间不足10%,请清理您的邮箱,否则将可能无法收发邮件" ) -Encoding ([System.Text.Encoding]::UTF8)
   }
}
else
{
$mbxtotal=Get-Mailbox $i.identity|Select-Object @{n= "显示名" ;e={$_.displayname}},`
@{n= "登录名" ;e={$_.samaccountname}}, `
@{n= "邮箱地址" ;e={$_.PrimarySmtpAddress}}, `
@{n= "公司名" ;e={$i.company}}, `
@{n= "部门" ;e={$i.Department}}, `
@{n= "邮件数量" ;e={$mbxstatistics.ItemCount}},`
@{n= "已用空间(MB)" ;e={$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "剩余空间(MB)" ;e={( get -mailbox $i.identity).ProhibitSendQuota.value.tomb()-$mbxstatistics.TotalItemSize.value.tomb()}},`
@{n= "邮箱配额(MB)" ;e={( get -mailbox $i.identity).ProhibitSendQuota.value.tomb()}},`
@{n= "存档邮箱数量" ;e={$mbxsarctatistics.ItemCount}},`
@{n= "存档邮箱大小(MB)" ;e={$mbxsarctatistics.TotalItemSize.value.tomb()}},`
@{n= "挂载的服务器名" ;e={$mbxstatistics.ServerName}},`
@{n= "最后一次登录时间" ;e={$mbxstatistics.LastLogonTime}},`
@{n= "数据库名" ;e={$mbxstatistics.DatabaseName}},`
@{n= "手机号码" ;e={(Get-User $i.Identity).MobilePhone}},`
@{n= "办公室" ;e={(Get-User $i.Identity).office}},`
@{n= "工作电话" ;e={(Get-User $i.Identity).Phone}},`
@{n= "职务" ;e={(Get-User $i.Identity).title}}
if ( "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -ge 0.1 -and  "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -lt 0.2) `
   {
    $UserName =  "test@yuntcloud.com"       #定义发送账户名称
    $Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
    Send-MailMessage -From  "test@yuntcloud.com"  -to (Get-Mailbox $i.identity).PrimarySmtpAddress -Cc  "mis@yuntcloud.com"  -Subject ($i.DisplayName+ "的邮箱剩余空间不足20%,请清理您的邮箱,否则将可能无法发送邮件" ) -Credential $cred `
    -SmtpServer  "mail.yuntcloud.com"  -BodyAsHtml ($i.DisplayName+ "的邮箱剩余空间不足20%,请清理您的邮箱,否则将可能无法发送邮件" ) -Encoding ([System.Text.Encoding]::UTF8)
   }
   elseif( "{0:n0}"  -F $mbxtotal. "剩余空间(MB)" /$mbxtotal. "邮箱配额(MB)"  -lt 0.1) `
   {
    $UserName =  "test@yuntcloud.com"       #定义发送账户名称
    $Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
    Send-MailMessage -From  "test@yuntcloud.com"  -to (Get-Mailbox $i.identity).PrimarySmtpAddress -Cc  "mis@yuntcloud.com"  -Subject ($i.DisplayName+ "的邮箱剩余空间不足10%,请清理您的邮箱,否则将可能无法收发邮件" ) -Credential $cred `
    -SmtpServer  "mail.yuntcloud.com"  -BodyAsHtml ($i.DisplayName+ "的邮箱剩余空间不足10%,请清理您的邮箱,否则将可能无法收发邮件" ) -Encoding ([System.Text.Encoding]::UTF8)
   }
}
$userinfo+=$mbxtotal #把每次运行获取到的用户信息存入到userinfo
}
$userinfo #把查询到的信息在屏幕显示出来
$userinfo|Export-Csv -Path c:\mbxinfo.csv -NoTypeInformation -Encoding utf8 #把用户信息导出到c:\mbxinfo.csv
$UserName =  "test@yuntcloud.com"       #定义发送账户名称
$Password = ConvertTo-SecureString  "Aa12345678"  -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 
Send-MailMessage -From  "test@yuntcloud.com"  -To  "mis@yuntcloud.com"  -Subject  "全员邮箱使用情况汇总"  -Credential $cred -SmtpServer  "mail.yuntcloud.com"  -Attachments  "c:\mbxinfo.csv"  -Encoding ([System.Text.Encoding]::UTF8)



本文转自 zhou_ping 51CTO博客,原文链接:http://blog.51cto.com/yuntcloud/1749788,如需转载请自行联系原作者

相关文章
|
弹性计算 关系型数据库 数据库
使用ROS部署ECS+SqlServer资源并通过PowerShell操作数据库
本文介绍如何通过ROS部署ECS+RDS+SqlServer资源,以及如何通过PowerShell操作SqlServer数据库。 注意:附录模板和截图中的commandtext部分仅为演示作用,请根据需求修改后使用。
114 0
使用ROS部署ECS+SqlServer资源并通过PowerShell操作数据库
|
数据安全/隐私保护
|
3月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
51 0