Az和AzureRM模块不能在同一会话中导入

时间:2019-10-03 01:17:51

标签: azure powershell azure-powershell

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

 #region DilKursu
        routes.MapRoute(
            name: "DilKursuUlkeEyaletSehir",
            url: "dil-kurslari/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}",
            defaults: new
            {
                controller = "DilKursu",
                action = "ShowDilKursuMenu",
                ulke = UrlParameter.Optional,
                eyalet = UrlParameter.Optional,
                sehir = UrlParameter.Optional,
                ilce = UrlParameter.Optional,
                firma = UrlParameter.Optional,
            });

        routes.MapRoute(
            name: "DilKursuAll",
            url: "dil-kursu/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}/{kursAdi}",
            defaults: new
            {
                controller = "DilKursu",
                action = "DilKursuDetay",
                ulke = UrlParameter.Optional,
                eyalet = UrlParameter.Optional,
                sehir = UrlParameter.Optional,
                ilce = UrlParameter.Optional,
                firma = UrlParameter.Optional,
                kursAdi = UrlParameter.Optional,
            });

        #endregion
        #region HomeIndex2
        routes.MapRoute(
            name: "HomeLang2",
            url: "",
            defaults: new { controller = "Home", action = "HomeIndex" }
        );
        #endregion

现在失败,并显示以下错误:

$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

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

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

然后我尝试运行Import-Module Az.Accounts并获得了此信息:

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

有人知道我需要做些什么才能使它工作吗?我正在跑步

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

2 个答案:

答案 0 :(得分:2)

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

$Ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

如果仍然出现相同的错误,请以管理员身份运行一个新的powershell会话,使用如下所示的命令Uninstall-Module,然后重试该脚本。

Uninstall-Module -Name AzureRm -AllVersions -Force

答案 1 :(得分:0)

我在尝试使用 Octopus deployRun a PowerShell 脚本将 Web 应用 部署到 Azure 时遇到了同样的问题使用加载了 Azure 模块的 Azure 订阅模板。

我在 Octopus 服务器上安装了 Azure Powershell 的 Az 模块:

Install-Module -Name Az -Repository PSGallery -Force

我还卸载了八达通服务器上的所有 AzureRm 模块:

Uninstall-AzureRm

但是,当我尝试部署时出现此错误:

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. 

Invoke-Expression : The 'New-AzStorageAccount' 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:\Octopus\Work\20210326200138-344663-101\Octopus.AzureContext.ps1:143  
char:5 
+     Invoke-Expression ". $OctopusAzureTargetScript $OctopusAzureTarge ... 
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo          : ObjectNotFound: (New-AzStorageAccount:String) [I  
   nvoke-Expression], CommandNotFoundException 
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule,Microsoft.PowerSh  
   ell.Commands.InvokeExpressionCommand 
 
Fatal
The remote script failed with exit code 1 
The action Create Azure Storage Account for Test, Staging and Production on the Octopus Server failed

这是我修复的

问题是使用加载了 Azure 模块的 Azure 订阅运行 PowerShell 脚本 模板上加载了 AzureRm 模块,这与我安装的 Az 模块 冲突八达通服务器。

由于我不想修改我的 Powershell 脚本以使用 AzureRm 模块,我所要做的就是使用加载了 Azure 模块的 Azure 订阅运行 PowerShell 脚本模板并使用正常的运行 Powershell 脚本模板,它允许我与 Octopus 服务器上安装的 Az 模块进行通信,而不会干扰 Octopus 的 AzureRm 模块。

这意味着我必须修改我的脚本,现在必须在它可以运行任何命令之前处理对 Azure 帐户的身份验证,这就是使用加载了 Azure 模块的 Azure 订阅运行 PowerShell 脚本 strong> 模板最初应该帮助我实现。

仅此而已。

我希望这会有所帮助