Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active December 31, 2021 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsmelton/5f1216307efdaf40fbc5ebe49d5f65c4 to your computer and use it in GitHub Desktop.
Save wsmelton/5f1216307efdaf40fbc5ebe49d5f65c4 to your computer and use it in GitHub Desktop.
Commands index page comparison to Commands in dbatools module (cmdlet and function)
function Find-MissingCommands {
<#
.SYNOPSIS
Find missing commands between the dbatools.io/commands and dbatools Module public functions
.PARAMETER ModulePath
Path to dbatools local repository
.PARAMETER CommandPagePath
Full path to the index.html commands page (e.g. c:\git\web\commands\index.html)
.PARAMETER Reverse
Compare commands found in the CommandPagePath to those in the module
.EXAMPLE
Find-MissingCommands
Returns string list of the commands not found in the Commands page.
#>
[cmdletbinding()]
param(
[string]
$ModulePath = 'C:\git\dbatools',
[string]
$CommandPagePath = 'C:\git\web\commands\index.html',
[switch]
$Reverse
)
$commandPage = Get-Content $CommandPagePath
if (-not (Get-Module dbatools)) {
Import-Module $ModulePath
}
$commands = Get-Command -Module dbatools -CommandType Cmdlet, Function | Where-Object Name -NotIn 'Where-DbaObject','New-DbaTeppCompletionResult' | Select-Object -Expand Name
if ($Reverse) {
$commandRefs = $commandPage | Select-String '<a href="http://docs.dbatools.io/' | ForEach-Object { $_.ToString().Trim() }
$commandRefList = foreach ($ref in $commandRefs) {
$ref.ToString().SubString(0,$ref.ToString().IndexOf('">')).TrimStart('<a href="http://docs.dbatools.io/')
}
$commandRefList | Where-Object { $_ -notin $commands }
} else {
#find missing
$notFound = $commands | ForEach-Object -ThrottleLimit 10 -Parallel { $foundIt = $using:commandPage | Select-String -Pattern $_; if (-not $foundIt) { $_ } }
# found
$found = $commands | ForEach-Object -ThrottleLimit 10 -Parallel { $foundIt = $using:commandPage | Select-String -Pattern $_; if ($foundIt) { $_ } }
Write-Host "Tally: Total Commands ($($commands.Count)) | Found ($($found.Count)) | Missing ($($notFound.Count))"
$notFound
}
}
@Levistator
Copy link

I think this function should work as expected now. I would just recommend verifying that the returns look correct.

 function Find-MissingCommands {
    <#
    .SYNOPSIS
    Find missing commands between the dbatools.io/commands and dbatools Module public functions
    .PARAMETER ModulePath
    Path to dbatools local repository
    .PARAMETER CommandPage
    Full path to the index.html commands page (e.g. c:\git\web\commands\index.html)
    .EXAMPLE
    Find-MissingCommands
    Returns string list of the commands not found in the Commands page.
    #>
    [cmdletbinding()]
    param(
        [string]
        $ModulePath = 'E:\Development\Powershell\dbatools',

        [string]
        $CommandPage = 'E:\Development\Powershell\web\commands\index.html',

        [Switch]
        $Reverse
    )
    # Changed to $CommandPageContent because $CommandPage is statically typed to [String] which breaks the code. 
    $CommandPageContent = Get-Content $CommandPage

    if (-not (Get-Module dbatools)) {
        Import-Module $ModulePath
    }
    $commands = Get-Command -Module dbatools -CommandType Cmdlet, Function | Where-Object Name -NotIn 'Where-DbaObject', 'New-DbaTeppCompletionResult' | Select-Object -Expand Name

    If ($Reverse) {
        # This takes advantage of Select-String returning a MatchInfo object and allows us to extract that information directly using Regex Capture groups.
        # The pattern is as follows.
            # Capture anything that might exist before <a href ....
            # Capture <a href...
            # Capture 0 or more any characters, as few as possible (lazy quantifier) until
            # Capture "> literals
            # Capture 0 or more any characters, as few as possible (lazy quantifier) but record the group, until
            # Capture literal </a>
        $commandRefList = $CommandPageContent | Select-String -Pattern '.*<a href="http://docs.dbatools.io/.*?">(.*?)</a>' | ForEach-Object {$_.Matches[0].Groups[1].Value}
        $commandRefList | Where-Object { $_ -notin $commands }
    }
    else {
        #find missing
        $notFound = $commands | ForEach-Object -ThrottleLimit 10 -Parallel { $foundIt = $using:commandPage | Select-String -Pattern $_; if (-not $foundIt) { $_ } }

        # found
        $found = $commands | ForEach-Object -ThrottleLimit 10 -Parallel { $foundIt = $using:commandPage | Select-String -Pattern $_; if ($foundIt) { $_ } }

        Write-Host "Tally: Total Commands ($($commands.Count)) | Found ($($found.Count)) | Missing ($($notFound.Count))"
        $notFound
    }
}

@wsmelton
Copy link
Author

That change truncates the command names found in the index:

image

I've updated Gist with the current version that returns the expected content:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment