ProductivityTools.AzureAutomation.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function GetConfiguration{
    param(
        [string]$Profile
    )

    if ($Profile -eq "")
    {
        throw "Get-Configuration Missing profile name!"
    }

    $location = Get-MasterConfiguration $("$Profile"+":Location")
    $resourceGroup = Get-MasterConfiguration $("$Profile"+":ResourceGroup")
    $storageName = Get-MasterConfiguration $("$Profile"+":StorageName")
    $skuName= Get-MasterConfiguration $("$Profile"+":SkuName")
    $storageContainerName = Get-MasterConfiguration $("$Profile"+":StorageContainerName")
    $cdnHostname = Get-MasterConfiguration $("$Profile"+":CdnHostname")

    $config = New-Object -TypeName psobject 
    $config | Add-Member -MemberType NoteProperty -Name Location -Value $location
    $config | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $resourceGroup
    $config | Add-Member -MemberType NoteProperty -Name StorageName -Value $storageName
    $config | Add-Member -MemberType NoteProperty -Name SkuName -Value $skuName
    $config | Add-Member -MemberType NoteProperty -Name StorageContainerName -Value $storageContainerName
    $config | Add-Member -MemberType NoteProperty -Name CdnHostname -Value $cdnHostname

    # $config | Add-Member -MemberType NoteProperty -Name CdnProfileName -Value "productivitycdnprofile"
    # $config | Add-Member -MemberType NoteProperty -Name CdnSku -Value "Standard_Akamai"
    # $config | Add-Member -MemberType NoteProperty -Name CdnEndpointName -Value "ptblog"
    
    # $config | Add-Member -MemberType NoteProperty -Name CustomDomainName -Value "cdn3"
    return $config
}


function GetContext{

    [cmdletbinding()]
    param(
        [string]$Profile
    )

    if ($Profile -eq "")
    {
        throw "Get-Configuration Missing profile name!"
    }
    

    $config=GetConfiguration $Profile
    $keys=Get-AzStorageAccountKey -ResourceGroupName $config.ResourceGroup -Name $config.StorageName
    $ctx=New-AzStorageContext -StorageAccountName $config.StorageName -StorageAccountKey $keys[0].value
    return $ctx
}

function Create-ResourceGroup{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )

    Write-Verbose "Hello from CreateResourceGroup"
    
    $config=GetConfiguration $Profile
    Write-Verbose "INVOKE COMMAND: New-AzResourceGroup -Name $($config.ResourceGroup) -Location $($config.Location)"
    New-AzResourceGroup -Name $config.ResourceGroup -Location $config.Location
}

function RemoveResourceGroup{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )

    Write-Verbose "Hello from RemoveResourceGroup"
    
    $config=GetConfiguration $Profile

    Write-Verbose "INVOKE COMMAND: Remove-AzResourceGroup -Name $($config.ResourceGroup) -Force"
    Remove-AzResourceGroup -Name $config.ResourceGroup -Force
}

function CreateStorageAccount{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )
    Write-Verbose "Hello from CreateStorageAccount"
    
    $config=GetConfiguration $Profile
    Write-Verbose "INVOKE COMMAND: New-AzStorageAccount -ResourceGroupName $($config.ResourceGroup) -Name $($config.StorageName) -SkuName $($config.SkuName) -Location $($config.Location) -EnableHttpsTrafficOnly $($False)"

    $storageAccount = New-AzStorageAccount -ResourceGroupName $config.ResourceGroup -Name $config.StorageName  -SkuName $config.SkuName -Location $config.Location -EnableHttpsTrafficOnly $False
    #$ctx = $storageAccount.Context
}

function RemoveStorageAccount{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )
    Write-Verbose "Hello from RemoveStorageAccount"
    
    $config=GetConfiguration $Profile
    Write-Verbose "INVOKE COMMAND: Remove-AzStorageAccount -Name $($config.StorageName) -ResourceGroupName $($config.ResourceGroup) -Force"
    Remove-AzStorageAccount -Name $config.StorageName -ResourceGroupName $config.ResourceGroup -Force
}

function CreateStorageContainer{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )

    Write-Verbose "Hello from CreateStorageContainer"
    
    $config=GetConfiguration -Profile $Profile
    $context=GetContext -Profile $Profile
    Write-Verbose "INVOKE COMMAND: New-AzStorageContainer -Name $($config.StorageContainerName) -Context $($context) -Permission blob"
    New-AzStorageContainer -Name $config.StorageContainerName -Context $context -Permission blob
}

function RemoveStorageContainer{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )

    Write-Verbose "Hello from RemoveStorageContainer"
    
    $config=GetConfiguration -Profile $Profile
    $context=GetContext -Profile $Profile
    Write-Verbose "INVOKE COMMAND: Remove-AzStorageContainer -Name $($config.StorageContainerName) -Context $($context) -Force"
    Remove-AzStorageContainer -Name $config.StorageContainerName -Context $context -Force
}

function SetStorageAccountCustomDomain{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Profile
    )

    Write-Verbose "Hello from SetStorageAccountCustomDomain"
    
    $config=GetConfiguration -Profile $Profile
    Write-Verbose "INVOKE COMMAND: Set-AzStorageAccount -ResourceGroupName $($config.ResourceGroup) -AccountName $($config.StorageName) -CustomDomainName $($config.CdnHostname)"

    Set-AzStorageAccount -ResourceGroupName $config.ResourceGroup  -AccountName $config.StorageName  -CustomDomainName $config.CdnHostname
    
}

function Push-FileToAzureBlobStorage{
    
    [cmdletbinding()]
    param(
        [string]$Profile,
        [string]$Prefix,
        [string]$Path
    )
    
    Write-Verbose "Hello from AddFile"
    Write-Verbose "Prefix: $Prefix"
    Write-Verbose "Path to file: $Path"
    
    $file=Get-Item $Path
    $fileName=$file.Name
    $DestinationFileName="$Prefix\$fileName"
    Write-Verbose "Destination file name which will be placed on azure blob: $DestinationFileName"
    
    
    $config=GetConfiguration $Profile
    $context=GetContext $Profile
    $blob=Set-AzStorageBlobContent -File $Path -Container $config.ImagesContainerName -Blob $DestinationFileName -Context $context -Force 
    return $blob
}