Get-CAzureRMStorageServicesAuthorizationHeader.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
function Get-CAzureRMStorageServicesAuthorizationHeader
{
<#
.Synopsis
   This command gets you authentication headers for the given storage account
.DESCRIPTION
   This command gets you authentication headers for the given storage account
.EXAMPLE
    $StorageAccountName="abcdatastore"
    PS C:\>$url = "https://$storageAccountName.blob.core.windows.net/?comp=list"
    PS C:\>$key="jYgN08+O4wDX5aWc3rdqyFgtYYKXf7+vv3jFI+bo8m3SAGAR143JYOWXT1ce7+YdikuJlIdprNWhUkqhKVEJnQ=="
    PS C:\>Get-CAzureRMStorageServicesAuthorizationHeader -StorageAccountName $StorageAccountName $storageAccountKey $key -Url $Url -Method GET
.INPUTS
   1. StorageAccountName
   2. StorageAccountKey
   3. Url
   4. WebRequestMethod
.OUTPUTS
   Azure Storage Service's authrization headers in hashtable
.NOTES
   https://docs.microsoft.com/de-de/azure/storage/common/storage-rest-api-auth
.COMPONENT
   AzureRM Storage Services Authorization Header
#>

    Param(
        [String]$StorageAccountName,
        [String]$storageAccountKey,
        [String]$Url,
        [Microsoft.PowerShell.Commands.WebRequestMethod]$Method,
        [String]$xmsversion = "2018-03-28"
    )
    PROCESS
    {
        $httpRequestMessage= [System.Net.Http.HttpRequestMessage]::new($Method.ToString().ToUpper(),$Url)
        $xmsdate = [datetime]::UtcNow.ToString("R",[CultureInfo]::InvariantCulture)
        $httpRequestMessage.Headers.Add("x-ms-date",$xmsdate);
        $httpRequestMessage.Headers.Add("x-ms-version",$xmsversion);
        $httpRequestMessage.Headers.Authorization=[StorageRestApiAuth.AzureStorageAuthenticationHelper]::GetAuthorizationHeader($StorageAccountName,$storageAccountKey,$xmsdate,$httpRequestMessage);

        [String]${x-ms-date}=$httpRequestMessage.Headers.GetValues("x-ms-date")
        [String]${x-ms-version}=$httpRequestMessage.Headers.GetValues("x-ms-version")
        [String]$Authorization=$httpRequestMessage.Headers.GetValues("Authorization")
        [String]$Authorization=$httpRequestMessage.Headers.GetValues("Authorization")
        $headers=@{
            "Authorization"=$Authorization
            "x-ms-date"=${x-ms-date}
            "x-ms-version"=${x-ms-version}
        }
        Write-Output $headers
    }
}