2

I'm running a PowerShell Function App (~3) which uses the Az PowerShell module to manage a Storage Account. If any of the operations I carry out result in an error, I am unable to check for specific types in a try/catch block.

It is important to note that where there is no error, operations using the Az.Storage module are successful.

For example, if I were to try and delete a container that does not exist, the example below results in the following error -

Unable to find type [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException].

To obtain the type of exception that is returned, I'm using $_.Exception.GetType().fullname.

I've also tried to add the namespace to the script that may produce the exceptions.

using namespace Microsoft.WindowsAzure.Commands.Storage.Common

Example

Class Storage
{
    [AppSettings]$AppSettings = [AppSettings]::GetInstance()
    [Object]$Context

    Storage()
    {
        $key = Get-AzStorageAccountKey -ResourceGroupName $this.AppSettings.StorageAccountResourceGroup -Name $this.AppSettings.StorageAccountName
        $this.Context = New-AzStorageContext -StorageAccountName $this.AppSettings.StorageAccountName -StorageAccountKey $key[0].Value
    }

    [void] DeleteBlobContainer([String]$name)
    {
        try {
            Remove-AzStorageContainer -Name $name -Context $this.Context -Force -ErrorAction Stop
        }
        catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException] {
            throw [ContainerNotFoundException]::new($name)
        }
        catch {
            throw [DustBinException]::new($_.Exception.Message)
        }
    }
}

Update

When calling an HTTP triggered function, I am able to see that the Az.Storage module is installed. This is expected, given operations that require the module are successful -

Get-Module -Name Az.Storage -ListAvailable | Select-Object Name, Version, ModuleBase | ConvertTo-Json
[
  {
    "Name": "Az.Storage",
    "Version": {
      "Major": 3,
      "Minor": 0,
      "Build": 0,
      "Revision": -1,
      "MajorRevision": -1,
      "MinorRevision": -1
    },
    "ModuleBase": "C:\\Users\\dgard\\AppData\\Local\\AzureFunctions\\DustBin\\ManagedDependencies\\201202095548376.r\\Az.Storage\\3.0.0"
  }
]

However, if copy the module to .\bin and include a Module manifest to require Microsoft.Azure.Storage.Common.dll, as suggested in this question, the type is still not found.

New-ModuleManifest ./Modules/StorageHelper/StorageHelper.psd1 -RootModule StorageHelper.psm1 -RequiredAssemblies .\bin\Az.Storage\3.0.0\Microsoft.Azure.Storage.Common.dll

To be sure I was adding the correct assembly, I have updated the manifest to include every single assembly in the Az.Storage module, but the type is still not found.

David Gard
  • 8,944
  • 25
  • 89
  • 182
  • This is `ResourceNotFoundException`. I haven't tried it yet. But I hope you will be able to get it. – Ranadip Dutta Dec 03 '20 at 16:25
  • The [exception type](https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.commands.storage.common.resourcenotfoundexception?view=azurerm-ps) is public, so your symptom is curious. What happens when you execute `[Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException]` stand-alone? A - suboptimal - workaround would be to do string comparison on `$_.Exception.GetType().ToString()` in a single, catch-all `catch` block. – mklement0 Dec 03 '20 at 16:35
  • Is this code part of a `class` definition? – Mathias R. Jessen Dec 04 '20 at 16:16
  • @MathiasR.Jessen, yes it is. I didn't realise that would make a difference - I'll update the example in my question. – David Gard Dec 04 '20 at 17:48
  • @DavidGard can you try to install the latest stable version of [Az.Storage 3.0.0](https://www.powershellgallery.com/packages/Az.Storage/3.0.0)? I'm using it and your code can work well. – Ivan Yang Dec 07 '20 at 07:06
  • @IvanYang - I already have 3.0.0 installed. I'll update my question to make that more obvious. – David Gard Dec 07 '20 at 10:38

1 Answers1

0

In my question I added an update mentioning that I had tried to add a module manifest requiring all of the Az.Storage assemblies; this was not quite correct...

I had copied the list of required assemblies from the module manifest included with the Az.Storage module, but this did not include Microsoft.Azure.PowerShell.Cmdlets.Storage.dll. Using a module manifest to require this assembly (just this one, no others required) has worked.

New-ModuleManifest ./Modules/StorageHelper/StorageHelper.psd1 -RootModule StorageHelper.psm1 -RequiredAssemblies .\bin\Az.Storage\3.0.0\Microsoft.Azure.PowerShell.Cmdlets.Storage.dll 
David Gard
  • 8,944
  • 25
  • 89
  • 182