Remove-AzBlobItem.ps1
|
Function Remove-AzBlobItem { <# .SYNOPSIS Remove item from Azure Storage Account container. .PARAMETER StorageAccountName Name of the storage account containing the table. .PARAMETER ContainerName Name of the container to query. .PARAMETER Item Item to remove. .PARAMETER AccessToken Access token for authenticating with Azure Table Storage. .EXAMPLE $Item = Remove-AzBlobItem -StorageAccountName "<StorageAccountName>" -ContainerName <ContainerName> -Item <Item> -AccessToken $Token.AccessToken .NOTES Author: Michal Gajda .LINK https://learn.microsoft.com/en-us/rest/api/storageservices/delete-blob?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{} } |