Search code examples
windowsazurepowershellazure-file-share

API call to get Azure File Share storage details via PowerShell not working


I’m attempting to retrieve detailed information from an Azure File Share (The used Capacity using a PowerShell script to make an API call. My intention is to automate this process within a larger PowerShell workflow, so solutions involving Azure CLI (via Az-connect)or manual Azure Portal operations are not viable for the intended purpose. Below is the script I’ve used:

    $subscriptionId = ""
    $resourceGroupName = ""
    $storageAccountName = ""
    $accessToken = "" 

# Set the request headers including the Authorization header with your access token
    $headers = @{
        'Authorization' = "Bearer $accessToken"
        'Content-Type' = 'application/json'
    }

# Set the API version
    $apiVersion = "2023-04-01"

# Construct the URL for the FileShares API
    $url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Storage/storageAccounts/$storageAccountName/fileServices/default/shares?api-version=$apiVersion"

# Make the API call
    $response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get

# Output the response
    $response.value

While the script executes without errors and returns basic information (id, name, type, etc.), it doesn’t provide the detailed file share storage data I expect. I’ve specifically set the API version to “2023-04-01”. Could anyone help clarify if my issue lies within the PowerShell script or the API call structure? Any guidance on retrieving detailed file share information through this method would be greatly appreciated.

The output I get is the below

id : /subscriptions/subID/resourceGroups/ResourceName/providers/Microsoft.Storage/storageAccounts/StorageAcctName/fileServices/default/shares/ServerName-g
name : ServerName-g
type : Microsoft.Storage/storageAccounts/fileServices/shares
etag : “0x8D”
properties : @{leaseStatus=unlocked; leaseState=available; accessTier=Cool; accessTierChangeTime=2022-12-30T14:18:32.0000000Z; lastModifiedTime=2024-03-09T20:46:29.0000000Z; shareQuota=102400;
enabledProtocols=SMB}

Thank you in advance for your time and help.


Solution

  • I would love to get used capacity for all fileshares by doing an API call and not necessarily via Az-connect. If it works for a single fileshare, that can then be adopted to recursively retrieve it from all fileshares.

    You can use the below API call to get the used capacity of a single file share using a script.

    Script:

    $storageaccountname="venkat789"
    
    $headers = @{
            'Authorization' = "Bearer $accessToken"
            'Content-Type' = 'application/json'
            'x-ms-version'='2020-04-08'
            'x-ms-date'='Thu, 21 March 2024 05:10:33 GMT'
        }
    
    $url="https://$storageaccountname.file.core.windows.net/<sharename>?restype=share&comp=stats"
    
    $response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
    $response
    

    Output:

    <?xml version="1.0" encoding="utf-8"?><ShareStats><ShareUsageBytes>7868</ShareUsageBytes></ShareStats>
    

    enter image description here

    The above script retrieves used capacity for a file share in an Azure Storage account. It sets the necessary headers for the REST API call, constructs the URL for the file share statistics endpoint, and uses the Invoke-RestMethod cmdlet to make a GET request to the URL with the headers specified.

    Reference:

    Get Share Stats (FileREST API) - Azure Files | Microsoft Learn