|
| 1 | +--- |
| 2 | +title: Migrate Azure PowerShell scripts from AzureRM to Az |
| 3 | +description: Learn the steps and tools for migrating scripts from the AzureRM module to the new Az module. |
| 4 | +author: sptramer |
| 5 | +ms.author: sttramer |
| 6 | +manager: carmonm |
| 7 | +ms.devlang: powershell |
| 8 | +ms.topic: conceptual |
| 9 | +ms.date: 11/01/2018 |
| 10 | +--- |
| 11 | + |
| 12 | +# Migrate from AzureRM to Azure PowerShell Az |
| 13 | + |
| 14 | +With the introduction of the Az module, older scripts which use the AzureRM modules and cmdlets no |
| 15 | +longer work automatically. No migration that changes command names is ever convenient or easy to |
| 16 | +handle. In order to help with the process of changing all your scripts over from AzureRM to Az, |
| 17 | +this article will outline steps, tools, and scripts that you can take advantage of to automate |
| 18 | +the process of migrating to Az as easy as possible. |
| 19 | + |
| 20 | +Each section of this article outlines a step in the process, and it's recommended that you follow |
| 21 | +them in order unless you've already completed the process outlined in a single step. |
| 22 | + |
| 23 | +## Ensure your existing scripts work with the latest AzureRM release |
| 24 | + |
| 25 | +This is the most important step! Run your existing scripts, and make sure that they work with the |
| 26 | +_latest_ release of AzureRM (__6.11.0__). If your scripts do not work, make sure to go through and read |
| 27 | +the [AzureRM migration guide](/powershell/azure/migration-guide.6.0.0). |
| 28 | + |
| 29 | +## Install the Azure PowerShell Az module |
| 30 | + |
| 31 | +The first step is to install the Az module on your platform. This _does_ require uninstalling |
| 32 | +the AzureRM module, but the following steps will outline how you can get compatibility with the old |
| 33 | +command set until your migration is completed. |
| 34 | + |
| 35 | +To install the Azure PowerShell Az module, follow these steps: |
| 36 | + |
| 37 | +* [Uninstall the AzureRM module](/powershell/azure/uninstall-azurerm-ps?view=azurermps-6.11.0). Make sure that |
| 38 | +you remove _all_ installed versions of AzureRM, not just the most recent version. |
| 39 | +* [Install the Az module](install-az-ps.md) |
| 40 | + |
| 41 | +## <a name="aliases"/>Enable AzureRM alias mode |
| 42 | + |
| 43 | +With AzureRM uninstalled and your scripts working with the latest AzureRM version, now is the time to |
| 44 | +enable the compatibility mode for the Az module. This is done with the command |
| 45 | + |
| 46 | +```powershell-interactive |
| 47 | +Enable-AzureRmAlias -Scope CurrentUser |
| 48 | +``` |
| 49 | + |
| 50 | +This enables the ability to use old cmdlet names with the `Az` module installed, through aliases. These |
| 51 | +aliases are written to the user profile for the selected scope. If no user profile exists, one is created. |
| 52 | + |
| 53 | +> [!WARNING] |
| 54 | +> |
| 55 | +> You can use a different `-Scope` for this command, but it's not recommended! Because the aliases are written to |
| 56 | +> the user profile for the selected scope, keep enabling them to as limited a scope as possible. Enabling aliases |
| 57 | +> system-wide could also cause issues for other users which have AzureRM installed in their local scope. |
| 58 | +
|
| 59 | +Once the alias mode is enabled, run your scripts again to confirm that they still function as expected. |
| 60 | + |
| 61 | +## Change module imports and cmdlet names |
| 62 | + |
| 63 | +In general, the module names have been changed so that `AzureRM` becomes `Az`, and the same for cmdlets. |
| 64 | +For example, the `AzureRM.Compute` module has been renamed to `Az.Compute`. `New-AzureRmVM` has become `New-AzVM`. |
| 65 | +There are some exceptions to this naming change that you should be aware of before doing any renaming: |
| 66 | + |
| 67 | +| AzureRM module | Az module | |
| 68 | +|----------------|-----------| |
| 69 | +| Azure.Storage | Az.Storage | |
| 70 | +| Azure.AnalysisServices | Az.AnalysisServices | |
| 71 | +| AzureRM.DataFactories | Az.DataFactory | |
| 72 | +| AzureRM.DataFactoryV2 | Az.DataFactory | |
| 73 | +| AzureRM.RecoveryServices.Backup | Az.RecoveryServices | |
| 74 | +| AzureRM.RecoveryServices.SiteRecovery | Az.RecoveryServices | |
| 75 | + |
| 76 | +In the case of `Azure.Storage` and `Azure.AnalysisServices`, the associated cmdlets have also been renamed |
| 77 | +so that `Azure` is replaced with `Az`. For example, `Get-AzureStorageBlob` has been renamed to `Get-AzStorageBlob`. |
| 78 | + |
| 79 | +To help with the renaming of modules and cmdlets in your scripts, you can use the following PowerShell script |
| 80 | +to automate most of the work. You will still need to make sure that the script runs at the end of the edits, |
| 81 | +but the number of changes you have to make on your own will hopefully be much fewer! |
| 82 | + |
| 83 | +```powershell-interactive |
| 84 | +function Update-AzureRmScript { |
| 85 | + param( |
| 86 | + [Parameter(mandatory=$true)] |
| 87 | + [string]$File |
| 88 | + ) |
| 89 | +
|
| 90 | + $replacements = @( |
| 91 | + @{ |
| 92 | + "name"="Azure.Storage" |
| 93 | + "module"="Az.Storage" |
| 94 | + }, |
| 95 | + @{ |
| 96 | + "name"="Azure.AnalysisServices" |
| 97 | + "module"="Az.AnalysisServices" |
| 98 | + }, |
| 99 | + @{ |
| 100 | + "name"="AzureRM.DataFactories" |
| 101 | + "module"="Az.DataFactory" |
| 102 | + "moduleOnly"=$true |
| 103 | + }, |
| 104 | + @{ |
| 105 | + "name"="AzureRM.DataFactoryV2" |
| 106 | + "module"="Az.DataFactory" |
| 107 | + "moduleOnly"=$true |
| 108 | + }, |
| 109 | + @{ |
| 110 | + "name"="AzureRM.RecoveryServices.Backup" |
| 111 | + "module"="Az.RecoveryServices" |
| 112 | + "moduleOnly"=$true |
| 113 | + }, |
| 114 | + @{ |
| 115 | + "name"="AzureRM.RecoveryServices.SiteRecovery" |
| 116 | + "module"="Az.RecoveryServices" |
| 117 | + "moduleOnly"=$true |
| 118 | + } |
| 119 | + ) |
| 120 | +
|
| 121 | + echo "Updating $File..." |
| 122 | + $content = Get-Content $file |
| 123 | + for ($i=0; $i -lt $content.Length; $i++) { |
| 124 | + $line = $content[$i] |
| 125 | + if ($line -match "^\s*Import-Module.+(AzureRm)") { |
| 126 | + $content[$i] = $line -replace $matches[1],"Az" |
| 127 | + } |
| 128 | + elseif ($line -match "[A-Za-z]+-(AzureRm)") { |
| 129 | + $content[$i] = $line -replace $matches[1],"Az" |
| 130 | + } |
| 131 | + else { |
| 132 | + foreach ($module in $renames) { |
| 133 | + if ($line -match "^\s*Import-Module.+($($module.Name))") { |
| 134 | + $content[$i] = $line -replace $matches[1],$module.Module |
| 135 | + } |
| 136 | + elseif ((-not $module.moduleOnly) -and ($line -match "[A-Za-z]+-($($module.Module))")) { |
| 137 | + $content[$i] = $line -replace $matches[1],$module.Module |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + Set-Content -Path $file -Value $content -Force |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +> [!IMPORTANT] |
| 147 | +> |
| 148 | +> If you used `AzureRM` as part of any of your function names in your script, those commands will be renamed to |
| 149 | +> use `Az` instead. As long as you also update all scripts which call these functions, that shouldn't |
| 150 | +> cause a problem. Just be aware that this script __will__ change all function and command names |
| 151 | +> containing `AzureRM`. |
| 152 | +
|
| 153 | +## Disable AzureRM aliases |
| 154 | + |
| 155 | +When you've completed your script migration and no longer need AzureRM compatibility, you should disable the cmdlet |
| 156 | +aliases. This is done with the `Disable-AzureRmAlias` command: |
| 157 | + |
| 158 | +```powershell-interactive |
| 159 | +Disable-AzureRmAlias -Scope CurrentUser |
| 160 | +``` |
| 161 | + |
| 162 | +If you provided a different value for `-Scope` when enabling aliases, make sure that you use it here as well. |
| 163 | + |
| 164 | +## Summary |
| 165 | + |
| 166 | +By following these steps, you can update all of your existing scripts to use the new module without a lot of pain or difficulty. If you have any questions or encountered problems with these steps that made your migration difficult, please feel free to comment on this article so that we can improve the instructions. |
0 commit comments