1、數據處理過程
- 在WinSer執行排程腳本遠程控制DC做業,導出7天內賬號的鎖定EventLog;
- 經過WinSer中轉至LinuxSer;
- 在LinuxSer執行排程對數據進行格式化,過濾出(50次/月)的數據。
- 經過訪問訪問\\WinSer可訪問共享,(每28天的星期三)拿處處理過的文件(ad20170101.txt..)
![統計頻繁被鎖定的AD賬號](http://static.javashuo.com/static/loading.gif)
2、腳本
2.1 WinSer服務器
- 腳本一:
因Windows默認不容許直接在排程運行powshell腳本(服務器有更多限制),因此選擇運行Bat腳本調用運行。
powershell D:\PS\AccountLockOut\Start-AccountLockOut.ps1
- 腳本二:
發送本地腳本文件到DC上執行(便於管理)。
$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
- 腳本三:
讀取7天內賬號的鎖定日誌並導出csv文件。
[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
2.2 LinuxSer服務器
#!/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
3、文件
#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