PoshCode Logo PowerShell Code Repository

Wrapper 1 Select-String by Bernd Kriszio 5 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1261"></script>download | new post

Select-String has the funny behavior that it -Encoding parameter could be default, but Select-String uses unicode as default value. That means we do not find strings containing some western european letters like ‘ń÷Ř▀’ in ansi coded files. Finally I want the choice default_or_oem, to search even my OEM850 coded files. Here I fix only the default to ‘default’

  1. [CmdletBinding()]
  2. param(
  3.     [Parameter(ParameterSetName='Object', Mandatory=$true, ValueFromPipeline=$true)]
  4.     [AllowEmptyString()]
  5.     [AllowNull()]
  6.     [System.Management.Automation.PSObject]
  7.     ${InputObject},
  8.  
  9.     [Parameter(Mandatory=$true, Position=0)]
  10.     [System.String[]]
  11.     ${Pattern},
  12.  
  13.     [Parameter(ParameterSetName='File', Mandatory=$true, Position=1, ValueFromPipelineByPropertyName=$true)]
  14.     [Alias('PSPath')]
  15.     [System.String[]]
  16.     ${Path},
  17.  
  18.     [Switch]
  19.     ${SimpleMatch},
  20.  
  21.     [Switch]
  22.     ${CaseSensitive},
  23.  
  24.     [Switch]
  25.     ${Quiet},
  26.  
  27.     [Switch]
  28.     ${List},
  29.  
  30.     [ValidateNotNullOrEmpty()]
  31.     [System.String[]]
  32.     ${Include},
  33.  
  34.     [ValidateNotNullOrEmpty()]
  35.     [System.String[]]
  36.     ${Exclude},
  37.  
  38.     [Switch]
  39.     ${NotMatch},
  40.  
  41.     [Switch]
  42.     ${AllMatches},
  43.  
  44.     [ValidateSet('unicode','utf7','utf8','utf32','ascii','bigendianunicode','default','oem')]
  45.     [ValidateNotNullOrEmpty()]
  46.     [System.String]
  47.     ${Encoding},
  48.  
  49.     [ValidateNotNullOrEmpty()]
  50.     [ValidateCount(1, 2)]
  51.     [ValidateRange(0, 2147483647)]
  52.     [System.Int32[]]
  53.     ${Context})
  54.  
  55. begin
  56. {
  57.     try {
  58.         $outBuffer = $null
  59.         if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  60.         {
  61.             $PSBoundParameters['OutBuffer'] = 1
  62.         }
  63.         $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Select-String', [System.Management.Automation.CommandTypes]::Cmdlet)
  64.         $scriptCmd = {& $wrappedCmd @PSBoundParameters }
  65.            
  66.         # wrapper generated by New-ProxyCommand
  67.         # http://blogs.msdn.com/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx
  68.  
  69.         # ------- modification added by Bernd Kriszio -- http://pauerschell.blogspot.com/
  70.         #
  71.         #
  72.         if (! $Encoding)
  73.         {
  74.             # it is ridiculous to call a parameter default which is not
  75.             $PSBoundParameters.Encoding = 'default'
  76.  
  77.             <#
  78.                 # finally I want a values default_or_oem for -Encoding
  79.                 # to find strings in unicode, ansii and oem coded files
  80.                 # but it is not that simple
  81.                 @(Select-String * -pattern ─rger -encoding default) +
  82.                 @(Select-String * -pattern ─rger -encoding oem) |sort-object | Get-Unique
  83.            #>
  84.          
  85.         }
  86.         # ---------------------------------------------------------------------
  87.         $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  88.         $steppablePipeline.Begin($PSCmdlet)
  89.     } catch {
  90.         throw
  91.     }
  92. }
  93.  
  94. process
  95. {
  96.     try {
  97.         $steppablePipeline.Process($_)
  98.     } catch {
  99.         throw
  100.     }
  101. }
  102.  
  103. end
  104. {
  105.     try {
  106.         $steppablePipeline.End()
  107.     } catch {
  108.         throw
  109.     }
  110. }
  111. <#
  112.  
  113. .ForwardHelpTargetName Select-String
  114. .ForwardHelpCategory Cmdlet
  115.  
  116. #>

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me