Send-AzBlobItem.ps1
|
Function Send-AzBlobItem { <# .SYNOPSIS Upload item to Azure Storage Account container. .PARAMETER StorageAccountName Name of the storage account containing the table. .PARAMETER ContainerName Name of the container to query. .PARAMETER DestinationPath Folder path in container. .PARAMETER Item Name of the source item to upload. .PARAMETER AccessToken Access token for authenticating with Azure Table Storage. .EXAMPLE $Item = Send-AzBlobItem -StorageAccountName "<StorageAccountName>" -ContainerName <ContainerName> -FilePath <FilePath> -AccessToken $Token.AccessToken .NOTES Author: Michal Gajda .LINK https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob?tabs=microsoft-entra-id #> [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact="Low")] Param( [Parameter(Mandatory = $true)] [String]$StorageAccountName, [Parameter(Mandatory = $true)] [String]$ContainerName, [Parameter()] [String]$DestinationPath, [Parameter(Mandatory = $true)] [String]$FilePath, [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 source file name $FileName = (Get-Item $FilePath).Name #Upload destination in Storage Account container $BaseUri = "https://$($StorageAccountName).blob.core.windows.net/$($ContainerName)" $DestinationUri = "$($DestinationPath)/$($FileName)" -replace "^/","" -replace "//","/" $Uri = "$BaseUri/$($DestinationUri)" Write-Verbose $Uri Write-Verbose "Headers: $($Headers | Select-Object -exclude "Authorization" | Convertto-Json -Compress)" If ($PSCmdlet.ShouldProcess($StorageAccountName,"Upload item $($FileName) to container $ContainerName")) { $Response = Invoke-RestMethod -Uri $Uri -Headers $Headers -Method PUT -InFile $FilePath -ResponseHeadersVariable ResponseHeaders $Response = Invoke-RestMethod -Uri "$($BaseUri)?restype=container&comp=list&prefix=$($DestinationUri)" -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{} } New-Alias -Name "Upload-AzBlobItem" Receive-AzBlobItem |