How to find and replace commands from AzureAD and MSOnline deprecated modules in your scripts for the Graph ones

How to find and replace commands from AzureAD and MSOnline deprecated modules in your scripts for the Graph ones

ยท

3 min read

If you are using PowerShell modules AzureAD, AzureADPreview, or MSOnline in your scripts you should be aware that they are gonna be retired on 30.6.2023. Moreover licensing MSOL cmdlets will stop working on 31.3.2023! So now is a good time to start replacing these modules in your scripts and the modules you are using.

What you should use from now on is Microsoft Graph SDK.


TL;DR

Use functions Get-ModuleCommandUsedInCode and Get-CorrespondingGraphCommand (from my module DependencySearch) like this ๐Ÿ‘‡

Install-Module DependencySearch

$scriptsDir = "<pathToFolderWithYourPS1Scripts>"

# get ps1 scripts
$scripts = Get-ChildItem $scriptsDir -Recurse -File | ? Extension -in ".ps1", ".psm1" | select -exp FullName

# load modules whose cmdlets should be searched
$deprecatedModule = @()
"AzureAD", "AzureADPreview", "MSOnline", "AzureRM" | % {
    $module = Get-Module $_ -ListAvailable
    if ($module) {
        $deprecatedModule += $module
    } else {
        Write-Warning "Module $_ isn't available on your computer"
    }
}

# output used cmdlets and corresponding Graph API cmdlet if available
$scripts | % {
    Get-ModuleCommandUsedInCode -scriptPath $_ -module $deprecatedModule | select *, @{n = 'GraphCommand'; e = { (Get-CorrespondingGraphCommand $_.command).GraphCommand } } | select * -exclude module | ft -AutoSize
}

And the result will look like this ๐Ÿ‘‡ a.k.a. on the left side you'll see commands that need to be replaced and on the right side by which Graph command


Find commands from the deprecated module(s) in my scripts

For identifying scripts where commands from given (deprecated in our case) modules are used I've created a function Get-ModuleCommandUsedInCode (in module DependencySearch)

The function requires two parameters:

  • script path (where commands will be searched)

  • module(s) object (retrieved using Get-Module)

Function:

  • gets the name of cmdlets, functions and aliases defined in given module(s)

  • gets all used commands in a given file (using AST) and outputs just those defined in given module(s)


Find the corresponding Graph command to the deprecated one

Now when we are able to extract deprecated commands using Get-ModuleCommandUsedInCode we need a way to "translate" such commands to their new Graph API version.

There is an official page with a "translation map" so I've created a function Get-CorrespondingGraphCommand that:

  • extracts HTML tables from this page (using ConvertFrom-HTMLTable function from CommonStuff module)

  • caches the result object to your user profile

  • uses such information to get the corresponding Graph API command


Summary

Thanks to functions Get-ModuleCommandUsedInCode and Get-CorrespondingGraphCommand that were explained in previous sections you are now able to easily identify all your scripts where those deprecated modules are used, moreover, you know which cmdlet from Microsoft Graph SDK should be used instead (in case there is some ๐Ÿ˜).

Hopefully, that will make your migration journey easier ๐Ÿ™‚

Did you find this article valuable?

Support Ondrej Sebela by becoming a sponsor. Any amount is appreciated!

ย