Get-AzBlobItem.ps1
|
Function Get-AzBlobItem { <# .SYNOPSIS Get items from Azure Storage Account container. .PARAMETER StorageAccountName Name of the storage account containing the table. .PARAMETER ContainerName Name of the container to query. .PARAMETER Prefix Prefix for items search. .PARAMETER AccessToken Access token for authenticating with Azure Table Storage. .EXAMPLE #Require RBAC app permissions on storage account: Storage Blob Data Reader or Storage Table Blob Contributor $Params = @{ Scope = @("https://storage.azure.com/.default") ClientId = $Connection.ApplicationId TenantId = $Connection.TenantId RedirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient" Certificate = $Certificate } $Token = Get-PSMSALToken @Params #Get single item from Azure Table $Item = Get-AzBlobItem -StorageAccountName "<StorageAccountName>" -ContainerName <ContainerName> -AccessToken $Token.AccessToken .NOTES Author: Michal Gajda .LINK https://learn.microsoft.com/en-us/rest/api/storageservices/list-blobs?tabs=microsoft-entra-id #> [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact="Low")] Param( [Parameter(Mandatory = $true)] [String]$StorageAccountName, [Parameter(Mandatory = $true)] [String]$ContainerName, [Parameter()] [String]$Prefix, [Parameter(Mandatory = $true)] [String]$AccessToken ) Begin { #Build headers $Date = [DateTime]::UtcNow.ToString('R') $Headers = @{ "x-ms-version" = "2026-06-06" "x-ms-date" = $Date "Authorization" = "Bearer $($AccessToken)" "x-ms-blob-type" = "BlockBlob" } } Process { #Get list of files in Storage Account container $Uri = "https://$($StorageAccountName).blob.core.windows.net/$($ContainerName)?restype=container&comp=list" if($Prefix) { $Uri += "&prefix=$($Prefix)" } Write-Verbose $Uri Write-Verbose "Headers: $($Headers | Select-Object -exclude "Authorization" | Convertto-Json -Compress)" If ($PSCmdlet.ShouldProcess($StorageAccountName,"Get list of items in container $ContainerName")) { $Response = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method GET -ResponseHeadersVariable ResponseHeaders Write-Verbose "Item retrieved: $($Response.Count)" $Xml = [xml]$Response.Substring($Response.IndexOf('<')) $Result = $Xml.EnumerationResults.Blobs.Blob } Return $Result } End{} } |