Public/Get-B2ItemVersion.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
function Get-B2ItemVersion
{
<#
.SYNOPSIS
    The Get-B2ItemVersion cmdlet retirives file version info.
.DESCRIPTION
    The Get-B2ItemVersion cmdlet retirives file version info.
    Bucket information required for this cmdlet can be retirived with Get-B2Bucket.
     
    An API key is required to use this cmdlet.
.EXAMPLE
    Get-B2ItemVersion -BucketID 5b232e8875c6214145260818
     
    Name : files/hello.txt
    Size : 6
    UploadTime : 1439162596000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886245_d20150809_m232316_c100_v0009990_t0003
     
    Name : files/world.txt
    Size : 0
    UploadTime : 1439162603000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886247_d20150809_m232323_c100_v0009990_t0005
     
    Name : files/world.txt
    Size : 6
    UploadTime : 1439162596000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886246_d20150809_m232316_c100_v0009990_t0003
     
    The above cmdlet will return the verion information for all file versions in the given bucket.
.EXAMPLE
    PS C:\>Get-B2Bucket | Get-B2ItemVersion
     
    Name : files/hello.txt
    Size : 6
    UploadTime : 1439162596000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886245_d20150809_m232316_c100_v0009990_t0003
     
    Name : files/world.txt
    Size : 0
    UploadTime : 1439162603000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886247_d20150809_m232323_c100_v0009990_t0005
     
    Name : files/world.txt
    Size : 6
    UploadTime : 1439162596000
    Action : upload
    ID : 4_z27c88f1d182b150646ff0b16_f100920ddab886246_d20150809_m232316_c100_v0009990_t0003
     
    The above cmdlet will return the verion information for all file versions in all buckets.
.INPUTS
    System.String
             
        This cmdlet takes the AccountID and ApplicationKey as strings.
         
    System.Uri
     
        This cmdlet takes the ApiUri as a Uri.
.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('gb2bv')]
    [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 Uri for the B2 Api query.
        [Parameter(Mandatory=$false,
                   Position=1)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Uri]$ApiUri = $script:SavedB2ApiUri,
        # The authorization token for the B2 account.
        [Parameter(Mandatory=$false,
                   Position=2)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$ApiToken = $script:SavedB2ApiToken
    )
    
    Begin
    {
        [Hashtable]$sessionHeaders = @{'Authorization'=$ApiToken}
        [Uri]$b2ApiUri = "$ApiUri/b2api/v1/b2_list_file_versions"
    }
    Process
    {
        foreach($bucket in $BucketID)
        {
            try
            {
                [String]$sessionBody = @{'bucketId'=$bucket} | 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 file information.`n`r$errorDetail" `
                    -Message "Unable to retrieve the file information.`n`r$errorDetail" -Category ReadError
            }
        }
    }
}