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
         
        .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
    {
        # Counter for how many objects were skipped
        $skipped = 0
        
        # Counter for how many objects were sent onwards
        $sent = 0
    }
    
    Process
    {
        foreach ($o in $InputObject)
        {
            # If we have yet to skip objects, do so and continue with the next object
            if ($Skip -gt $skipped) { $skipped++; continue }
            
            # If we have yet to send on an object, do so, then continue with the next object
            if ($sent -lt $Number) { $sent++; $o; continue }
        }
    }
    
    End
    {
        
    }
}
Import-PSUAlias -Name "s" -Command "Select-PSUObjectSample"