PSAzureMigrationAdvisor.psm1

$script:ModuleRoot = $PSScriptRoot
$script:ModuleVersion = (Import-PowerShellDataFile -Path "$($script:ModuleRoot)\PSAzureMigrationAdvisor.psd1").ModuleVersion

# Detect whether at some level dotsourcing was enforced
$script:doDotSource = Get-PSFConfigValue -FullName PSAzureMigrationAdvisor.Import.DoDotSource -Fallback $false
if ($PSAzureMigrationAdvisor_dotsourcemodule) { $script:doDotSource = $true }

<#
Note on Resolve-Path:
All paths are sent through Resolve-Path/Resolve-PSFPath in order to convert them to the correct path separator.
This allows ignoring path separators throughout the import sequence, which could otherwise cause trouble depending on OS.
Resolve-Path can only be used for paths that already exist, Resolve-PSFPath can accept that the last leaf my not exist.
This is important when testing for paths.
#>


# Detect whether at some level loading individual module files, rather than the compiled module was enforced
$importIndividualFiles = Get-PSFConfigValue -FullName PSAzureMigrationAdvisor.Import.IndividualFiles -Fallback $false
if ($PSAzureMigrationAdvisor_importIndividualFiles) { $importIndividualFiles = $true }
if (Test-Path (Resolve-PSFPath -Path "$($script:ModuleRoot)\..\.git" -SingleItem -NewChild)) { $importIndividualFiles = $true }
if ("<was compiled>" -eq '<was not compiled>') { $importIndividualFiles = $true }
    
function Import-ModuleFile
{
    <#
        .SYNOPSIS
            Loads files into the module on module import.
         
        .DESCRIPTION
            This helper function is used during module initialization.
            It should always be dotsourced itself, in order to proper function.
             
            This provides a central location to react to files being imported, if later desired
         
        .PARAMETER Path
            The path to the file to load
         
        .EXAMPLE
            PS C:\> . Import-ModuleFile -File $function.FullName
     
            Imports the file stored in $function according to import policy
    #>

    [CmdletBinding()]
    Param (
        [string]
        $Path
    )
    
    $resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($Path).ProviderPath
    if ($doDotSource) { . $resolvedPath }
    else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($resolvedPath))), $null, $null) }
}

