Public/Get-B2ChildItem.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function Get-B2ChildItem
{
<#
.SYNOPSIS
    The Get-B2ChildItem cmdlet will return a list of items in a bucket and associated file properties.
.DESCRIPTION
    The Get-B2ChildItem cmdlet will return a list of items in a bucket and associated file properties.
     
    The file information to be returned:
     
    - Name
    - Size
    - UploadTime
    - Action
    - ID
     
    By default the selection has a hard limit to the first 1000 items; to increment the selection
    use the StartName paramter to specifiy the next starting item's name and ItemCount to set the
    list limit from the file in StartName.
     
    An API key is required to use this cmdlet.
.EXAMPLE
    Get-B2ChildItem -BucketID 4a48fe8875c6214145260818
     
    Name : items/hello.txt
    Size : 6
    UploadTime : 1439083733000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f1004ba650fe24e6b_d20150809_m012853_c100_v0009990_t0000
     
    Name : items/world.txt
    Size : 6
    UploadTime : 1439083734000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f1004ba650fe24e6c_d20150809_m012854_c100_v0009990_t0000
     
    The cmdlet above will list the first 1000 items in a given bucket.
.EXAMPLE
    PS C:\>Get-B2ChildItem -BucketID 4a48fe8875c6214145260818 -StartName items/world.txt -ItemCount 3
     
    Name : items/world.txt
    Size : 6
    UploadTime : 1439083734000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f1004ba650fe24e6c_d20150809_m012854_c100_v0009990_t0000
     
    Name : items/how.txt
    Size : 6
    UploadTime : 1439083734000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f8aa4ba650fe24e6c_d20150809_m012854_c100_v0009990_t0000
     
    Name : items/are.txt
    Size : 6
    UploadTime : 1439083734000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f1004hf950fe24e6c_d20150809_m012854_c100_v0009990_t0000
     
    The cmdlet above will start listing the items from items/world.txt and then three preceeding
    items after that.
.LINK
    https://www.backblaze.com/b2/docs/
.OUTPUTS
    PS.B2.File
     
        The cmdlet will output a PS.B2.File object holding file version info.
.ROLE
    PS.B2
.FUNCTIONALITY
    PS.B2
#>

    [CmdletBinding(SupportsShouldProcess=$false)]
    [Alias('gb2ci')]
    [OutputType('PS.B2.File')]
    Param
    (
        # The ID of the bucket to query.
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String[]]$BucketID,
        # The name of the item to start listing from.
        [Parameter(Mandatory=$false,
                   Position=1)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$StartName,
        # The number of items to return; the default and max is 1000.
        [Parameter(Mandatory=$false,
                   Position=1)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [UInt32]$ItemCount = 1000,
        # The Uri for the B2 Api query.
        [Parameter(Mandatory=$false,
                   Position=2)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Uri]$ApiUri = $script:SavedB2ApiUri,
        # The authorization token for the B2 account.
        [Parameter(Mandatory=$false,
                   Position=3)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$ApiToken = $script:SavedB2ApiToken
    )
    
    Begin
    {
        [Hashtable]$sessionHeaders = @{'Authorization'=$ApiToken}
        [Uri]$b2ApiUri = "${ApiUri}b2api/v1/b2_list_file_names"
        Write-Debug $b2ApiUri
    }
    Process
    {
        foreach($bucket in $BucketID)
        {
            try
            {
                [String]$sessionBody = @{'bucketId'=$bucket;'maxFileCount'=$ItemCount;'startFileName'=$StartName} | ConvertTo-Json
                $bbInfo = Invoke-RestMethod -Method Post -Uri $b2ApiUri -Headers $sessionHeaders -Body $sessionBody
                foreach($info in $bbInfo.files)
                {
                    $bbReturnInfo = [PSCustomObject]@{
                        'Name' = $info.fileName
                        'Size' = $info.size
                        'UploadTime' = $info.uploadTimestamp
                        'Action' = $info.action
                        'ID' = $info.fileId
                    }
                    # bbReturnInfo is returned after Add-ObjectDetail is processed.
                    Add-ObjectDetail -InputObject $bbReturnInfo -TypeName 'PS.B2.File'
                }
            }
            catch
            {
                $errorDetail = $_.Exception.Message
                Write-Error -Exception "Unable to retrieve the item list.`n`r$errorDetail" `
                    -Message "Unable to retrieve the item list.`n`r$errorDetail" -Category ReadError
            }
        }
    }
}