functions/objects/Select-PSUObjectSample.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Select-PSUObjectSample
{
    <#
        .SYNOPSIS
            Used to only pick a sample from the objects passed to the function.
         
        .DESCRIPTION
            Used to only pick a sample from the objects passed to the function.
         
        .PARAMETER InputObject
            The objects to pick a sample from.
         
        .PARAMETER Skip
            How many objects to skip.
         
        .PARAMETER Number
            How many objects to pick
            Use a negative number to pick the last X items instead.
         
        .EXAMPLE
            PS C:\> Get-ChildItem | Select-PSUObjectSample -Skip 1 -Number 3
     
            Scans the current directory, skips the first returned object, then passes through the next three objects and skips the rest.
         
        .EXAMPLE
            PS C:\> dir | s 3 1
     
            Same as the previous example, only this time using aliases and positional binding.
            Scans the current directory, skips the first returned object, then passes through the next three objects and skips the rest.
    #>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        $InputObject,
        
        [Parameter(Position = 1)]
        [int]
        $Skip = 0,
        
        [Parameter(Position = 0)]
        [int]
        $Number = 1
    )
    
    Begin
    {
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Select-Object', [System.Management.Automation.CommandTypes]::Cmdlet)
        $splat = @{ }
        if ($Skip -gt 0) { $splat["Skip"] = $Skip }
        if ($Number -ge 0) { $splat["First"] = $Number + 1 }
        else { $splat["Last"] = $Number * -1 }
        $scriptCmd = { & $wrappedCmd @splat }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline()
        $steppablePipeline.Begin($true)
    }
    
    Process
    {
        foreach ($o in $InputObject)
        {
            $steppablePipeline.Process($o)
        }
    }
    
    End
    {
        $steppablePipeline.End()
    }
}
Import-PSUAlias -Name "s" -Command "Select-PSUObjectSample"