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.