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 = "GETn
nn
nn
nn
nn
nn
nx-ms-date:$datenx-ms-version:2009-09-19
n/$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