#region Load individual files
if ($importIndividualFiles)
{
    # Execute Preimport actions
    foreach ($path in (& "$ModuleRoot\internal\scripts\preimport.ps1")) {
        . Import-ModuleFile -Path $path
    }
    
    # Import all internal functions
    foreach ($function in (Get-ChildItem "$ModuleRoot\internal\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {
        . Import-ModuleFile -Path $function.FullName
    }
    
    # Import all public functions
    foreach ($function in (Get-ChildItem "$ModuleRoot\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {
        . Import-ModuleFile -Path $function.FullName
    }
    
    # Execute Postimport actions
    foreach ($path in (& "$ModuleRoot\internal\scripts\postimport.ps1")) {
        . Import-ModuleFile -Path $path
    }
    
    # End it here, do not load compiled code below
    return
}
#endregion Load individual files

#region Load compiled code
<#
This file loads the strings documents from the respective language folders.
This allows localizing messages and errors.
Load psd1 language files for each language you wish to support.
Partial translations are acceptable - when missing a current language message,
it will fallback to English or another available language.
#>

Import-PSFLocalizedString -Path "$($script:ModuleRoot)\en-us\*.psd1" -Module 'PSAzureMigrationAdvisor' -Language 'en-US'

function Convert-AzScriptFile {
    <#
    .SYNOPSIS
        Converts scripts to use the Microsoft.Graph commands from using AzureAD or Msonline.
     
    .DESCRIPTION
        Converts scripts to use the Microsoft.Graph commands from using AzureAD or Msonline.
         
        WARNING! WARNING! WARNING!
        Migration REQUIRES manual intervention!
        Almost all commands have different parameter signatures that often cannot be automatically converted.
        Or some commands are split into two commands.
        Or some commands offer only partial functionality afterwards.
 
        Use Read-AzScriptFile to get a list of all places where you need to adjust things.
        This command really is only there so you don't have to do the part we already can automate,
        but alone this will not be enough!
     
    .PARAMETER Path
        Path to the script-file(s) to convert.
     
    .PARAMETER Type
        What kind of module to migrate.
        Supports scanning for migrating AzureAD or Msonline, defaults to scanning both in parallel.
 
    .PARAMETER TransformPath
        By default, this command uses a predefined set of scanning rules to determine which changes to perform and which warnings to write.
        You can find the rules used in the module's data folder or on Github:
        https://github.com/FriedrichWeinmann/PSAzureMigrationAdvisor/tree/master/PSAzureMigrationAdvisor/data
 
        With this parameter, you can have the tool use instead your own transform/scanning ruleset.
        For more details on how this works and what you can do with it, see the documentation on the Refactor module:
        https://github.com/FriedrichWeinmann/Refactor
 
        If your own customizations could be viable for a larger audience, please consider filing them as a pull request on this project.
     
    .PARAMETER EnableException
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
 
    .PARAMETER WhatIf
        If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
 
    .PARAMETER Confirm
        If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
     
    .EXAMPLE
        PS C:\> Get-ChildItem C:\scripts -Recurse | Convert-AzScriptFile
 
        Migrate all files under c:\scripts recursively and get a list of changes to perform and warnings to consider.
        You may want to create a backup before doing this.
        Also consider the messages written!
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High', DefaultParameterSetName = 'preset')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('FullName')]
        [string[]]
        $Path,

        [Parameter(ParameterSetName = 'preset')]
        [ValidateSet('AzureAD', 'MSOnline')]
        [string[]]
        $Type = @('AzureAD', 'MSOnline'),

        [Parameter(Mandatory = $true, ParameterSetName = 'custom')]
        [string[]]
        $TransformPath,

        [switch]
        $EnableException
    )

    begin {
        Write-PSFMessage -String 'Convert-AzScriptFile.Danger.Warning' -Once Danger -Level Warning

        Clear-ReTokenTransformationSet
        if ($TransformPath) {
            Import-ReTokenTransformationSet -Path $TransformPath
        }
        else {
            if ($Type -contains 'AzureAD') { Import-ReTokenTransformationSet -Path "$script:ModuleRoot\data\azureAD-to-graph.psd1" }
            if ($Type -contains 'MSOnline') { Import-ReTokenTransformationSet -Path "$script:ModuleRoot\data\msol-to-graph.psd1" }
        }
        
        $commandNames = (Get-ReTokenTransformationSet).Name
    }
    process {
        foreach ($pathName in $Path) {
            try { $paths = Resolve-PSFPath -Path $pathName -Provider FileSystem }
            catch {
                Stop-PSFFunction -String 'Convert-AzScriptFile.Path.ResolveError' -StringValues $pathName -EnableException $EnableException -ErrorRecord $_ -Tag path -Target $pathName -Continue -Cmdlet $PSCmdlet
            }
            
            #region Process individual files
            foreach ($filePath in $paths) {
                if ($filePath -notmatch '\.ps1$|\.psm1|\.psf1') {
                    Write-PSFMessage -Level Warning -String 'Convert-AzScriptFile.Path.NoScript' -StringValues $filePath -Target $filePath
                    continue
                }

                $scriptFile = Get-ReScriptFile -Path $filePath
                $relevantTokens = $scriptFile.GetTokens() | Where-Object Name -In $commandNames | Where-Object Type -EQ Command

                if (-not $relevantTokens) {
                    Write-PSFMessage -String 'Convert-AzScriptFile.Path.NoAzCommand' -StringValues $filePath -Target $filePath
                    continue
                }

                Write-PSFMessage -String 'Convert-AzScriptFile.Path.CommandsFound' -StringValues @($relevantTokens).Count, $filePath -Target $filePath

                $results = $scriptFile.Transform($relevantTokens)
                Invoke-PSFProtectedCommand -ActionString 'Convert-AzScriptFile.Converting' -ActionStringValues $results.Count, $filePath -ScriptBlock {
                    $scriptFile.Save($false)
                } -Target $filePath -EnableException $EnableException -PSCmdlet $PSCmdlet -Continue
                $results
            }
            #endregion Process individual files
        }
    }
    end {
        Clear-ReTokenTransformationSet
    }
}

function Read-AzScriptFile {
    <#
    .SYNOPSIS
        Scans scriptfiles for required changes to migrate from AzureAD or Msonline to Microsoft.Graph.
     
    .DESCRIPTION
        Scans scriptfiles for required changes to migrate from AzureAD or Msonline to Microsoft.Graph.
 
        Does not apply any changes, returns one entry per change needed, plus one for each message - error, warning or info - generated from a given file.
         
        Note:
        This scan uses the Refactor module's system of transforms to perform the scan, more details on that
        can be found on the project site:
        https://github.com/FriedrichWeinmann/Refactor
 
        The transform files used _by default_ can be found in the data sub-folder of this module:
        https://github.com/FriedrichWeinmann/PSAzureMigrationAdvisor/tree/master/PSAzureMigrationAdvisor/data
        Use the -TransformPath parameter to override this with your own transforms if desired.
     
    .PARAMETER Path
        Path to the script-file(s) to scan
 
    .PARAMETER Name
        Name of the script to scan
        Used for scenarios where the scanned code is not in a file format (e.g. from Azure DevOps API)
 
    .PARAMETER Content
        Content/Code of the script to scan
        Used for scenarios where the scanned code is not in a file format (e.g. from Azure DevOps API)
     
    .PARAMETER Type
        What kind of module to migrate.
        Supports scanning for migrating AzureAD or Msonline, defaults to scanning both in parallel.
 
    .PARAMETER TransformPath
        By default, this command uses a predefined set of scanning rules to determine which changes to perform and which warnings to write.
        You can find the rules used in the module's data folder or on Github:
        https://github.com/FriedrichWeinmann/PSAzureMigrationAdvisor/tree/master/PSAzureMigrationAdvisor/data
 
        With this parameter, you can have the tool use instead your own transform/scanning ruleset.
        For more details on how this works and what you can do with it, see the documentation on the Refactor module:
        https://github.com/FriedrichWeinmann/Refactor
 
        If your own customizations could be viable for a larger audience, please consider filing them as a pull request on this project.
 
    .PARAMETER ExpandDevOps
        Adds properties to the output for the input's organization, project, repository and branch.
        Only intended for input from scanning Azure DevOps Services repositories using the AzureDevOps.Services.OpenApi module.
     
    .PARAMETER EnableException
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
     
    .EXAMPLE
        PS C:\> Get-ChildItem C:\scripts -Recurse | Read-AzScriptFile
 
        Scan all files under c:\scripts recursively and get a list of changes to perform and warnings to consider.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Path')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Path')]
        [Alias('FullName')]
        [string[]]
        $Path,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Content')]
        [string]
        $Name,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'Content')]
        [string]
        $Content,

        [ValidateSet('AzureAD', 'MSOnline')]
        [string[]]
        $Type = @('AzureAD', 'MSOnline'),

        [string[]]
        $TransformPath,

        [Parameter(ParameterSetName = 'Content')]
        [switch]
        $ExpandDevOps,

        [switch]
        $EnableException
    )

    begin {
        #region Functions
        function Read-ScriptFile {
            [CmdletBinding()]
            param (
                [Refactor.ScriptFile]
                $ScriptFile,

                [string[]]
                $CommandNames,

                [switch]
                $ExpandDevOps
            )

            if ($ExpandDevOps) {
                $splitPath = $ScriptFile.Path -split "/", 4
                $branch = ($ScriptFile.Path -split '\[')[-1].Trim(']')
                $devOpsHash = [ordered]@{
                    Organization = $splitPath[0]
                    Project      = $splitPath[1]
                    Repository   = $splitPath[2]
                    Branch       = $branch
                }
            }

            $hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
            $messageParam = @{
                FunctionName = 'Read-AzScriptFile'
                ModuleName   = 'PSAzureMigrationAdvisor'
                Target       = $ScriptFile.Path
            }
            $relevantTokens = $scriptFile.GetTokens() | Where-Object Name -In $CommandNames | Where-Object Type -EQ Command

            if (-not $relevantTokens) {
                Write-PSFMessage @messageParam -String 'Read-AzScriptFile.Path.NoAzCommand' -StringValues $ScriptFile.Path
                return
            }
            $fileBytes = [System.Text.Encoding]::UTF8.GetBytes($scriptFile.Content)
            $fileHash = $hashAlgorithm.ComputeHash($fileBytes).ForEach{ $_.ToString('x2') } -join ""

            Write-PSFMessage @messageParam -String 'Read-AzScriptFile.Path.CommandsFound' -StringValues @($relevantTokens).Count, $ScriptFile.Path

            $results = $scriptFile.Transform($relevantTokens)
            $messagesUsed = [System.Collections.ArrayList]::new()
            foreach ($result in $results.Results) {
                $messages = $results.Messages | Where-Object Token -EQ $result.Token
                $resultHash = [ordered]@{
                    PSTypeName  = 'PSAzureMigrationAdvisor.ScanResult'
                    Path        = $result.Path
                    Command     = $result.Token -replace '^Command -> '
                    CommandLine = $result.Token.Ast.Extent.StartLineNumber
                    Before      = $result.Change.Before
                    After       = $result.Change.After
                    Message     = $messages.Text -join "`n"
                    MessageType = $messages.Type -join "`n"
                    FileHash    = $fileHash
                }
                if ($ExpandDevOps) { $resultHash += $devOpsHash }
                [PSCustomObject]$resultHash
                foreach ($message in $messages) {
                    $null = $messagesUsed.Add($message)
                }
            }
            foreach ($message in $results.Messages) {
                # If messages were found that are not associated to a resultant token
                if ($message -in $messagesUsed) { continue }

                $resultHash = [ordered]@{
                    PSTypeName  = 'PSAzureMigrationAdvisor.ScanResult'
                    Path        = $results.Path
                    Command     = $message.Data.Name
                    CommandLine = $message.Token.Ast.Extent.StartLineNumber
                    Before      = ''
                    After       = ''
                    Message     = $message.Text
                    MessageType = $message.Type
                    FileHash    = $fileHash
                }
                if ($ExpandDevOps) { $resultHash += $devOpsHash }
                [PSCustomObject]$resultHash
            }
        }
        #endregion Functions

        Clear-ReTokenTransformationSet
        if ($TransformPath) {
            Import-ReTokenTransformationSet -Path $TransformPath
        }
        else {
            if ($Type -contains 'AzureAD') { Import-ReTokenTransformationSet -Path "$script:ModuleRoot\data\azureAD-to-graph.psd1" }
            if ($Type -contains 'MSOnline') { Import-ReTokenTransformationSet -Path "$script:ModuleRoot\data\msol-to-graph.psd1" }
        }
        
        $commandNames = (Get-ReTokenTransformationSet).Name
    }
    process {
        #region Process by Path
        foreach ($pathName in $Path) {
            try { $paths = Resolve-PSFPath -Path $pathName -Provider FileSystem }
            catch {
                Stop-PSFFunction -String 'Read-AzScriptFile.Path.ResolveError' -StringValues $pathName -EnableException $EnableException -ErrorRecord $_ -Tag path -Target $pathName -Continue -Cmdlet $PSCmdlet
            }
            
            #region Process individual files
            foreach ($filePath in $paths) {
                if ($filePath -notmatch '\.ps1$|\.psm1|.psf1') {
                    Write-PSFMessage -Level Warning -String 'Read-AzScriptFile.Path.NoScript' -StringValues $filePath -Target $filePath
                    continue
                }

                $scriptFile = Get-ReScriptFile -Path $filePath
                Read-ScriptFile -ScriptFile $scriptFile -CommandNames $commandNames
            }
            #endregion Process individual files
        }
        #endregion Process by Path

        #region Process by Name & Content
        if ($Name -and $Content) {
            $scriptFile = Get-ReScriptFile -Name $Name -Content $Content
            Read-ScriptFile -ScriptFile $scriptFile -CommandNames $commandNames -ExpandDevOps:$ExpandDevOps
        }
        #endregion Process by Name & Content
    }
    end {
        Clear-ReTokenTransformationSet
    }
}

