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 Import-MappingFile { <# .SYNOPSIS Imports a mapping file for a specified resource type. .DESCRIPTION The Import-MappingFile cmdlet imports a mapping file for a specified resource type, which contains a set of ReToken transformations that can be used to migrate Azure Active Directory (Azure AD) PowerShell scripts to Microsoft Graph PowerShell scripts. The cmdlet searches for the mapping file in the configuration directory specified by the PSAzureMigrationAdvisor.Path.Config configuration value. If the mapping file is not found, the cmdlet searches for it in the module data directory. .PARAMETER Type Specifies an array of resource types to import. .EXAMPLE PS C:> Import-MappingFile -Type AzureADPreview, MSOnline Imports the mapping files for the 'AzureADPreview' and 'MSOnline' resource types. #> [CmdletBinding()] param ( [string[]] $Type ) $configRoot = Get-PSFConfigValue -FullName 'PSAzureMigrationAdvisor.Path.Config' $moduleDataRoot = Join-Path -Path $script:ModuleRoot -ChildPath 'data' # Update the Token Transformation Sets foreach ($typeName in $Type) { Write-PSFMessage -Message "Loading definitions for $typeName" if (Test-Path -Path (Join-Path -Path $configRoot -ChildPath "definition-$typeName.json")) { Import-ReTokenTransformationSet -Path (Join-Path -Path $configRoot -ChildPath "definition-$typeName.json") } else { Import-ReTokenTransformationSet -Path (Join-Path -Path $moduleDataRoot -ChildPath "definition-$typeName.json") } } # Update the global command dataset if (Test-Path -Path (Join-Path -Path $configRoot -ChildPath "raw-data.clidat")) { $data = Import-PSFCLixml -Path (Join-Path -Path $configRoot -ChildPath "raw-data.clidat") } else { $data = Import-PSFCLixml -Path (Join-Path -Path $moduleDataRoot -ChildPath "raw-data.clidat") } $commands = @{} foreach ($command in $data) { $commands[$command.Name] = $command if (-not $command.NewCommand) { continue } foreach ($newCommand in $command.NewCommand) { $commands[$newCommand] = $command } } $script:migrationCommandMapping = $commands } 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 at the same time. .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 OutPath Folder to which to write the converted scriptfile. .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, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'path')] [PsfValidateScript('PSFramework.Validate.FSPath.Folder', ErrorString = 'PSFramework.Validate.FSPath.Folder')] [string] $OutPath, [switch] $EnableException ) begin { Write-PSFMessage -String 'Convert-AzScriptFile.Danger.Warning' -Once Danger -Level Warning Clear-ReTokenTransformationSet if ($TransformPath) { Import-ReTokenTransformationSet -Path $TransformPath } else { Import-MappingFile -Type $Type } $commandNames = (Get-ReTokenTransformationSet).Name $lastResolvedPath = "" } process { if ($OutPath -ne $lastResolvedPath) { $resolvedOutPath = Resolve-PSFPath -Path $OutPath $lastResolvedPath = $OutPath } 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 try { $results = $scriptFile.Transform($relevantTokens) } catch { Stop-PSFFunction -String 'Convert-AzScriptFile.Converting.Error' -StringValues $filePath -ErrorRecord $_ -EnableException $EnableException -Continue -Target $filePath } if ($OutPath) { Invoke-PSFProtectedCommand -ActionString 'Convert-AzScriptFile.ConvertingWriting' -ActionStringValues $results.Count, $filePath, $resolvedOutPath -ScriptBlock { $scriptfile.WriteTo($resolvedOutPath, "") } -Target $filePath -EnableException $EnableException -PSCmdlet $PSCmdlet -Continue } else { 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 Export-AzScriptReport { <# .SYNOPSIS Exports the results of Read-AzScriptFile to csv or xlsx. .DESCRIPTION Exports the results of Read-AzScriptFile to csv or xlsx. It reformats some of the data and eliminates linebreaks in the "Before" column. In order to generate xlsx files, the additional module "ImportExcel" is required. .PARAMETER Path The path where to write the report to. May be a relative path, must include the filename. The parent folder must already exist. .PARAMETER Delimiter Which delimiter to use when generating a CSV file. Defaults to the current culture, but may be overridden as needed. This affects whether you can open the file in Excel by simple doubleclick or whether you need to first import the data. .PARAMETER ScriptPath The path where to write converted scripts. Specifying this parameter will save the scanned files to disk, with commands renamed to their new name. THESE SCRIPTS WILL NOT WORK!! This is a starting aid to help migrating, but commands do not always work the same way, may need different parameters or even map to two different vommands, depending on use. .PARAMETER InputObject The report objects from Read-AzScriptFile to export. .EXAMPLE PS C:\> Get-ChildItem C:\scripts -Recurse -Filter *.ps1 | Read-AzScriptFile | Export-AzScriptReport -Path .\report.csv Will search all PowerShell code under C:\scripts, search it for AzureAD and MSOnline commands and then export the finidngs to .\report.csv. #> [CmdletBinding()] param ( [Parameter(Position = 0, Mandatory = $true)] [PsfValidateScript('PSFramework.Validate.FSPath.FileOrParent', ErrorString = 'PSFramework.Validate.FSPath.FileOrParent')] [string] $Path, [string] $Delimiter = (Get-PSFConfigValue -FullName 'PSAzureMigrationAdvisor.Export.CsvDelimiter'), [PsfValidateScript('PSFramework.Validate.FSPath.Folder', ErrorString = 'PSFramework.Validate.FSPath.Folder')] [string] $ScriptPath, [Parameter(ValueFromPipeline = $true)] $InputObject ) begin { #region Functions function Write-ScriptFile { [CmdletBinding()] param ( [string] $ScriptPath, $Result, $Item, $Delimiter ) # Already Saved $doSave = $true if ($Item._ScriptFile.Path -like "$ScriptPath*") { $doSave = $false } $newFileName = '{0}-{1}.{2}' -f ((Split-Path $datum.Path -Leaf) -replace '\.[^\.]+'), $Result.FileHash, ((Split-Path $datum.Path -Leaf) -replace '^.+\.') -replace '\[[^\[\]]+\]$' $newFilePath = Join-PSFPath $ScriptPath $newFileName if ($Result.Branch) { $newFilePath = Join-PSFPath $ScriptPath $Result.Organization $Result.Project $Result.Repository $Result.Branch $newFileName } $parentPath = Split-Path -Path $newFilePath if (-not (Test-Path -Path $parentPath)) { $null = New-Item -Path $parentPath -ItemType Directory -Force -ErrorAction Stop } $reportPath = Join-Path -Path $parentPath -ChildPath 'findings.csv' $Result | Export-Csv -Path $reportPath -NoTypeInformation -Append -Delimiter $Delimiter if (-not $doSave) { return } $Item._ScriptFile.Path = $newFilePath $Item._ScriptFile.Save() } #endregion Functions if ($ScriptPath) { $ScriptPath = Resolve-PSFPath -Path $ScriptPath } $useExcel = $Path -like "*.xlsx" if ($useExcel -and -not (Get-Command Export-Excel -ErrorAction Ignore)) { Stop-PSFFunction -String 'Export-AzScriptReport.NoExcel' -EnableException $true -Cmdlet $PSCmdlet } if ($useExcel) { $steppable = { Export-Excel -Path $Path }.GetSteppablePipeline() } else { $param = @{ Path = $Path } if ($Delimiter) { $param.Delimiter = $Delimiter } else { $param.UseCulture = $true } $steppable = { Export-Csv @param }.GetSteppablePipeline() } try { $steppable.Begin($true) } catch { Stop-PSFFunction -String 'Export-AzScriptReport.Export.Failed' -StringValues $Path -ErrorRecord $_ -EnableException $true -Cmdlet $PSCmdlet } # Ensure mappings for graph commands exist if (0 -eq $script:migrationCommandMapping.Count) { Import-MappingFile } } process { foreach ($datum in $InputObject) { #region Process Messages # Note: This approach breaks down for messages that had multiple lines $msgInfo = @() $msgWarning = @() $msgError = @() $msgTypes = $datum.MessageType -split "`n" $messages = $datum.Message -split "`n" if ($msgTypes) { foreach ($index in 0..$msgTypes.Count) { switch (($msgTypes)[$index]) { 'Info' { $msgInfo += $messages[$index] } 'Information' { $msgInfo += $messages[$index] } 'Warning' { $msgWarning += $messages[$index] } 'Error' { $msgError += $messages[$index] } } } } #endregion Process Messages $mapped = $script:migrationCommandMapping.$($datum.Command) $result = [PSCustomObject][ordered]@{ Path = $datum.Path Command = $datum.Command CommandLine = $datum.CommandLine Before = $datum.Before -replace "``[`r`n]+" -replace '\s+', ' ' # Eliminate backticks and linebreaks After = $datum.After GraphCommand = $mapped.NewCommand GraphModule = $mapped.NewCommandModule MsgInfo = $msgInfo -join " " MsgWarning = $msgWarning -join " " MsgError = $msgError -join " " FileHash = $datum.Filehash MessageType = $datum.MessageType # Keep in the full dataset, just in case somebody includes multiline messages Messages = $datum.Message ScopesDelegate = $mapped.ScopesDelegate -join ',' ScopesApplication = $mapped.ScopesApplication -join ',' Examples = $mapped.LinkExamples -join ', ' OnlineMapping = 'https://github.com/microsoft/AzureAD-to-MSGraph/blob/main/docs/{0}/{1}.md' -f $mapped.Module, $mapped.Name Organization = $datum.Organization Project = $datum.Project Repository = $datum.Repository Branch = $datum.Branch } $steppable.Process($result) if ($ScriptPath) { try { Write-ScriptFile -ScriptPath $ScriptPath -Result $result -Item $datum -Delimiter $Delimiter } catch { Write-PSFMessage -Level Error -String 'Export-AzScriptReport.ExportScript.Failed' -StringValues $result.Path -Target $result -ErrorRecord $_ -PSCmdlet $PSCmdlet } } } } end { $steppable.End() } } 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 AzureADPreview, AzureAD or Msonline, defaults to scanning AzureADPreview & MSOnline 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')] [AllowEmptyString()] [string] $Content, [ValidateSet('AzureADPreview', 'AzureAD', 'MSOnline')] [string[]] $Type = @('AzureADPreview', '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 _ScriptFile = $ScriptFile } 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 _ScriptFile = $ScriptFile } if ($ExpandDevOps) { $resultHash += $devOpsHash } [PSCustomObject]$resultHash } } #endregion Functions Clear-ReTokenTransformationSet if ($TransformPath) { Import-ReTokenTransformationSet -Path $TransformPath } else { Import-MappingFile -Type $Type } $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 } } function Update-AzADMapping { <# .SYNOPSIS Updates Azure AD to Microsoft Graph API command mapping data. .DESCRIPTION The Update-AzADMapping function retrieves the command mapping data from GitHub and saves it in a local directory. If the local data is up-to-date, the function exits without performing any action. .PARAMETER ConfigRoot Specifies the root directory where the mapping data is stored. If this parameter is not provided, the value of the PSAzureMigrationAdvisor.Path.Config configuration setting is used instead. .PARAMETER Force Perform the update, no matter whether the local definitions are already up-to-date or not. .EXAMPLE PS C:\> Update-AzADMapping -ConfigRoot "C:\AzureADMapping" This command updates the Azure AD to Microsoft Graph API command mapping data stored in the C:\AzureADMapping directory. If the directory does not exist, the function creates it. .LINK https://github.com/microsoft/AzureAD-to-MSGraph #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] [CmdletBinding()] param ( [string] $ConfigRoot = (Get-PSFConfigValue -FullName 'PSAzureMigrationAdvisor.Path.Config'), [switch] $Force ) if (-not (Test-Path -Path $ConfigRoot)) { $null = New-Item -Path $ConfigRoot -ItemType Directory -Force } $timestampUrl = 'https://github.com/microsoft/AzureAD-to-MSGraph/raw/main/export/timestamp.clidat' $definitionUrl = 'https://github.com/microsoft/AzureAD-to-MSGraph/raw/main/export/all.clidat' $localTimestampPath = Join-Path -Path $ConfigRoot -ChildPath timestamp.clidat try { $timestampOnline = (Invoke-WebRequest -Uri $timestampUrl).Content | ConvertFrom-PSFClixml } catch { Stop-PSFFunction -String 'Update-AzADMapping.Internet.Error' -ErrorRecord $_ -Cmdlet $PSCmdlet -EnableException $true } $timestampLocal = Get-Date -Date 1970-01-01 if (Test-Path -Path "$script:ModuleRoot\data\timestamp.clidat") { $timestampLocal = Import-PSFClixml -Path "$script:ModuleRoot\data\timestamp.clidat" } if (Test-Path -Path $localTimestampPath) { $timestampLocal = Import-PSFClixml -Path $localTimestampPath } if (-not $Force -and $timestampLocal -ge $timestampOnline) { return } $dataEntries = (Invoke-WebRequest -Uri $definitionUrl).Content | ConvertFrom-PSFClixml $dataEntries | Export-PSFClixml -Path (Join-Path -Path $ConfigRoot -ChildPath 'raw-data.clidat') foreach ($moduleGroup in $dataEntries | Group-Object Module) { $result = @{ Version = 1 Type = 'Command' Content = @{ } } foreach ($commandDefinition in $moduleGroup.Group) { $data = @{ Name = $commandDefinition.Name NewName = $commandDefinition.NewCommand } $paramRemap = @{} $paramInfo = @{} $paramWarn = @{} $paramError = @{} foreach ($param in $commandDefinition.Parameters.Values) { if ($param.NewName -and $param.Name -ne $param.NewName) { $paramRemap[$param.Name] = $param.NewName } if ($param.MsgInfo) { $paramInfo[$param.Name] = $param.MsgInfo } if ($param.MsgWarning) { $paramWarn[$param.Name] = $param.MsgWarning } if ($param.MsgError) { $paramError[$param.Name] = $param.MsgError } } if (0 -lt $paramRemap.Count) { $data.Parameters = $paramRemap } if (0 -lt $paramInfo.Count) { $data.InfoParameters = $paramInfo } if (0 -lt $paramWarn.Count) { $data.WarningParameters = $paramWarn } if (0 -lt $paramError.Count) { $data.ErrorParameters = $paramError } $result.Content[$data.Name] = $data } $moduleExportPath = Join-Path -Path $ConfigRoot -ChildPath "definition-$($moduleGroup.Name).json" $result | ConvertTo-Json -Depth 5 -Compress | Set-Content -Path $moduleExportPath } $timestampOnline | Export-PSFClixml -Path $localTimestampPath } <# 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." Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Export.CsvDelimiter' -Value ',' -Initialize -Validation 'string' -Description 'The delimiter to use with "Export-AzScriptReport" when generating CSV files. By default, the current culture will be used.' Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Path.Config' -Value (Join-Path -Path (Get-PSFPath -Name AppData) -ChildPath 'PowerShell/PSAzureMigrationAdvisor') -Initialize -Validation string -Description 'Path where the Azure Migration Advisor looks for its bulk configuration data, such as the mapping data from AzureAD or MSOnline to MSGraph.' Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Path.ModuleRoot' -Value $script:ModuleRoot -Initialize -Validation string -Description 'Path where the module can be found. Used to find the module for automatic background data refreshes.' Set-PSFConfig -Module 'PSAzureMigrationAdvisor' -Name 'Definitions.AutoUpate.Enabled' -Value $true -Initialize -Validation bool -Description 'Whether the module automatically performs update checks for new definitions mapping AzureAD & MSOnline commands to Graph.' <# 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 #> # Mapping data used to add additional metadata to the script report export $script:migrationCommandMapping = @{ } if (Get-PSFConfigValue -FullName 'PSAzureMigrationAdvisor.Definitions.AutoUpate.Enabled') { Register-PSFTaskEngineTask -Name PSAzureMigrationAdvisor.UpdateCheck -ScriptBlock { $moduleRoot = Get-PSFConfigValue -FullName 'PSAzureMigrationAdvisor.Path.ModuleRoot' Import-Module (Join-Path -Path $moduleRoot -ChildPath 'PSAzureMigrationAdvisor.psd1') -Scope Global Update-AzADMapping } -Description 'Looks for the latest definition set of AzureAD/MSOnline to Graph mappings' -Once } 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 |