Copy blob to and from different containers

Raj D 586 Reputation points
2022-11-11T02:21:28.2+00:00

Greetings!!!

I'm using powershell from an azure automation runbook to copy blob from one source container to a different destination container using managed identity and run into errors. Could you please help me point where exactly I'm going wrong.

Code:

param (  
    [Parameter()]  
    [string] $storageAccount = "myStorageAccount",  
    [string] $resourceGroup = "myResourceGroup",  
    [string] $srcBlob = "myCSV.csv",  
    [string] $destBlob = "myNewCSV.csv",  
    [string] $srcContainer = "mySrcCont",  
    [string] $destContainer = "myDestCont"  
    )   
  
# Connect to Azure using managed identity  
$AzureContext = (Connect-AzAccount -Identity).context  
Disable-AzContextAutosave -Scope Process  
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext   
$storageContext = New-AzStorageContext -StorageAccountName $storageAccount -UseConnectedAccount   
# Connect to Storage Account  
try  
{  
$storage = Get-AzStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup  
$copyOperation = Start-AzStorageBlobCopy -SrcBlob $srcBlob -SrcContainer $srcContainer -DestBlob $destBlob -DestContainer $destContainer -Context $storageContext  
$copyOperation | Get-AzStorageBlobCopyState  
Write-Output $copyOperation  
}  
	catch   
	{  
		$errmsg = $_.Exception.Message    
		Write-Output $errmsg   
	}  

Error:

The input object cannot be bound because it did not contain the information required to bind all mandatory parameters:  CloudBlob  

P.S: Also, unable to capture the copy status using Get-AzStorageBlobCopyState.

Thanks in advance.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,160 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 20,991 Reputation points
    2022-11-14T09:43:05.58+00:00

    Hi @Raj D ,

    I see that you are trying to copy a blob from one container to another using Start-AzStorageBlobCopy cmdlet. I modified the script shared in question a bit and was able to run it successfully. Refer to the steps below for more details.

    1. Ensure that the Azure Automation's managed identity has Storage Blob Data Contributor assigned on the storage account. On storage account, under "Access Control (IAM)" it should look like below:
    259939-image.png

    2. I modified the try{} catch{} block - mainly to wait until the operation succeeds/fails -
    260085-image.png

    3. Notice the $_.Exception.Message and $_.ScriptStackTrace in catch{} -- these have been added so that the line number can also be obtained where the error occurred. In error shared in question, the source of error was unclear.

    Hope this helps. Please let me know if you have any questions.

    ---
    Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.


0 additional answers

Sort by: Most helpful