zoukankan      html  css  js  c++  java
  • 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

    运行结果

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

  • 相关阅读:
    git Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
    git error: failed to push some refs to 'git@github.com:xxx/xxx.git'
    git LF will be replaced by CRLF in hellogit.txt
    为什么要买保险,并且如何配置保险,以及家庭保险的配置
    Molile App(HTTP/HTML)—Analyze Traffic
    Molile App(HTTP/HTML)—Record and Analyze Traffic
    清空KindEditor富文本编辑器里面的内容方法
    图片上传和显示——上传图片——上传文件)==ZJ
    页面静态化实现——根据模板动态创建静态页
    通过Ajax实现增删改查
  • 原文地址:https://www.cnblogs.com/abelmou/p/12213835.html
Copyright © 2011-2022 走看看