powershell D:\PS\AccountLockOut\Start-AccountLockOut.ps1
$CredUser="ikulin" #定义用户
$PWD=ConvertTo-SecureString "Iku963" -AsPlainText -Force #定义密码,转换安全字符,强制明文
$Cred=New-Object System.Management.Automation.PSCredential($CredUser,$PWD) #定义认证对象
Invoke-Command -FilePath "D:\PS\AccountLockOut\Get-AccountLockOut.ps1" -ComputerName 10.10.10.10 -Credential $Cred
net use \\DC\D$\PS\AccountLockOut Iku963.. /u:ikulin
robocopy \\DC \D$\PS\AccountLockOut D:\PS\AccountLockOut\LOG
net use \\LinuxSer\ad passwd /u:username
robocopy D:\PS\AccountLockOut\LOG \\LinuxSer\ad
net use /d * /y
[CmdletBinding()]
param(
[INT]$Num=7
)
$After=((Get-Date).adddays(-$Num+1)).ToString('yyyy-MM-dd')
$Before=(Get-Date).ToString('yyyy-MM-dd')
$Filename="D:\PS\AccountLockOut\"+"$After"+'-'+"$Before"+'.csv'
Get-EventLog -LogName Security -After $After -InstanceId 4740 |
select @{Name="USER";Expression={(($_.Message).Split(":"))[8].Trim().Split("")[0]}},
@{Name="TIME";Expression={$_.TimeGenerated}},
@{Name="COMPUTER";Expression={(($_.Message).Split(":"))[10].Trim()}} |
Export-Csv -Encoding UTF8 -path "$Filename" -Force
#!/bin/bash
#Date:2017-09-21
#Version:1.0.0
#Author:linxianyu
#Description:Format out for AD AccountLock.csv
#将锁定次数超过50次的帐号统计并存入变量a
a=$(cut -d ',' -f 1 $@ | sort | uniq -c | sort -n |
awk -F ' ' '{ if ($1>50) print $1,$2 }' |
tr -d '"' )
#打印变量a的内容
#因从变量输入原格式会改变,所以有awk对输出格式化
echo $a|
awk -F ' ' ' BEGIN{printf "%15-s %10-s \n","Statistics","Account";
print "-----------------------------"}
{ for(i=1;i<=NF;i++){if(i%2==1){printf "%-10s \t",$i } else{printf "%-10s\n",$i}}}
END{print "-----------------------------"}'
#将变量a中的帐号筛选并存入变量b
b=$(echo $a | awk -F ' ' '{for(i=1;i<=NF;i++){if(i%2==0){print $i }}}')
#for循环数组变量b中的帐号并再次查找、统计、打印
#若同一帐号在不同pc上登陆则分开打印
for i in ${b[@]};
do
grep "$i" $@ |
cut -d "," -f1,3 |
cut -d ":" -f2 |
sort -t ',' -k2 |
sed -e 's#"##g' -e 's#,#\t#g' |
#清除pcname中与帐号同名的行
grep "^$i" |
uniq -c |
sort -b -k2
done
#!/bin/bash
#Date:20171121
#Version:1.0
#Discription: The creat date for tj.sh
path=/backup/ad
cd $path
#测试文件是否存在
[ -e missionnum ]
if [ $? = 0 ];
then
#查看运行资料
num=$(cat missionnum)
#定义循环4次(周)调用一次脚本
mouth=4
#判断是否满足4周
if [ $num -ne $mouth ];
then
#循环计数+1
echo $[ num += 1 ] > missionnum
else
#定义文件名
filename=$(date +"ad%Y%m%d.txt")
#查找4周内产生的日志并调用执行脚本tj.sh
find -name "2017-*" -mtime -28 | xargs sh tj.sh > $filename
#进行linux to windows文本格式转换
unix2dos $filename
#重置计数
echo 1 > missionnum
fi
else
#若无计数文件则创建(因第一次执行后值因为2所以直接赋值2)
echo 2 > missionnum
fi
08 17 * * 3 sh /backup/ad/mission.sh
#TYPE Selected.System.Diagnostics.EventLogEntry
"USER","TIME","COMPUTER"
"Administrator","2017/11/29 下午 12:14:18","PC1"
"USER1","2017/11/29 下午 12:06:53","PC2"
"USER3","2017/11/29 下午 12:02:35","PC3"
"USER6","2017/11/29 上午 11:53:51","PC9"
"Administrator","2017/11/29 上午 11:48:39","PC2"
[root@bogon ~]# cat /backup/ad/ad20171122.txt
Statistics Account
-----------------------------
60 USER1
161 USER3
482 USER9
971 Administrator
-----------------------------
60 USER1 PC2
100 USER3 PC3
30 USER3 PC4
31 USER3 PC11
209 USER9 PC6
273 USER9 PC100
531 Administrator PC1
440 Administrator PC2