Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active August 29, 2015 14:22
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 stknohg/858a21655522a6dd2c15 to your computer and use it in GitHub Desktop.
Save stknohg/858a21655522a6dd2c15 to your computer and use it in GitHub Desktop.
Get-Commandをちょっとラップしたやつ+whichエイリアス
<#
.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
@stknohg
Copy link
Author

stknohg commented Jun 11, 2015

実行結果はこんな感じ。

> which notepad

2 command found.
Application : C:\WINDOWS\system32\notepad.exe
Application : C:\WINDOWS\notepad.exe

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