-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMicrosoft.Graph.Migration.Tool.psm1
More file actions
217 lines (194 loc) · 9.2 KB
/
Copy pathMicrosoft.Graph.Migration.Tool.psm1
File metadata and controls
217 lines (194 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
$BetaProfileRegex = "^(Select-MgProfile)\s+(?:-Name)?(\s+)?('beta'|""beta""|beta)"
$V1ProfileRegex = "^(Select-MgProfile)\s+(?:-Name)?(\s+)?('v1\.0'|v1\.0|""v1\.0"")"
$ConnectMgGraphRegex = "(?i)^(connect-mggraph|#connect-mggraph)"
$DisconnectMgGraphRegex = "(?i)^(disconnect-mggraph|#disconnect-mggraph)"
function Read-MgScriptDirectory {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[string] $DirectoryPath,
[string] $GraphProfile,
[string] $UpdatedFilePath
)
process {
$SupportedFileTypes = ".ps1", ".psm1"
Get-ChildItem $DirectoryPath -Recurse -File | ForEach-Object {
if ($_.Extension -in $SupportedFileTypes) {
Invoke-MgScriptAnalyzer -FilePath $_.FullName -GraphProfile $GraphProfile -UpdatedFilePath $UpdatedFilePath
}
else {
Write-Warning "Skipping unsupported file type: $($_.FullName)"
}
}
}
}
function Invoke-MgScriptAnalyzer {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[string] $FilePath,
[string] $GraphProfile,
[string] $UpdatedFilePath
)
# TODO: Analyze script content for breaking change.
# TODO: Store changes in an object.
# TODO: Write object as output.
# TODO: Settle on object members.
# Proposed Output
# Order | Location (file name:line:tab) | Type (Cmdlet|CmdletParameter|ModuleName) | ApiVersion | Original | New
[System.Collections.ArrayList]$LineContent = @()
[System.Collections.ArrayList]$LineNumbers = @()
$LineUpdate = @{}
[Hashtable]$Original = @{}
[Hashtable]$ProposedChanges = @{}
$Result = @()
$SupportedFileTypes = ".ps1", ".psm1"
if ($SupportedFileTypes -notContains [System.IO.Path]::GetExtension($FilePath)) {
throw "Unsupported file type: $FilePath"
}
$WriteToFile = $false
$FileName = [System.IO.Path]::GetFileName($FilePath)
$UpdatedFile = ""
if (-not ([string]::IsNullOrEmpty($UpdatedFilePath))) {
$WriteToFile = $true
$UpdatedFile = Join-Path $UpdatedFilePath "Migration-$FileName"
if (-not (Test-Path $UpdatedFilePath)) { throw "Path does not exist: $UpdatedFilePath" }
}
$ScriptContent = Get-Content $FilePath
$i = 0
foreach ($_ in $ScriptContent) {
$_ = $_.ToString().TrimStart()
$indexOfItem = $LineContent.Add($_)
if ($_ -match $BetaProfileRegex) {
if(-not ($_.StartsWith("#"))){
$indexOfLineItem = $LineNumbers.Add($i)
}
}
$i++
}
if ($GraphProfile -ieq "beta") {
for ($g = 0; $g -lt $LineContent.Count; $g++) {
if ($LineContent[$g].Contains("-Mg")) {
if( $LineContent[$g] -notmatch $ConnectMgGraphRegex -and $LineContent[$g] -notmatch $DisconnectMgGraphRegex){
$Original.Add($g + 1, $LineContent[$g])
$ProposedChanges.Add($g + 1, $LineContent[$g].ToString().Replace("-Mg", "-MgBeta"))
}
}
}
}
else {
$Lines = $LineNumbers.Count
for ($j = 0; $j -lt $Lines; $j++) {
for ($k = $LineNumbers[$j]; $k -lt $LineContent.Count; $k++) {
if ($LineContent[$k] -match $V1ProfileRegex) {
if (-not $LineUpdate.ContainsKey($LineNumbers[$j])) {
$LineUpdate.Add($LineNumbers[$j], $k)
}
}
}
}
$LineUpdate.Keys | ForEach-Object {
$LineNumber = $_
for ($m = $lineNumber + 1; $m -lt $LineUpdate[$LineNumber]; $m++) {
if($LineContent[$m] -notmatch $ConnectMgGraphRegex -and $LineContent[$m] -notmatch $DisconnectMgGraphRegex){
if ($LineContent[$m].Contains("-Mg")) {
$Original.Add($m + 1, $LineContent[$m])
$ProposedChanges.Add($m + 1, $LineContent[$m].ToString().Replace("-Mg", "-MgBeta"))
}
}
}
}
if ($Lines -gt $LineUpdate.Count) {
for ($n = $LineNumbers[$Lines - 1] + 1; $n -lt $LineContent.Count; $n++) {
if( $LineContent[$n] -notmatch $ConnectMgGraphRegex -and $LineContent[$n] -notmatch $DisconnectMgGraphRegex){
if ($LineContent[$n].Contains("-Mg")) {
$Original.Add($n + 1, $LineContent[$n])
$ProposedChanges.Add($n + 1, $LineContent[$n].ToString().Replace("-Mg", "-MgBeta"))
}
}
}
}
}
$p = 1
foreach ($item in $Original.GetEnumerator() | Sort Name) {
$LineNumber = $item.Key
$OriginalValue = $Original[$LineNumber].ToString()
$ProposedValue = $ProposedChanges[$LineNumber].ToString()
$Result += [pscustomobject]@{"Order" = $p;"Location" = $FileName+":"+$LineNumber; "Type"="Cmdlet"; "ApiVersion" = "Beta"; "Original" = $OriginalValue; "New" = $ProposedValue }
if ($WriteToFile) {
foreach ($Content in $ScriptContent) {
if ($Content -eq $OriginalValue) {
$ScriptContent = $ScriptContent | ForEach-Object { $_ -replace [regex]::Escape($OriginalValue), $ProposedValue }
}
if ($Content.StartsWith("Select-MgProfile")) {
$ScriptContent = $ScriptContent -replace $Content, $null
}
}
$ScriptContent > $UpdatedFile
}
$p++
}
if ($Result.Count -gt 0) {
Write-Host -ForegroundColor Green "--------- Your script(s) contains commands that need to conform to the naming convention as per the 'New' column on the table below ---------"
Write-Output $Result | Format-Table
}
if($WriteToFile){
Write-Host -ForegroundColor Blue "Your updated script is on this path: $UpdatedFile"
}
$Result = @()
}
function New-MgMigrationPlan {
[CmdletBinding(DefaultParameterSetName = 'FileOrDirectory')]
param (
[Parameter(ParameterSetName = 'Directory', Mandatory = $true)]
[ValidateNotNullorEmpty()]
[string] $DirectoryPath,
[Parameter(ParameterSetName = 'File', Mandatory = $true)]
[ValidateNotNullorEmpty()]
[string] $FilePath,
[Parameter(ParameterSetName = 'FileOrDirectory', Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[PSCustomObject] $InputObject,
[string] $GraphProfile,
[string] $UpdatedFilePath
)
process {
switch ($PSCmdlet.ParameterSetName) {
'File' {
if (-not (Test-Path $FilePath)) { throw "Path does not exist: $FilePath" }
Invoke-MgScriptAnalyzer -FilePath $FilePath -GraphProfile $GraphProfile -UpdatedFilePath $UpdatedFilePath
}
'Directory' {
if (-not (Test-Path $DirectoryPath)) { throw "Path does not exist: $DirectoryPath" }
Read-MgScriptDirectory -DirectoryPath $DirectoryPath -GraphProfile $GraphProfile -UpdatedFilePath $UpdatedFilePath
}
'FileOrDirectory' {
foreach ($input in $InputObject) {
if (-not (Test-Path $input)) { throw "Path does not exist: $input" }
if ([System.IO.Path]::HasExtension($input)) {
# Process as file.
Invoke-MgScriptAnalyzer -FilePath $input -GraphProfile $GraphProfile -UpdatedFilePath $UpdatedFilePath
}
else {
# Process as directory.
Read-MgScriptDirectory -DirectoryPath $input -GraphProfile $GraphProfile -UpdatedFilePath $UpdatedFilePath
}
}
}
}
}
}
# TODO: Remove test when done.
#New-MgMigrationPlan -FilePath "C:\Dev\M\msgraph-sdk-powershell\samples\6-Sites.ps1"
#New-MgMigrationPlan -FilePath "C:\Projects\msgraph-sdk-powershell\samples\6-Sites.ps1" -GraphProfile Beta
#New-MgMigrationPlan -FilePath "C:\Projects\msgraph-sdk-powershell\samples\5-Teams.ps1"
#New-MgMigrationPlan -FilePath"C:\PlayGround\M365Groupreport.ps1" -UpdatedFilePath C:\PlayGround
# Test cases:
# 1. Cmdlet with no changes.
# 2. Cmdlet with changes.
# 3. Cmdlet with changes and deprecated parameter.
# 4. Cmdlet with changes and deprecated parameter and new parameter.
# 5. Cmdlet with changes and deprecated parameter and new parameter and new cmdlet.
# 6. Cmdlet with changes and deprecated parameter and new parameter and new cmdlet and new module.
# 7. Empty script file.
# 8. Script file without Microsoft Graph cmdlets.
# 9. Script file with v2.x cmdlets.