Buzz
Buzz

Reputation: 325

Accessing Storage Account from Azure Function (Powershell)

i am trying to access a Storage Account via Powershell Azure Function in this way:

using namespace System.Net
using namespace Microsoft.WindowsAzure.Storage
using namespace Microsoft.WindowsAzure.Storage.Blob


$storageAccount = [CloudStorageAccount]::Parse("DefaultEndpointsProtocol=https;AccountName=example;AccountKey=example")     
$blobClient = $storageAccount.CreateCloudBlobClient()

$container = $blobClient.GetContainerReference("xyz")

$blobName = "$jobId.xml"

$blob = $container.GetBlockBlobReference($blobName)

But in line 6 the error occurs that [CloudStorageAccount] type could not be found

ERROR: Unable to find type [CloudStorageAccount]. Exception : Type : System.Management.Automation.RuntimeException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Unable to find type [CloudStorageAccount]. HResult : -2146233087 TargetObject : CloudStorageAccount CategoryInfo : InvalidOperation: (CloudStorageAccount:TypeName) [], ParentContainsErrorRecordException FullyQualifiedErrorId : TypeNotFound InvocationInfo : ScriptLineNumber : 30 OffsetInLine : 23 HistoryId : -1 ScriptName :

It seems for me that the namespaces are ignored.

Does anyone have an idea what I am doing wrong or is there another way to access the Storage Account from within my Azure Powershell Function?

Upvotes: 0

Views: 1127

Answers (1)

SiddheshDesai
SiddheshDesai

Reputation: 8195

I tried connecting to my Azure Storage account with my Function app like below:-

I created one Powershell Function and added one HTTP Trigger to it:-

enter image description here

I selected my Function > App files > requirements.psd1 and uncommented Az Powershell module and saved the file so my Function can run Azure Powershell command lets successfully, Refer below:-

enter image description here

Now, I went to my Function app and ran a sample Get-AzStorageAccount command by connecting to it with my Service Principal like below:-

Code:-

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Import-Module Az.Resources
Import-Module Az.Accounts
Import-Module Az.Storage

# Remember to change the AppId, AppSecret and TenantID values accordingly.

$AppId="<client-id>"
$AppSecret="<client-secret>"

# We need to convert the password to a secute string

$SecureSecret = $AppSecret | ConvertTo-SecureString -AsPlainText -Force

# We then create a new object to encapsulate the Application ID and secret

$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $AppId,$SecureSecret

$TenantID="<tenant-id>"

# We can then connnect to our Azure account via the use of the Service Principal

Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID 

$AccountName="siliconstrg5438"
$ResourceGroupName="siliconrg67"

$StorageAccount=Get-AzStorageAccount -Name $AccountName -ResourceGroupName $ResourceGroupName

$StorageAccount

Selected HTTP Get method and ran the function with above code:-

enter image description here

Output- Got connected to my Storage Account and got Storage account details like below:-

enter image description here

When I ran your command, I received an error :-

enter image description here

023-03-24T05:57:31.974 [Error] ERROR: Unable to find type [CloudStorageAccount].Exception             :Type        : System.Management.Automation.RuntimeExceptionErrorRecord :Exception             :Type    : System.Management.Automation.ParentContainsErrorRecordExceptionMessage : Unable to find type [CloudStorageAccount].HResult : -2146233087TargetObject          : CloudStorageAccountCategoryInfo          : InvalidOperation: (CloudStorageAccount:TypeName) [], ParentContainsErrorRecordExceptionFullyQualifiedErrorId : TypeNotFoundInvocationInfo        :ScriptLineNumber : 33OffsetInLine     : 1HistoryId        : -1ScriptName       : C:\home\site\wwwroot\Storage\run.ps1Line             : 

I tweaked your code like below and got the desired results:-

Code:-

using namespace System.Net

# Add-Type -AssemblyName "Microsoft.WindowsAzure.Storage, Version=9.3.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"


# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Import-Module Az.Resources
Import-Module Az.Accounts
Import-Module Az.Storage

# Remember to change the AppId, AppSecret and TenantID values accordingly.

$AppId="<client-id>"
$AppSecret="<client-secret>"

# We need to convert the password to a secute string

$SecureSecret = $AppSecret | ConvertTo-SecureString -AsPlainText -Force

# We then create a new object to encapsulate the Application ID and secret

$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $AppId,$SecureSecret

$TenantID="<tenant-id>"

# We can then connnect to our Azure account via the use of the Service Principal

Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID 

$storageAccountName = "siliconstrg5438"
$storageAccountKey = "<storage-account-key>"

$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

$containerName = "xyz"
$blobName = "jobId.xml"

$blob = Get-AzStorageBlob -Context $ctx -Container $containerName -Blob $blobName

$blob

Output:-

enter image description here

enter image description here

Upvotes: 0

Related Questions