Last active
August 29, 2015 14:22
-
-
Save stknohg/858a21655522a6dd2c15 to your computer and use it in GitHub Desktop.
Get-Commandをちょっとラップしたやつ+whichエイリアス
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
<# | |
.Synopsis | |
コマンドを検索し文字列形式で返します。 | |
.DESCRIPTION | |
コマンドを検索し文字列形式で返します。 | |
Get-Commandコマンドレットのラッパーになります。 | |
返される文字列の形式は "[コマンドの種別] : [名称およびPath]"になります。 | |
.PARAMETER Name | |
検索する名称を指定します。 | |
ワイルドカードが使用できます。 | |
.PARAMETER CommandType | |
検索対象とするコマンドの種類を指定します。 | |
未指定の場合はAll(すべての種類を検索)となります。 | |
.EXAMPLE | |
Get-CommandString notepad | |
.EXAMPLE | |
Get-CommandString get* -CommandType Alias | |
#> | |
function Get-CommandString{ | |
[OutputType([string[]])] | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true, Position=0)] | |
[string]$Name, | |
[Parameter(Mandatory=$false)] | |
[System.Management.Automation.CommandTypes]$CommandType = [System.Management.Automation.CommandTypes]::All | |
) | |
$CurrentPref = $ErrorActionPreference | |
try{ | |
$ErrorActionPreference = "Stop" | |
# Execute Get-Command | |
$Commands = Get-Command -Name $Name -CommandType $CommandType | |
if($Commands.Count -eq 0){ | |
Write-Warning "Command not found." | |
return "" | |
} | |
Write-Host ("{0} command found." -F $Commands.Count) -ForegroundColor Green | |
# Get Strig Values | |
foreach ($cmd in $Commands) | |
{ | |
$Path = "" | |
switch ($cmd.GetType().Name) | |
{ | |
'ApplicatoinInfo' { $Path = $cmd.Definition; break } | |
'AliasInfo' { $Path = $cmd.DisplayName; break } | |
'CmdletInfo' { $Path = $cmd.Name; break } | |
'FunctionInfo' { $Path = $cmd.Name; break } | |
'WorkflowInfo' { $Path = $cmd.Name; break } | |
Default { $Path = $cmd.Definition } | |
} | |
# Output Result | |
Write-Output ("{0} : {1}" -F $cmd.CommandType, $Path) | |
} | |
}catch [System.Management.Automation.CommandNotFoundException] { | |
Write-Warning "Command not found." | |
return "" | |
}finally{ | |
$ErrorActionPreference = $CurrentPref | |
} | |
} | |
# Set Alias | |
Set-Alias which Get-CommandString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果はこんな感じ。