Created
November 2, 2018 19:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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