Need help in fixing the Error while creating a storage account to copy the VHD file.

Karthik Sundaresh 0 Reputation points
2023-04-28T11:06:41.4+00:00

A positional parameter cannot be found that accepts argument 'Storage account name'.

try {
    # Create the context for the storage account which will be used to copy the disk to the storage account 
    Write-Log -Message "Attempting to create storage account: $($StorageAccountName)" -Level Info
    $StorageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName -SkuName Standard_LRS -Kind StorageV2 -MinimumTlsVersion TLS1_2 -AllowBlobPublicAccess $false -PublicNetworkAccess disabled -Location $VM.Location
    $StorageAccountKey = Get-AzStorageAccountKey –ResourceGroupName $StorageAccount.ResourceGroupName `–Name $StorageAccount.StorageAccountName
    $DestinationContext = New-AzStorageContext –StorageAccountName $StorageAccount.StorageAccountName `–StorageAccountKey ($StorageAccountKey).Value[0]
    Write-Log -Message "Attempting to create storage account container: $($StorageAccountName)" -Level Info
    $Container = New-AzStorageContainer -Name $StorageContainerName -Permission "Off" -Context $DestinationContext -ErrorAction Stop
    Write-Log -Message "Success: created storage account: $($StorageAccountName) and container: $($StorageAccountName)" -Level Info
}
catch {
    Write-Log -Message "Failed to create storage account for transfer. Exit script" -Level Warn
    Write-Log -Message $_ -Level Warn
    StopIteration
    Exit 1
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,585 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,944 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Sedat SALMAN 13,345 Reputation points
    2023-04-28T11:19:32.9633333+00:00

    To fix the error, simply replace $storageAccountName with $StorageAccountName in the script

    try {
        # Create the context for the storage account which will be used to copy the disk to the storage account 
        Write-Log -Message "Attempting to create storage account: $($StorageAccountName)" -Level Info
        $StorageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup -Name $StorageAccountName -SkuName Standard_LRS -Kind StorageV2 -MinimumTlsVersion TLS1_2 -AllowBlobPublicAccess $false -PublicNetworkAccess disabled -Location $VM.Location
        $StorageAccountKey = Get-AzStorageAccountKey –ResourceGroupName $StorageAccount.ResourceGroupName `–Name $StorageAccount.StorageAccountName
        $DestinationContext = New-AzStorageContext –StorageAccountName $StorageAccount.StorageAccountName `–StorageAccountKey ($StorageAccountKey).Value[0]
        Write-Log -Message "Attempting to create storage account container: $($StorageAccountName)" -Level Info
        $Container = New-AzStorageContainer -Name $StorageContainerName -Permission "Off" -Context $DestinationContext -ErrorAction Stop
        Write-Log -Message "Success: created storage account: $($StorageAccountName) and container: $($StorageAccountName)" -Level Info
    }
    catch {
        Write-Log -Message "Failed to create storage account for transfer. Exit script" -Level Warn
        Write-Log -Message $_ -Level Warn
        StopIteration
        Exit 1
    }
    
    
    0 comments No comments

  2. Rich Matheisen 45,906 Reputation points
    2023-04-29T19:36:33.35+00:00

    These two lines are incorrect:

    $StorageAccountKey = Get-AzStorageAccountKey –ResourceGroupName $StorageAccount.ResourceGroupName `–Name $StorageAccount.StorageAccountName
        $DestinationContext = New-AzStorageContext –StorageAccountName $StorageAccount.StorageAccountName `–StorageAccountKey ($StorageAccountKey).Value[0]
    
    

    They both use an en-dash instead of a hyphen, and the "-Name" and "-StoreAccountKey" parameter both have a backtick escape in front of the eh-dash.

    Also, it's unnecessary use this sort of code $($variableName) when a simple variable name is used within a double quoted string. That's only necessary when for example, you're referencing an objects' property like this: $_.property or $object.property and the string interpolation would use the $<underbar> and $object as the thing your interested in.

    Try this:

    try {
        # Create the context for the storage account which will be used to copy the disk to the storage account 
        Write-Log -Message "Attempting to create storage account: $StorageAccountName" -Level Info
        $StorageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName -SkuName Standard_LRS -Kind StorageV2 -MinimumTlsVersion TLS1_2 -AllowBlobPublicAccess $false -PublicNetworkAccess disabled -Location $VM.Location
        $StorageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $StorageAccount.ResourceGroupName -Name $StorageAccount.StorageAccountName
        $DestinationContext = New-AzStorageContext -StorageAccountName $StorageAccount.StorageAccountName -StorageAccountKey ($StorageAccountKey).Value[0]
        Write-Log -Message "Attempting to create storage account container: $StorageAccountName" -Level Info
        $Container = New-AzStorageContainer -Name $StorageContainerName -Permission "Off" -Context $DestinationContext -ErrorAction Stop
        Write-Log -Message "Success: created storage account: $StorageAccountName and container: $StorageAccountName" -Level Info
    }
    catch {
        Write-Log -Message "Failed to create storage account for transfer. Exit script" -Level Warn
        Write-Log -Message $_ -Level Warn
        StopIteration
        Exit 1
    }
    

  3. Rich Matheisen 45,906 Reputation points
    2023-04-29T20:09:17.55+00:00

    Well, it took 25 minutes for my original answer to appear, so I'll just remove this one. :-/

    0 comments No comments