some issue with my Class :(

Class AzBlobContainer{
    [String]$ResourceGroup
    [String]$Location       = 'ukwest'
    [String]$StorageAccName
    [String]$StorageSkuName = 'Standard_LRS'
    [String]$StorageKind    = 'StorageV2'
    [String]$ContainerName

    AzBlobContainer(){}
   
    AzBlobContainer([String]$ResourceGroup, [String]$Location, [String]$StorageAccName, [String]$StorageSkuName, [String]$StorageKind, [String]$ContainerName)
    {        
        $this.ResourceGroup  = $ResourceGroup
        $this.Location       = $Location
        $this.StorageAccName = $StorageAccName.ToLower()
        $this.StorageSkuName = $StorageSkuName
        $this.StorageKind    = $StorageKind
        $this.ContainerName  = $ContainerName.ToLower()
    }
    [bool]testStorageAccountName(){
        #Only characters lowercase a to z.
        $Regex = '^[a-z]+$'
        return ($this.StorageAccName -cmatch $Regex)
    }
    [bool]testResourceGroup(){
        return ($null -ne (Get-AzResourceGroup -Name $this.ResourceGroup -Location $this.Location -ErrorAction 'SilentlyContinue'))
    }
    [bool]testStorageAccount(){
        return ($null -ne (Get-AzStorageAccount -ResourceGroupName $this.ResourceGroup -Name $this.StorageAccName -ErrorAction 'SilentlyContinue'))
    }
    [bool]testBlobContainer(){
        $Context = (Get-AzStorageAccount -ResourceGroup $this.ResourceGroup -Name $this.StorageAccName).Context
        return ($null -ne (Get-AzStorageContainer -Name $this.ContainerName -Context $Context -ErrorAction 'SilentlyContinue'))
    }

    [void]newResourceGroup(){
        If (!($this.testResourceGroup())){
            Try{
                New-AzResourceGroup -Name $this.ResourceGroup -Location $this.Location 
            }Catch{
                Write-Warning ":("
                Write-Warning "Error Unable to create the Resource Group."
		Write-Warning "$Error.Exception"
                break
            }
        }
        Else{
            Write-Warning "$($this.ResourceGroup) resource group already exists!"
        }
    }

    [void]newStorageAccount(){
        If ($this.testStorageAccountName()){
            If (!($this.testResourceGroup())){
                $this.newResourceGroup()
            }
            If (!($this.testStorageAccount())){
                Try{       
                    New-AzStorageAccount -ResourceGroupName $this.ResourceGroup -AccountName $this.AccountName -Location $this.Location -SkuName $this.StorageSkuName -Kind $this.StorageKind
                }Catch{                    
                    Write-Warning ":("
                    Write-Warning "Error Unable to create the Storage Account."
		    Write-Warning "$Error.Exception"
                    break
                }
            }
            Else { Write-Warning "$($this.StorageAccName) storage account already exists!"}
        }
        Else{
            Write-Warning "$($this.StorageAccName) is invalid name."
        }
    }    
}

Function New-BlobContainer{
    Param(
        [Parameter(Mandatory)]
        [String]$ResourceGroup,
        [String]$Location,
        [Parameter(Mandatory)]
        [String]$AccountName,
        [String]$SkuName,
        [String]$Kind,
        [Parameter(Mandatory)]
        [String]$ContainerName
    )

    $BlobObj = New-Object AzBlobContainer
    $BlobObj.ResourceGroup  = $ResourceGroup
    $BlobObj.Location       = $Location
    $BlobObj.StorageAccName = $AccountName
    $BlobObj.StorageSkuName = $SkuName
    $BlobObj.StorageKind    = $Kind
    $BlobObj.ContainerName  = $ContainerName.ToLower()
    
    #$BlobObj.newResourceGroup()
    $BlobObj.newStorageAccount()    
}
New-BlobContainer -ResourceGroup 'RG-Stor-Test-one' -Location 'ukwest' -AccountName 'stortestdev' -SkuName 'Standard_LRS' -Kind 'StorageV2' -ContainerName 'sqlbackup'

Could someone take a look at my script? For some reasons, newStorageAccount() method is throwing some errors. If I copy and paste this line below and run it on PS console, it works. But when running the whole script it fails.

New-AzStorageAccount -ResourceGroupName $this.ResourceGroup -AccountName $this.AccountName -Location $this.Location -SkuName $this.StorageSkuName -Kind $this.StorageKind

Creating resource group works fine but just something weird happens when creating the storage account on Azure.
Thanks in advance for your help.

Please share the error message you get when you execute this as script.

WARNING: :(
WARNING: Error Unable to create the Storage Account.
WARNING: Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and
then try the command again. The Resource 'Microsoft.Storage/storageAccounts/stortestdev' under resource group 'RG-Stor-Test-Dev' was not
found..Exception

Not sure why it’s saying it’s null. If I create a new blob container in an existing storage account with the following code. It works fine.

[void]newBlobContainer(){       
        If (!($this.testStorageAccount())){
            $this.newStorageAccount()
        }
        If (!($this.testBlobContainer())){
            $StorAcc = Get-AzStorageAccount -ResourceGroupName $this.ResourceGroup -AccountName $this.StorageAccName
            Try{
                New-AzStorageContainer -Name $this.ContainerName -Context $StorAcc.Context -Permission Blob
            }Catch{
                Write-Warning ":("
                Write-Warning "Error Unable to create the Blob Container."
                break
            }
        }
        Else{
            Write-Warning "$($this.ContainerName) container already exists!"
        }        
    }

I am confused.

Hi Prasoon

Thanks for your quick response I think I’ve got this now. I see it’s null due to -AccountName.
New-AzStorageAccount -ResourceGroupName $this.ResourceGroup -AccountName $this.AccountName

It should’ve been like this. Blind me what a typo!
New-AzStorageAccount -ResourceGroupName $this.ResourceGroup -Name $this.AccountName

:slight_smile: