@@ -313,7 +313,7 @@ function Update-ExampleFile {
313313 Clear-Content $ExampleFile - Force
314314 for ($d = 0 ; $d -lt $HeaderList.Count ; $d ++ ) {
315315 $CodeValue = $ExampleList [$d ].Trim()
316- if ($CodeValue.Contains ( $CommandPattern ) ){
316+ if ($CodeValue - match " \b $CommandPattern \b " ){
317317 $TitleValue = " ### " + $HeaderList [$d ].Trim()
318318 $Code = " `````` powershell`r $CodeValue `r`n`````` "
319319 $TotalText = " $TitleValue `r`n`n $Code `r`n $Description `r`n "
@@ -329,11 +329,11 @@ function Update-ExampleFile {
329329 # The code below updates existing examples
330330 # ------------------------------------------------------------#
331331 $PatternToSearch = " Import-Module Microsoft.Graph.$Module "
332+ if ($GraphProfile -eq " beta" ){
333+ $PatternToSearch = " Import-Module Microsoft.Graph.Beta.$Module "
334+ }
332335 if (($Content | Select-String - pattern $SearchText ) -and ($Content | Select-String - pattern " This example shows" )){
333336 $ContainsPatternToSearch = $False
334- if ($GraphProfile -eq " beta" ){
335- $PatternToSearch = " Import-Module Microsoft.Graph.Beta.$Module "
336- }
337337 foreach ($List in $ExampleList ){
338338 if ($List.Contains ($PatternToSearch ) -and $List.Contains ($CommandPattern )){
339339 $ContainsPatternToSearch = $True
@@ -343,7 +343,7 @@ function Update-ExampleFile {
343343 Clear-Content $ExampleFile - Force
344344 for ($d = 0 ; $d -lt $HeaderList.Count ; $d ++ ) {
345345 # We should only add the correct examples from external docs link
346- if ($ExampleList [$d ].Contains( $CommandPattern ) ){
346+ if ($ExampleList [$d ] -match " \b $CommandPattern \b " ){
347347 $CodeValue = $ExampleList [$d ].Trim()
348348 $TitleValue = " ### " + $HeaderList [$d ].Trim()
349349 $Code = " `````` powershell`r $CodeValue `r`n`````` "
@@ -358,15 +358,22 @@ function Update-ExampleFile {
358358
359359
360360 }else {
361- if (-not ($Content | Select-String - pattern $CommandPattern )){
362361 Clear-Content $ExampleFile - Force
362+ if ($Content | Select-String - pattern $CommandPattern ){
363+ Retain- ExistingCorrectExamples - Content $Content - File $ExampleFile
364+ }else {
363365 # Replace everything with boiler plate code
364- $DefaultBoilerPlate = " ### Example 1: {{ Add title here }}`r`n`````` powershell`r`n PS C:\> {{ Add code here }}`r`n`n {{ Add output here }}`r`n```````n`n {{ Add description here }}`r`n`n ### Example 2: {{ Add title here }}`r`n`````` powershell`r`n PS C:\> {{ Add code here }}`r`n`n {{ Add output here }}`r`n```````n`n {{ Add description here }}`r`n`n "
365- Add-Content - Path $ExampleFile - Value $DefaultBoilerPlate.Trim ()
366+ $DefaultBoilerPlate = " ### Example 1: {{ Add title here }}`r`n`````` powershell`r`n PS C:\> {{ Add code here }}`r`n`n {{ Add output here }}`r`n```````n`n {{ Add description here }}`r`n`n ### Example 2: {{ Add title here }}`r`n`````` powershell`r`n PS C:\> {{ Add code here }}`r`n`n {{ Add output here }}`r`n```````n`n {{ Add description here }}`r`n`n "
367+ Add-Content - Path $ExampleFile - Value $DefaultBoilerPlate.Trim ()
366368 }
367369 }
368370
369371 }
372+ $CheckIfFileEmpty = Test-FileEmpty $ExampleFile
373+ if ($CheckIfFileEmpty ){
374+ Retain- ExistingCorrectExamples - Content $Content - File $ExampleFile
375+ }
376+ # ----------------------------------------------------------------------------------------------#
370377 # The code below corrects the numbering of the example headers/title if there is a situation where
371378 # some examples are wrong(which are left out) and some are right
372379 # -----------------------------------------------------------------------------------------------#
@@ -419,6 +426,36 @@ function Update-ExampleFile {
419426 }
420427 }
421428
429+ }
430+ function Test-FileEmpty {
431+
432+ Param ([Parameter (Mandatory = $true )][string ]$File )
433+
434+ if ((Test-Path - LiteralPath $file ) -and ! ((Get-Content - LiteralPath $file - Raw) -match ' \S' )) {return $true } else {return $false }
435+
436+ }
437+ function Retain-ExistingCorrectExamples {
438+
439+ Param ([Parameter (Mandatory = $true )][object ]$Content , [Parameter (Mandatory = $true )][string ]$File )
440+
441+ $CorrectMaintainableHeaderCount = 1 ;
442+ $LineCounter = 0 ;
443+
444+ foreach ($Line in $Content ){
445+
446+ if ($Line.StartsWith (" $CommandPattern " )){
447+ $LineCounter ++
448+ $Header = $Content [$LineCounter - 1 ]
449+ if (($Null -eq $Header ) -or ($Header -eq " " )){
450+ $Header = " ### Example " + $CorrectMaintainableHeaderCount + " : Code snippet"
451+ }
452+ $MaintainedCorrectExample = " $Header `r`n`n`````` powershell`r`n $PatternToSearch `r`n`n $Line `r`n```````r`n $Description `r`n "
453+ $CorrectMaintainableHeaderCount ++
454+ Add-Content - Path $File - Value $MaintainedCorrectExample
455+ }
456+
457+ }
458+
422459}
423460$JsonContent = Get-Content - Path $MetaDataJsonFile
424461$DeserializedContent = $JsonContent | ConvertFrom-Json
@@ -480,5 +517,3 @@ Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
480517
481518# 4. Test for beta updates from api reference
482519# Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/serviceprincipal-post-approleassignedto?view=graph-rest-beta" -GraphCommand "New-MgBetaServicePrincipalAppRoleAssignedTo" -GraphModule "Applications" -Profile "beta"
483- # Write-Host -ForegroundColor Green "-------------Done-------------"
484- # Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/accessreviewinstancedecisionitem-get?view=graph-rest-1.0" -GraphCommand "Get-MgBetaIdentityGovernanceAccessReviewDefinitionInstanceDecision" -GraphModule "Identity.Governance" -Profile "beta"
0 commit comments