Trouble connecting to azure file share

Function upload files to file share

Function UploadFiles  
{  
Write-Host -ForegroundColor Green "Upload files to file share.."    
## Get the storage account context  
$ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name 
$storageAccName).Context  
## Get the file share  
$fileShare=Get-AzStorageShare -Context $ctx -Name $fileShareName  
## Upload the file  
Set-AzStorageFileContent -Share $fileShare -Source $fileName -Path $folderPath -Force  
}  

Here is the error I get

Set-AzStorageFileContent : Cannot bind parameter 'Share'. Cannot convert the 
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" value of 
 type 
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" to type 
 "Microsoft.Azure.Storage.File.CloudFileShare".
  At line:22 char:37
 +     Set-AzStorageFileContent -Share $fileShare -Source $fileName -Pat ...
 +                                     ~~~~~~~~~~
 + CategoryInfo          : InvalidArgument: (:) [Set-AzStorageFileContent], 
 ParameterBindingException
 + FullyQualifiedErrorId : 

CannotConvertArgumentNoMessage,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.SetAzureStorageFileContent


Solution 1:

As mentioned in the comments, the error is self-explanatory. Get-AzStorageShare returns an object of type AzureStorageFileShare whereas Set-AzStorageFileContent expects -Share parameter of type CloudFileShare (or String).

Two ways to solve this problem:

  1. You can pass the share name (string) and context to Set-AzStorageFileContent. Something like:
Set-AzStorageFileContent -Share $fileShareName -Source $fileName -Path $folderPath -Context $ctx -Force
  1. Use CloudFileShare property of AzureStorageFileShare. Something like:
Set-AzStorageFileContent -Share $fileShare.CloudFileShare -Source $fileName -Path $folderPath -Force