[] PowerShell -. III: |
1. $cur = Get-Date
2. $Global:Count=0
3. $Global:baseline = @{"Monday" = @(3,8,5); "Tuesday" = @(4,10,7);"Wednesday" = @(4,4,4);"Thursday" = @(7,12,4); "Friday" = @(5,4,6); "Saturday"=@(2,1,1); "Sunday"= @(2,4,2)}
4. $Global:cnts = @(0,0,0)
5. $Global:burst = $false
6. $Global:evarray = New-Object System.Collections.ArrayList
7.
8. $action = {
9. $Global:Count++
10. $d=(Get-Date).DayofWeek
11. $i= [math]::floor((Get-Date).Hour/8)
12.
13. $Global:cnts[$i]++
14.
15. #event auditing!
16.
17. $rawtime = $EventArgs.NewEvent.TargetInstance.LastAccessed.Substring(0,12)
18. $filename = $EventArgs.NewEvent.TargetInstance.Name
19. $etime= [datetime]::ParseExact($rawtime,"yyyyMMddHHmm",$null)
20.
21. $msg="$($etime)): Access of file $($filename)"
22. $msg|Out-File C:\Users\bob\Documents\events.log -Append
23.
24.
25. $Global:evarray.Add(@($filename,$etime))
26. if(!$Global:burst) {
27. $Global:start=$etime
28. $Global:burst=$true
29. }
30. else {
31. if($Global:start.AddMinutes(15) -gt $etime ) {
32. $Global:Count++
33. #File behavior analytics
34. $sfactor=2*[math]::sqrt( $Global:baseline["$($d)"][$i])
35.
36. if ($Global:Count -gt $Global:baseline["$($d)"][$i] + 2*$sfactor) {
37.
38.
39. "$($etime): Burst of $($Global:Count) accesses"| Out-File C:\Users\bob\Documents\events.log -Append
40. $Global:Count=0
41. $Global:burst =$false
42. New-Event -SourceIdentifier Bursts -MessageData "We're in Trouble" -EventArguments $Global:evarray
43. $Global:evarray= [System.Collections.ArrayList] @();
44. }
45. }
46. else { $Global:burst =$false; $Global:Count=0; $Global:evarray= [System.Collections.ArrayList] @();}
47. }
48. }
49.
50. Register-WmiEvent -Query "SELECT * FROM __InstanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'CIM_DataFile' and TargetInstance.Path = '\\Users\\bob\' and targetInstance.Drive = 'C:' and (targetInstance.Extension = 'txt' or targetInstance.Extension = 'doc' or targetInstance.Extension = 'rtf') and targetInstance.LastAccessed > '$($cur)' " -sourceIdentifier "Accessor" -Action $action
51.
52.
53. #Dashboard
54. While ($true) {
55. $args=Wait-Event -SourceIdentifier Bursts # wait on Burst event
56. Remove-Event -SourceIdentifier Bursts #remove event
57.
58. $outarray=@()
59. foreach ($result in $args.SourceArgs) {
60. $obj = New-Object System.Object
61. $obj | Add-Member -type NoteProperty -Name File -Value $result[0]
62. $obj | Add-Member -type NoteProperty -Name Time -Value $result[1]
63. $outarray += $obj
64. }
65.
66.
67. $outarray|Out-GridView -Title "FAA Dashboard: Burst Data"
68. }
1. $Get-WmiObject -Query "SELECT * From CIM_DataFile where Path = '\\Users\\bob\' and Drive = 'C:' and (Extension = 'txt' or Extension = 'doc' or Extension = 'rtf')"
1. $Action = {
2.
3. Param (
4.
5. [string] $Name
6.
7. )
8.
9. $classify =@{"Top Secret"=[regex]'[tT]op [sS]ecret'; "Sensitive"=[regex]'([Cc]onfidential)|([sS]nowflake)'; "Numbers"=[regex]'[0-9]{3}-[0-9]{2}-[0-9]{3}' }
10.
11.
12. $data = Get-Content $Name
13.
14. $cnts= @()
15.
16. foreach ($key in $classify.Keys) {
17.
18. $m=$classify[$key].matches($data)
19.
20. if($m.Count -gt 0) {
21.
22. $cnts+= @($key,$m.Count)
23. }
24. }
25.
26. $cnts
27. }
1. $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, 5)
2.
3. $RunspacePool.Open()
4.
5. $Tasks = @()
6.
7.
8. foreach ($item in $list) {
9.
10. $Task = [powershell]::Create().AddScript($Action).AddArgument($item.Name)
11.
12. $Task.RunspacePool = $RunspacePool
13.
14. $status= $Task.BeginInvoke()
15.
16. $Tasks += @($status,$Task,$item.Name)
17. }