Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. Function Search-PSScripts{
  2. <#
  3. .SYNOPSIS
  4. Use to search the text inside PowerShell scripts for a particular string
  5.  
  6. .PARAMETER SearchString
  7. The string to search for inside the script file
  8.  
  9. .PARAMETER Path
  10. The folder path to search for PowerShell files in. Default to userprofile if not specified.
  11.  
  12. .PARAMETER Recurse
  13. Indicates that this function gets the items in the specified locations and in all child items of the locations.
  14.  
  15. .EXAMPLE
  16. Search-PSScripts -searchString "Get-Help" -recurse
  17.  
  18. Description
  19. -----------
  20. This command searches all of the script files in the user's profile path and its subdirectories.
  21.  
  22. .EXAMPLE
  23. Search-PSScripts -searchString "Invoke-WebRequest" -path 'C:\Scripts' -recurse
  24.  
  25. Description
  26. -----------
  27. This command searches all of the script files in the current directory and its subdirectories.
  28.  
  29. .EXAMPLE
  30. Search-PSScripts -searchString "Invoke-WebRequest" -path 'C:\Scripts'
  31.  
  32. Description
  33. -----------
  34. This command searches only the script files in the current directory.
  35.  
  36.  
  37. #>
  38. [cmdletbinding()]
  39. param(
  40. [Parameter(Mandatory=$true)]
  41. [string]$SearchString,
  42. [Parameter(Mandatory=$false)]
  43. [string]$Path = $env:USERPROFILE,
  44. [Parameter(Mandatory=$false)]
  45. [switch]$Recurse
  46. )
  47.  
  48. $filter = "*.ps1","*.psm1"
  49.  
  50. # Confirm path is valid
  51. if(!(Test-Path $Path)){
  52. throw "'$Path' is not a valid folder or is not accessible."
  53. }
  54.  
  55. # Get the name of this script to exclude it
  56. $Invocation = (Get-Variable MyInvocation -Scope 1).Value;
  57.  
  58.  
  59. $progressParam = @{
  60. Activity = "Search for PowerShell Script in $Path"
  61. Status = "Depending on the number of scripts this may take some time"
  62. PercentComplete = 0
  63. id = 1
  64. }
  65. Write-Progress @progressParam
  66.  
  67. # Get all files in the path
  68. if($Recurse){
  69. $fileList = Get-ChildItem $Path -Recurse -include $filter -Exclude $Invocation.MyCommand -File
  70. } else {
  71. $Path = (Join-Path $Path '*.*')
  72. $fileList = Get-ChildItem $Path -include $filter -Exclude $Invocation.MyCommand -File
  73. }
  74.  
  75. [System.Collections.Generic.List[PSObject]] $results = @()
  76. $progress=1
  77. # Check each file for the string pattern
  78. Foreach($file in $fileList){
  79. $progressParam = @{
  80. Activity = "Search for '$SearchString' - $progress of $(@($fileList).count)"
  81. Status = "Found: $(@($results).count)"
  82. PercentComplete = $(($progress/$($fileList.count))*100)
  83. id = 1
  84. }
  85. Write-Progress @progressParam
  86. $progress++
  87. $found = Select-String -Path $file.fullname -pattern $SearchString
  88. if($found){
  89. Write-Verbose ($found | Out-String)
  90. $results.Add(($file | Select-Object LastWriteTime, FullName))
  91. }
  92. }
  93. Write-Progress -Activity "Done" -Id 1 -Completed
  94.  
  95. # Return found scripts sorted by last write time
  96. $results | sort LastWriteTime
  97. }
Add Comment
Please, Sign In to add comment