This is a simple project in PowerShell which can scan through your source code to search for a specific text in each source file and shows you the results (in console and a text file report).
The biggest use case for me was to know if my project source code is IPR (Intellectual Property Rights) compliant. IPR header is a proprietary text put on top of each source file in a project (as a comment). If any of the source code file is not IPR compliant, the build pipeline should fail and tell the list of files which are not IPR compliant.
This PowerShell script can be used as a task in DevOps pipeline to scan through the source code and search for a specific text (IPR header in my case) in each file. You can use this script for other similar requirements.
- Source Code Path: Your source code path to scan. Eg. "D:\SourceCode\ProjectABC". Use "$(System.DefaultWorkingDirectory)" if running the script as a task in DevOps pipeline
- Text: Text (part or full) to search for in your source code. Text should be a single line. Eg. "COMPANY XYZ PROPRIETARY. This document and its accompanying elements, contain information which is proprietary and confidential"
- Files to Include: Extension of source code files to consider for scanning. Eg. "*.cs, *.ts, *.html, *.htm"
- Folders to Exclude: Folders to skip from scanning. Eg. "bin, obj, node_modules, dist"
- Output File: Path to a file to report the search results. Eg. "D:\SourceCode\ProjectABC\IPRReport.txt". Make use of "$(Build.ArtifactStagingDirectory)" if running the script as a task in DevOps pipeline. Eg. "$(Build.ArtifactStagingDirectory)/IPRReport.txt"
Refer SearchSourceCode.ps1 from the repository
I am going to use this script in my project to find the source code files with missing IPR header.
Use below command to run the script
Output
This is how the IPR report will look like:
- Add a PowerShell task in your pipeline
- Copy the PowerShell script (Inline). Specify/modify input values
- Add below code if you want to fail the pipeline in case there are files with missing IPR header
if($fileCountWithoutText -gt 0) { exit 1 }
-
Don't forget to publish the report as a pipeline artifact in a separate task (or as a part of an existing publish artifacts task in your pipeline)
-
Run the pipeline
Output
Pipeline task output:
There are no files with missing IPR header in my source code.
Report in pipeline artifacts: