2017-03-02 1 views
1

질문 : 함수와 스위치를 호출하는 별칭을 설정할 수 있습니까?함수 및 스위치를 사용하는 PowerShell 네이티브 Cmdlet 집합 별칭

다음은 내가 작업하는 예입니다.

myidea : The term 'Test-FunctionIdea -SwitchIdea' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if 
a path was included, verify that the path is correct and try again. 
At line:1 char:1 
+ myidea 
+ ~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (Test-FunctionIdea -SwitchIdea:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

나를 어떻게하는지 알려 주시기 바랍니다 가능한 경우 : 여기

Set-Alias -Name myidea -Value 'Test-FunctionIdea -SwitchIdea' 
function Test-FunctionIdea { 
    param(
    [switch]$SwitchIdea 
) 
    if($SwitchIdea) 
    { 
    Write-Host 'Good Idea' 
    } 
} 

나는 데 오류입니다. 나는 이것을 인터넷에서 찾았고 결과가 없었다.

+5

'function myidea {Test-FunctionIdea -SwitchIdea}'. * "Bash에서는"거의 모든 목적을 위해 쉘 함수가 별칭보다 선호됩니다 "라는 추천이 있으며 PowerShell의 권장 사항은 더욱 강력합니다 .PowerShell"별칭 "은 진정한 별칭입니다. 명령, 스크립트, 함수 등을 포함하며 매개 변수 전달이나 미니 스크립트 작성을 지원하지 않습니다. "* - https://web.archive.org/web/20120213013609/http://huddledmasses.org/ powershell-power-user-tips-bash-style-alias-command – TessellatingHeckler

+0

그 두 번째 : – 4c74356b41

+0

@ 테셀 레이팅 헬러 감사합니다! 매력처럼 작동합니다. 질문에 답하고 싶으니까 적절한 신용을 줄 수 있습니까? –

답변

3

$MyInvocation.InvocationName을 확인 함수 내부 파라미터를 조정할 수

function Some-Function([switch] $foo) { 
    if ($MyInvocation.InvocationName -eq 'foo') { $foo = $true } 
} 

Set-Alias foo Some-Function 

에서 [보통 무시할] 장점은 새로운 기능의 범위에 대한 추가의 실행 콘텍스트를 생성하지 않는다는 것이다.

+2

프로덕션 코드에서는 사용하지 않지만 흥미로운 방법으로는 +1합니다. – briantist

관련 문제