Azure Powershell:无法在同一会话中导入 Az 和 AzureRM 模块

Phi*_*lip 7 powershell azure azure-powershell

我有以下脚本需要从 Azure 存储容器中删除超过 7 天并且突然停止工作的文件:

$StorageAccountName = "#"
$StorageAccountKey = "#"
$ContainerName = "#"
$CleanupOlderThanDays = [DateTime]::UtcNow.AddDays(-7)
$FolderName = "Daily"

$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx -Prefix $FolderName/ | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupOlderThanDays } |Remove-AzStorageBlob
Run Code Online (Sandbox Code Playgroud)

它现在失败并出现以下错误:

PS C:\backupcopy> .\remove_files_daily.ps1
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the
same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm'
 cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of
 your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
Get-AzStorageBlob : The 'Get-AzStorageBlob' command was found in the module 'Az.Storage', but the module could not be
loaded. For more information, run 'Import-Module Az.Storage'.
At C:\backupcopy\remove_files_daily.ps1:8 char:1
+ Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx -Prefix $ ...
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzStorageBlob:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
Run Code Online (Sandbox Code Playgroud)

我尝试运行 Uninstall-AzureRm,但出现以下错误:

PS C:\backupcopy> Uninstall-AzureRm
WARNING: Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the
same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can
 use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure
Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found
here: https://aka.ms/azps-migration-guide
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the
same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm'
 cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of
 your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the
same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm'
 cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of
 your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
Uninstall-AzureRm : The 'Uninstall-AzureRm' command was found in the module 'Az.Accounts', but the module could not be
loaded. For more information, run 'Import-Module Az.Accounts'.
At line:1 char:1
+ Uninstall-AzureRm
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Uninstall-AzureRm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
Run Code Online (Sandbox Code Playgroud)

然后我尝试运行 Import-Module Az.Accounts 并得到这个:

WARNING: Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the
same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can
 use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure
Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found
here: https://aka.ms/azps-migration-guide
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the
same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm'
 cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of
 your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
Get-ChildItem : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used
in the same script or runbook. If you are running PowerShell in an environment you control you can use the
'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation,
take care that none of your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
At C:\Program Files\WindowsPowerShell\Modules\Az.Accounts\1.6.2\Az.Accounts.psm1:39 char:5
+     Get-ChildItem "$PSScriptRoot\StartupScripts" -ErrorAction Stop |  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (AzureRM.Profile...igration-guide.:String) [Get-ChildItem], RuntimeExc
   eption
    + FullyQualifiedErrorId : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same se
   ssion or used in the same script or runbook. If you are running PowerShell in an environment you control you can u
  se the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Aut
 omation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found he
re: https://aka.ms/azps-migration-guide.,Microsoft.PowerShell.Commands.GetChildItemCommand

WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the
same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm'
 cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of
 your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
Import-Module : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used
in the same script or runbook. If you are running PowerShell in an environment you control you can use the
'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation,
take care that none of your runbooks import both Az and AzureRM modules. More information can be found here:
https://aka.ms/azps-migration-guide.
At line:1 char:1
+ Import-Module Az.Accounts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (AzureRM.Profile...igration-guide.:String) [Import-Module], RuntimeExc
   eption
    + FullyQualifiedErrorId : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same se
   ssion or used in the same script or runbook. If you are running PowerShell in an environment you control you can u
  se the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Aut
 omation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found he
re: https://aka.ms/azps-migration-guide.,Microsoft.PowerShell.Commands.ImportModuleCommand
Run Code Online (Sandbox Code Playgroud)

有谁知道我需要做什么才能使其正常工作?我在跑

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14409  1005
Run Code Online (Sandbox Code Playgroud)

Joy*_*ang 14

您无法将AzAzureRm命令混合在一起,更改脚本中的第一行,如下所示,然后在新的 powershell 会话中尝试。

$Ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Run Code Online (Sandbox Code Playgroud)

如果您仍然遇到相同的错误,请以管理员身份运行新的 powershell 会话,使用如下命令Uninstall-Module,然后再次尝试该脚本。

Uninstall-Module -Name AzureRm -AllVersions -Force
Run Code Online (Sandbox Code Playgroud)