Azure PowerShell 会话尚未正确初始化。请导入模块并重试

The Azure PowerShell session has not been properly initialized. Please import the module and try again

我正在编写我的第一个 powershell 脚本以将数据从 CSV 加载到 Azure 存储 table。我不确定为什么

$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName)[0].Value

正在抛出错误:

运行 Get-Module 给出了这个结果:

这是我到目前为止编写的代码片段:

# Step 1, Set variables
# Enter Table Storage location data 
$resourceGroupName = "ComputeTesting"
$storageAccountName = 'computetestingdiag'
$tableName = 'strtable'
$dateTime = get-date

# Step 2, Login to your Azure subscription
$sub = Get-AzSubscription -ErrorAction SilentlyContinue
if(-not($sub))
{
    Connect-AzAccount
}

# If you have multiple subscriptions, set the one to use
# Select-AzSubscription -SubscriptionId "<SUBSCRIPTIONID>"

# Step 3, Get the access key for the Azure Storage account
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName)[0].Value

# Step 4, Connect to Azure Table Storage
$storageCtx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$table = Get-AzureStorageTable -Name $tableName -Context $storageCtx

我检查了一些类似的问题,我了解到卸载并重新安装 Azure 模块可能会有帮助。虽然我还没有尝试过,但是还有其他解决方法吗?任何帮助都会非常有帮助。

根据您提供的脚本,您在同一个 PowerShell 会话中使用 AzAzureRM 模块。可能会引起冲突。我建议你在一个会话中使用一个模块。

例如

$resourceGroupName = "<>"
$storageAccountName = '<>'
$tableName = '<>'

Connect-AzAccount
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName)[0].Value

$storageCtx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$table = Get-AzStorageTable -Name $tableName -Context $storageCtx

有关如何管理 Azure table 存储的更多详细信息,请参阅 document