• PowerShell 脚本批量检索项目里面的关键词


    PowerShell 版本5.x 系统 wIn10  

    PowerShell Visual Code 环境配置 https://docs.microsoft.com/zh-cn/powershell/scripting/components/vscode/using-vscode?view=powershell-7

    Visual Code 中文乱码解决办法 https://blog.csdn.net/weixin_45033342/article/details/90574434

    直接上代码

    $keyWordList = "md5","download","zip>","rar>","checkupdate","更新","远程","解压","压缩","下载","支付","downurl","ToSelector","performSelector","method_exchangeIm","dlopen","dlsym"
    
    if (Test-Path -Path keyWordPosition.txt)
    {
        Remove-item -Path keyWordPosition.txt
    }
    
    $millisec = [Timespan]::FromMilliseconds([Environment]::TickCount)
    
    Write-Host -NoNewline "start match $(Get-Date)"
    foreach($word in $keyWordList)
    {
        Write-Host -NoNewline " $($word) "
    }
    
    Write-Host
    $totalLineCount = 0
    $totalFileCount = 0
    $totalCheckLine = 0
    Get-ChildItem -Path .sourcecode -Exclude *.a,*.png,*.jpg,*.so,*log,*.obj,*pbxproj,*make,*pch,*filters,*xcuserstate,*vcxproj,*mk,*js,*project,*.py,*lib,*pdb,*dll,*txt,*mp3,*tmx,*mdown,*ttf -File -recurse | ForEach-Object {
        $lines = Get-Content $_.FullName -Encoding UTF8
        $idx = 1
        $keyCount = 0
        foreach($lineInfo in $lines)
        {
            foreach($word in $keyWordList)
            {
                if($lineInfo -match $word)
                {
                    Write-Host "find [$($word)] at line $($idx) in file $($_.Name)"
                    Start-Sleep -Milliseconds 11
                    Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "find [$($word)] at $($idx) in file $($_.Name)"
                    $keyCount ++
                    $totalLineCount ++
                }
                $totalCheckLine ++
            }
            $idx ++
        }
    
        if ( $keyCount -gt 0 )
        {
            Write-Host "$($_.Name) has $($keyCount) sensitive word total $($lines.Count) line"
            $totalFileCount ++
        }
    }
    
    $spendTime = [Timespan]::FromMilliseconds([Environment]::TickCount) - $millisec
    
    $usetime = "total spend time [$($spendTime)] total sensitive count [$($totalLineCount)] total check line [$($totalCheckLine)] total check file $($totalFileCount) "
    Write-Host "$($usetime)"
    Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "$($usetime)"
    
    $ignoretype = "*.a,*.png,*.jpg,*.so,*log,*.obj,*pbxproj,*make,*pch,*filters,*xcuserstate,*vcxproj,*mk,*js,*project,*.py,*lib,*pdb,*dll,*txt,*mp3,*tmx,*mdown,*ttf"
    Write-Host "过滤文件类型 $($ignoretype)"
    Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "过滤文件类型 $($ignoretype)"
    
    $keyStr = ""
    foreach($word in $keyWordList)
    {
        $keyStr = $keyStr + " " + $word
    }
    Write-Host "比对包含的敏感词 $($keyStr)"
    Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "比对包含的敏感词 $($keyStr)"
    Write-Host
    Pause

    运行结果

    当前脚本和被检索的文件夹放在一个目录 会遍历文件夹里面所有的子文件

  • 相关阅读:
    js 闭包
    js 图片放大镜功能
    前端页面优化
    css 背景图片自适应分辨率大小 兼容
    纯css实现箭头
    js 排序算法
    css3 渐变 兼容
    css 兼容性总结
    css3 @keyframe 抖动/变色动画
    关于$.data(element,key,value)与ele.data.(key,value)的区别
  • 原文地址:https://www.cnblogs.com/abelmou/p/12213835.html
Copyright © 2020-2023  润新知