<#
This is an example configuration file
 
By default, it is enough to have a single one of them,
however if you have enough configuration settings to justify having multiple copies of it,
feel totally free to split them into multiple files.
#>


<#
# Example Configuration
Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Example.Setting' -Value 10 -Initialize -Validation 'integer' -Handler { } -Description "Example configuration setting. Your module can then use the setting using 'Get-PSFConfigValue'"
#>


Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Import.DoDotSource' -Value $false -Initialize -Validation 'bool' -Description "Whether the module files should be dotsourced on import. By default, the files of this module are read as string value and invoked, which is faster but worse on debugging."
Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Import.IndividualFiles' -Value $false -Initialize -Validation 'bool' -Description "Whether the module files should be imported individually. During the module build, all module code is compiled into few files, which are imported instead by default. Loading the compiled versions is faster, using the individual files is easier for debugging and testing out adjustments."

<#
Stored scriptblocks are available in [PsfValidateScript()] attributes.
This makes it easier to centrally provide the same scriptblock multiple times,
without having to maintain it in separate locations.
 
It also prevents lengthy validation scriptblocks from making your parameter block
hard to read.
 
Set-PSFScriptblock -Name 'PSAzureMigrationAdvisor.ScriptBlockName' -Scriptblock {
     
}
#>


<#
# Example:
Register-PSFTeppScriptblock -Name "PSAzureMigrationAdvisor.alcohol" -ScriptBlock { 'Beer','Mead','Whiskey','Wine','Vodka','Rum (3y)', 'Rum (5y)', 'Rum (7y)' }
#>


<#
# Example:
Register-PSFTeppArgumentCompleter -Command Get-Alcohol -Parameter Type -Name PSAzureMigrationAdvisor.alcohol
#>


New-PSFLicense -Product 'PSAzureMigrationAdvisor' -Manufacturer 'Friedrich Weinmann' -ProductVersion $script:ModuleVersion -ProductType Module -Name MIT -Version "1.0.0.0" -Date (Get-Date "2022-03-11") -Text @"
Copyright (c) 2022 Friedrich Weinmann
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"@

#endregion Load compiled code