Advertisement
Yevrag35

Stupid GetContent Wrapper

Nov 3rd, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function StupidGetContentWrapper()
  2. {
  3.     [CmdletBinding(DefaultParameterSetName="None")]
  4.     param
  5.     (
  6.         [Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
  7.         [Alias("PSPath")]
  8.         [string] $FilePath,
  9.  
  10.         [Parameter(Mandatory, ParameterSetName="Skip")]
  11.         [Alias("Skip")]
  12.         [int] $SkipCount,
  13.  
  14.         [Parameter(Mandatory, ParameterSetName="Tail")]
  15.         [Alias("Tail")]
  16.         [int] $TailCount
  17.     )
  18.     Process
  19.     {
  20.         $getContentArgs = @{
  21.             Path = $FilePath
  22.         }
  23.  
  24.         if ($PSBoundParameters.ContainsKey("TailCount"))
  25.         {
  26.             $getContentArgs.Tail = $TailCount
  27.         }
  28.  
  29.         # Get the content of the specified file
  30.         $content = @(Get-Content @getContentArgs)
  31.  
  32.         if ($PSBoundParameters.ContainsKey("SkipCount") -and $null -ne $content -and $content.Count -gt 0)
  33.         {
  34.             $content | Select-Object -First $SkipCount
  35.             # Or without Select-Object and using the indexer instead...
  36.             # $content[($SkipCount-1)..$content.Count]
  37.         }
  38.         else
  39.         {
  40.             $content
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement