Skip to content

Instantly share code, notes, and snippets.

@KirkMunro
Created November 2, 2018 19:40
Show Gist options
  • Save KirkMunro/d280e7351dd376806ff89635e7e160f5 to your computer and use it in GitHub Desktop.
Save KirkMunro/d280e7351dd376806ff89635e7e160f5 to your computer and use it in GitHub Desktop.
Find the open source code for a cmdlet that ships as part of PowerShell Core
function Find-PSCmdletSource {
[CmdletBinding()]
[OutputType([string])]
[Alias('oss')]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Alias('Name')]
[string[]]
$CmdletName,
[ValidateNotNullOrEmpty()]
[Alias('Token')]
[string]
$GitHubAuthToken,
[switch]
$OpenInBrowser = $false
)
begin {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$baseUri = 'https://api.github.com/search/code'
}
process {
$cmdlet = if ($_ -is [System.Management.Automation.CmdletInfo]) {$_} else {Get-Command -Name $CmdletName -CommandType Cmdlet}
$cmdletClassName = $cmdlet.ImplementingType.Name
if ($results = Invoke-RestMethod -Method Get -Uri "${baseUri}?q=`"class+${cmdletClassName}`"+in:file+repo:powershell/powershell") {
$url = $results.items | Select-Object -ExpandProperty html_url
if ($OpenInBrowser) {
Start-Process -FilePath $url
} else {
$url
}
}
}
}
gcm Tee-Object | oss -OpenInBrowser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment