11

I have the below script that needs to remove files from an Azure Storage Container that are older than 7 days and it has suddenly stopped working:

$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

It fails now with the below error:

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

I tried running Uninstall-AzureRm but I get the following error:

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

I then tried running Import-Module Az.Accounts and got this:

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

Does anyone know what I need to do to get this working? I'm running

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14409  1005
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Philip
  • 2,460
  • 4
  • 27
  • 52

3 Answers3

18

You could not mix the Az and AzureRm commands together, change the first line in your script as below, then try it in a new powershell session.

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

If you still get the same error, run a new powershell session as admin, use the command Uninstall-Module as below, then try the script again.

Uninstall-Module -Name AzureRm -AllVersions -Force
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
0

I had this same issue when trying to deploy a Web App to Azure using Octopus deploy with the Run a PowerShell script using an Azure subscription with Azure modules loaded template.

I had Az modules for Azure Powershell installed on the Octopus server:

Install-Module -Name Az -Repository PSGallery -Force

I also uninstalled all AzureRm modules on the Octopus server:

Uninstall-AzureRm

However, when I try deploying I get this error:

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

Here's I fixed it:

The issue was Run a PowerShell script using an Azure subscription with Azure modules loaded template has AzureRm modules loaded on it, which conflicts with the Az modules that I installed on the Octopus server.

Since I did not want to modify my Powershell scripts to use the AzureRm modules, all I had to do was to Run a PowerShell script using an Azure subscription with Azure modules loaded template and use the normal Run a Powershell script template which allowed me to communicate with the Az modules installed on the Octopus server without interfering with the AzureRm modules from Octopus.

This meant that I had to modify my script now had to handle the authentication to the Azure account before it could run any command, which was what the Run a PowerShell script using an Azure subscription with Azure modules loaded template was initially supposed to help me achieve.

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
-3

Use version 3 of azure file copy and it solves the issue.