Модули Az и AzureRM не могут быть импортированы в одном сеансе - PullRequest
1 голос
/ 03 октября 2019

У меня есть скрипт ниже, который должен удалить файлы из контейнера хранения 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

Теперь происходит сбой с ошибкой ниже:

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

Я попытался запустить 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

Затем я попытался запустить 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

Кто-нибудь знает, что мне нужно сделать, чтобы это работало? Я бегу

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14409  1005

1 Ответ

1 голос
/ 03 октября 2019

Вы не можете смешивать команды Az и AzureRm вместе, измените первую строку в вашем скрипте, как показано ниже, а затем попробуйте в новом сеансе powershell.

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

ЕслиВы по-прежнему получаете ту же ошибку, запускаете новый сеанс powershell от имени администратора, используете команду Uninstall-Module, как показано ниже, затем попробуйте снова выполнить скрипт.

Uninstall-Module -Name AzureRm -AllVersions -Force
...