ProductivityTools.CloudItems.psm1

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
function InitResources(){
        [cmdletbinding()]
    param(
        [string]$profile
    )
    Create-ResourceGroup -Profile $profile  
    Create-StorageAccount -Profile $profile  
    Create-StorageContainer -Profile $profile  
}

function ZipFile(){
    [cmdletbinding()]
    param(
        [System.IO.FileSystemInfo]$fileInfo,
        [switch]$usePassword,
        [string]$profile
    )

    Write-Verbose $fileInfo.FullName
    $fileName=[System.IO.Path]::Combine($fileInfo.Directory.FullName,$fileInfo.BaseName+".zip")

    if ($usePassword)
    {
        $password = Get-MasterConfiguration $("$profile"+":Password")
        Write-Verbose "Password which will be used to zip files: $password"
        Compress-7Zip -Path $fileInfo.FullName -Password $password -ArchiveFileName $fileName 
    }
    else
    {
        Compress-7Zip -Path $fileInfo.FullName -ArchiveFileName $fileName 
    }
    return $fileName;
    
}

function PushItem()
{
    [cmdletbinding()]
    param(
        [System.IO.FileSystemInfo]$fileInfo,
        [switch]$compress,
        [switch]$usePassword,
        [string]$profile
    )

    $path=$fileInfo.FullName;

    if($compress -or $usePassword)
    {
        $zipPath=ZipFile -fileInfo $fileInfo -usePassword:$usePassword -profile $profile
        $path=$zipPath
    }
    
    $blob=Push-FileToAzureBlobStorage -Profile $profile -Path "$path"
    Write-Verbose "Item was pushed to the cloud"

    if($compress -or $usePassword)
    {
        Write-Verbose "Zip file will be removed now"
        Remove-Item -path $zipPath
    }

    $url=$blob.BlobClient.Uri.AbsoluteUri
    return $url
}

function GetProfile(){
    [cmdletbinding()]
    param(
        [string]$Profile
    )

    Write-Verbose "Passed profile $Profile"

    if ($Profile -eq $null -or $Profile -eq "")
    {
        $Profile="ProductivityTools.CloudItems.Default"
    }
    return $Profile
}

function Push-ItemToTheCloud(){
    [cmdletbinding()]
    param(
        [string]$Profile,
        [string]$Path,
        [switch]$Compress,
        [switch]$UsePassword
    )
    $Profile=GetProfile -Profile $Profile

    $fileInfo=Get-Item $Path
     
    InitResources $profile
    
    $url=PushItem -fileInfo $fileInfo -compress:$Compress -profile $Profile -usePassword:$UsePassword
    Write-Host $url
}

function Get-ItemListFromTheCloud()
{
    [cmdletbinding()]
    param(
        [string]$Profile
    )

    $Profile=GetProfile -Profile $Profile
    Get-AzureBlobStorageFiles -Profile "$Profile"
}

function Remove-ItemFromTheCloud()
{
    [cmdletbinding()]
    param(
        [string]$Profile,
        [string]$Name
    )
    $Profile=GetProfile -Profile $Profile
    Remove-AzureBlobStorageFile -Profile $Profile -Name $Name
}

function Remove-AllItemsFromTheCloud()
{
    [cmdletbinding()]
    param(
        [string]$Profile
    )
    $Profile=GetProfile -Profile $Profile
    $files=Get-AzureBlobStorageFiles -Profile "$Profile"
    foreach($file in $files)
    {
        $name=Split-Path $file -leaf
        Write-Output $name

        Remove-AzureBlobStorageFile -Profile $Profile -Name  $name -Force
    }
}

Export-ModuleMember Push-ItemToTheCloud
Export-ModuleMember Get-ItemListFromTheCloud
Export-ModuleMember Remove-ItemFromTheCloud
Export-ModuleMember Remove-AllItemsFromTheCloud