How to contruct authorization header using access key for Azure File share rest api using PowerShell

samer singh 21 Reputation points
2020-09-02T06:31:00.487+00:00

I am using following to get Azure File share stats but getting authorization error

$storageAccount = 'Test'
$key = 'key---------'
$Share= 'TestShare'

$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")

$stringToSign = "GETnnnnnnnnnnnnx-ms-date:$datenx-ms-version:2009-09-19n/$storageAccount/$Share`nrestype:share"

$hasher = New-Object System.Security.Cryptography.HMACSHA256
$hasher.Key = $sharedKey

$signedSignature = [System.Convert]::ToBase64String($hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($stringToSign)))

$authHeader = "SharedKey ${StorageAccount}:$signedSignature"

$headers = @{"x-ms-date"=$date
"x-ms-version"="2009-09-19"
"Authorization"=$authHeader}
$URI = "https://$storageAccount.file.core.windows.net/TestShare?restype=share&comp=stats"

$Shares = Invoke-RestMethod -method GET -Uri $URI -Headers $headers

Following is the error

Invoke-RestMethod : AuthenticationFailedServer failed to authenticate the
request. Make sure the value of Authorization header is formed correctly
including the signature.
RequestId:52a5446b-401a-0170-2zx2-70de7c000000
Time:2020-09-02T06:26:35.1284048ZThe MAC signature found in the HTTP request
'8dSFbVc/+d2JwQxjuD7BAx9hDYHoJaoSBqirfgi3xyz=' is not the same as any computed
signature. Server used following string to sign: 'GET
x-ms-date:Wed, 02 Sep 2020 06:26:35 GMT
x-ms-version:2009-09-19
/StorageAccount/testfileshare
comp:stats
restype:share'.
At line:24 char:14

  • ... container = Invoke-RestMethod -method GET -Uri $URI -Headers $header ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
    pWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
    ll.Commands.InvokeRestMethodCommand

Reference : https://learn.microsoft.com/en-us/rest/api/storageservices/get-share-stats

Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,857 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sumarigo-MSFT 44,816 Reputation points Microsoft Employee
    2020-09-03T07:25:28.863+00:00

    @samer singh Firstly, apologies for the delay in responding here and any inconvenience this issue may have caused.

    Based on the the error message, try to remove "x-ms-type:file" and add comp:stats in the stringToSign . Also there is a similar thread discussion in the SO forum, If you are still finding any issue, please let us know we would like to work closer on this Azure File share rest API using PowerShell.

    Hope this helps! Kindly let us know if the above helps or you need further assistance on this issue.

    ------------------------------------------------------------------------------------------------------------------------------------

    Please don’t forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.