Last active
March 14, 2024 19:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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