-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750
需求:根据要求对集团AD域及下属3个子域的所有AD账号进行统计,查询所有锁定的账号,解锁,并每天早上发送邮件到指向邮箱。
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
92
93
94
95
96
97
98
99
100
101
102
103
|
Import-Module activedirectory
$yuntcloud_Lockeduser = Search-ADAccount -LockedOut -SearchBase
"dc=yuntcloud,dc=com"
-server dc01 |
select
name, samaccountname
$aaa_Lockeduser = Search-ADAccount -LockedOut -SearchBase
"dc=aaa,dc=yuntcloud,dc=com"
-server aaaDC1 |
select
name, samaccountname
$bbb_Lockeduser = Search-ADAccount -LockedOut -SearchBase
"dc=bbb,dc=yuntcloud,dc=com"
-server bbbdc1 |
select
name, samaccountname
$ccc_Lockeduser = Search-ADAccount -LockedOut -SearchBase
"dc=ccc,dc=yuntcloud,dc=com"
-server cccDC1 |
select
name, samaccountname
#send email
$UserName =
"test01@yuntcloud.com"
#定义管理员账户名称
$Password = ConvertTo-SecureString
"Djds123"
-AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$nFrom =
"test01@yuntcloud.com"
$nTo =
"zhouping@yuntcloud.com"
$lockeduser = @()
#Unlock yuntcloud Locked user
if
([
string
]::IsNullOrEmpty($($yuntcloud_Lockeduser|%{$_.name})))
{
Write-Host
"yuntcloud no locked user"
Start-Sleep 5
Send-MailMessage -From $nFrom -To $nTo -Subject
"yuntcloud no locked user"
-Body
"yuntcloud no locked user"
-Credential $cred -SmtpServer
"mail.yuntcloud.com"
-Encoding ([System.Text.Encoding]::UTF8)
}
else
{
Write-Host
"yuntcloud Locked user"
foreach
($i
in
$yuntcloud_Lockeduser)
{
Write-Host $i.name
$lockeduser += $i
}
Start-Sleep 5
Search-ADAccount -LockedOut -SearchBase
"dc=yuntcloud,dc=com"
-server dc01 | Unlock-ADAccount
Write-Host
"yuntcloud all locked account were unlocked"
$lockeduser | Export-Csv -Path c:\yuntcloud_lockeduser.csv -NoTypeInformation -Encoding utf8
Send-MailMessage -From $nfrom -To $nto -Subject
"yuntcloud locked user"
-Body
"yuntcloud locked user"
-Attachments
"c:\yuntcloud_lockeduser.csv"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
Start-Sleep 3
}
#Unlock aaa.yuntcloud.com Locked user
$lockeduser = @()
if
([
string
]::IsNullOrEmpty($($aaa_Lockeduser|%{$_.name})))
{
Write-Host
"aaa no locked user"
Start-Sleep 5
Send-MailMessage -From $nfrom -To $nto -Subject
"aaa no locked user"
-Body
"aaa no locked user"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
}
else
{
Write-Host
"aaa Locked user"
foreach
($i
in
$aaa_Lockeduser)
{
Write-Host $i.name
$lockeduser += $i
}
Start-Sleep 5
Search-ADAccount -LockedOut -SearchBase
"dc=aaa,dc=yuntcloud,dc=com"
-server aaaDC1 | Unlock-ADAccount
Write-Host
"aaa all locked account were unlocked"
$lockeduser | Export-Csv -Path c:\aaa_lockeduser.csv -NoTypeInformation -Encoding utf8
Send-MailMessage -From $nfrom -To $nto -Subject
"aaa locked user"
-Body
"aaa locked user"
-Attachments
"c:\aaa_lockeduser.csv"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
Start-Sleep 3
}
#Unlock bbb.yuntcloud.com Locked user
$lockeduser = @()
if
([
string
]::IsNullOrEmpty($($bbb_Lockeduser|%{$_.name})))
{
Write-Host
"bbb no locked user"
Start-Sleep 5
Send-MailMessage -From $nfrom -To $nto -Subject
"bbb no locked user"
-Body
"bbb no locked user"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
}
else
{
Write-Host
"bbb Locked user"
foreach
($i
in
$bbb_Lockeduser)
{
Write-Host $i.name
$lockeduser += $i
}
Start-Sleep 5
Search-ADAccount -LockedOut -SearchBase
"dc=bbb,dc=yuntcloud,dc=com"
-server bbbdc1 | Unlock-ADAccount
Write-Host
"bbb all locked account were unlocked"
$lockeduser | Export-Csv -Path c:\bbb_lockeduser.csv -NoTypeInformation -Encoding utf8
Send-MailMessage -From $nfrom -To $nto -Subject
"bbb locked user"
-Body
"bbb locked user"
-Attachments
"c:\bbb_lockeduser.csv"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
Start-Sleep 3
}
#Unlock ccc.yuntcloud.com Locked user
$lockeduser = @()
if
([
string
]::IsNullOrEmpty($($ccc_Lockeduser|%{$_.name})))
{
Write-Host
"ccc no locked user"
Start-Sleep 5
Send-MailMessage -From $nfrom -To $nto -Subject
"ccc no locked user"
-Body
"ccc no locked user"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
}
else
{
Write-Host
"ccc Locked user"
foreach
($i
in
$ccc_Lockeduser)
{
Write-Host $i.name
$lockeduser += $i
}
Start-Sleep 5
Search-ADAccount -LockedOut -SearchBase
"dc=ccc,dc=yuntcloud,dc=com"
-server cccDC1 | Unlock-ADAccount
Write-Host
"ccc all locked account were unlocked"
$lockeduser | Export-Csv -Path c:\ccc_lockeduser.csv -NoTypeInformation -Encoding utf8
Send-MailMessage -From $nfrom -To $nto -Subject
"ccc locked user"
-Body
"ccc locked user"
-Attachments
"c:\ccc_lockeduser.csv"
-Credential $cred -SmtpServer mail.yuntcloud.com -Encoding ([System.Text.Encoding]::UTF8)
Start-Sleep 3
}
|
本文转自 zhou_ping 51CTO博客,原文链接:http://blog.51cto.com/yuntcloud/1945726,如需转载请自行联系原作者