Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active March 14, 2024 19:46
Show Gist options
  • Save mavaddat/9c7ba7ee0c5860c0253ed7a4cf9cd291 to your computer and use it in GitHub Desktop.
Save mavaddat/9c7ba7ee0c5860c0253ed7a4cf9cd291 to your computer and use it in GitHub Desktop.
A small function to move Azure Blobs using the already available cmdlets from Az.Storage
function Move-AzStorageBlob {
#Requires -Modules Az.Storage
[CmdletBinding(ConfirmImpact = 'High', SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[System.Object]
$SrcBlob,
[Parameter(Mandatory)]
[string]
$SrcContainer,
[Parameter(Mandatory)]
[string]
$DestContainer,
[Parameter(Mandatory)]
[System.Object]
$DestBlob,
[Parameter(Mandatory)]
[Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext]
$Context,
[switch]
$Force
)
begin {
[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]$Copy = [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]$null
if($SrcBlob -is [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]) {
$Source = $SrcBlob.Name
} else {
$Source = $SrcBlob.ToString()
}
if($DestBlob -is [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]) {
$Destination = $DestBlob.Name
} else {
$Destination = $DestBlob.ToString()
}
}
process {
$Copy = Copy-AzStorageBlob -SrcBlob $Source -SrcContainer $SrcContainer -DestContainer $DestContainer -DestBlob $Destination -Context $Context -Verbose:$VerbosePreference -Force:$Force -Confirm:$ConfirmPreference
if ($Copy -and ($Force -or $PSCmdlet.ShouldProcess($Source, 'Remove-AzStorageBlob'))) {
Remove-AzStorageBlob -Blob $Source -Container $SrcContainer -Context $Context -Verbose:$VerbosePreference -Force:$Force -Confirm:$ConfirmPreference
}
}
end {
$Copy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment