• 该文件用于打包成主程序 - USBLogSrv.exe
    1. # 获取通过USB总线连接的所有移动盘
    2. # Get-Disk | Where-Object {$_.Bustype -eq "USB"}
    3. # 获取磁盘号
    4. # $disk_num = (Get-Disk | Where-Object {$_.Bustype -eq "USB"}).number
    5. # 查看值的数据类型
    6. # echo $disk_num.GetType()
    7. # 获取移动盘盘符
    8. function GetDrive {
    9. # 获取磁盘号
    10. $num = (Get-Disk | Where-Object {$_.Bustype -eq "USB"}).number
    11. # 磁盘信息
    12. # ($num | ForEach-Object{(Get-Disk -Number $_) |Format-list number,FriendlyName,SerialNumber,@{name='Size(GB)';expression={$_.Size / 1GB -as [int]}}} | Out-String).Trim()
    13. if($num.count -ne 0){
    14. # 获取对应的盘符
    15. $drive = $num | ForEach-Object{((Get-Partition -DiskNumber $_) | ?{!$_.IsHidden}).DriveLetter}
    16. return $drive
    17. }
    18. }
    19. # 获取移动盘分区剩余大小
    20. function DriveSize {
    21. $drv = GetDrive
    22. if($drv.count -ne 0){
    23. $disk_remain_size = (Get-Volume -DriveLetter $drv).SizeRemaining
    24. return $disk_remain_size
    25. }
    26. }
    27. # 获取本机物理网卡IP
    28. function ip{
    29. $macadd = (Get-NetAdapter -Physical | ? Status -EQ "Up").MacAddress
    30. $address = foreach($address in (ipconfig /all) -like '*地址*') { ($address -split ' : ')[-1]}
    31. # $macadd使用ForEach-Object,是考虑到PC有多个物理网卡
    32. $macadd | ForEach-Object{
    33. # 将$_赋值给$macadd, 是为了和$address的$_做区分
    34. $macadd = $_
    35. $address | ForEach-Object{
    36. if($_ -like '*首选*'){
    37. $index = [array]::IndexOf($address, $_)
    38. $host_mac = $address[$($index-1)]
    39. if($host_mac -eq $macadd){
    40. $ip = ($_ -split '\(')[0]
    41. return $ip
    42. }
    43. }
    44. }
    45. }
    46. }
    47. # 钉钉报警
    48. function ddalert($ipfun, $info){
    49. $time = (Get-Date).ToShortTimeString()
    50. # 接收定义的ip函数作为参数传入
    51. $ip = Invoke-Command $ipfun
    52. #在15:41过去5分钟内,主机DN2D-00088,IP地址10.2.234.13,拷贝 50.91 MB大小数据到 F 移动盘
    53. $content = $time + " 主机" + $env:COMPUTERNAME + ",IP地址" + $ip + "," + $info
    54. $par = @{
    55. msgtype = "text";
    56. text = @{
    57. content = $content;
    58. };
    59. }
    60. $parjson = $par | ConvertTo-Json
    61. # 内网告警群
    62. #$ddhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx"
    63. # 深圳告警群
    64. $ddhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
    65. # 使Json支持中文
    66. $PostData = [System.Text.Encoding]::UTF8.GetBytes($parjson)
    67. $Header = @{"Content-Type" = "application/json;charset=utf-8"}
    68. # 参数UseBasicParsing对URI只进行简单解析,可提高效率
    69. Invoke-WebRequest -Uri $ddhook -Method Post -Body $PostData -Headers $Header -UseBasicParsing | Out-Null
    70. # 输出日志到本地
    71. msg $content
    72. }
    73. # 输出日志信息到本地
    74. function msg($message){
    75. $date = (Get-Date).GetDateTimeFormats()[1]
    76. $folder = Test-Path $env:PUBLIC\USBLogs
    77. if($folder -eq $false){
    78. New-Item -Path "$env:PUBLIC\" -Name "USBLogs" -ItemType "directory" | Out-Null
    79. }
    80. $message >> $env:PUBLIC\USBLogs\$date.log
    81. }
    82. while(1){
    83. if(GetDrive){
    84. $before = DriveSize
    85. # 间隔1分钟检测一次
    86. sleep 60
    87. $after = DriveSize
    88. # 判断是否拷贝数据
    89. $len = $after.Count
    90. for($i=0;$i -lt $len;$i++){
    91. $b = $before[$i]
    92. $a = $after[$i]
    93. if($a -lt $b){
    94. # {0:N2}保留两位小数点
    95. $filesize = "{0:N2}" -f $(($b - $a) / 1MB)
    96. # 获取拷贝数据的移动盘具体分区盘符
    97. $drive = (Get-Volume (GetDrive) | ?{$_.SizeRemaining -eq $a}).DriveLetter
    98. $info = "拷贝 $filesize MB大小数据到 $drive 移动盘"
    99. #$message = $time + " 主机" + $env:COMPUTERNAME + "IP地址" + (ip) + "" + $info
    100. # 输出日志到钉钉机器人( 将${function:ip}函数ip作为参数,传递给函数ddalert )
    101. ddalert ${function:ip} $info
    102. #ddalert $message $info
    103. $before = $after
    104. }elseif($a -gt $b){
    105. $before = $after
    106. }
    107. }
    108. }
    109. $time = (Get-Date).ToShortTimeString()
    110. if($time -eq "12:30"){
    111. $path = "$env:PUBLIC\USBLogSrv"
    112. # update.ps1存放路径
    113. powershell -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file $path\update.ps1' -verb RunAs -WindowStyle Hidden}"
    114. # 若未sleep,且在当前时间内没有插入移动盘,会持续执行update,以产生多个powershell进程
    115. sleep 60
    116. }
    117. }