함수의 정의를 얻고 코드를 에코


18

다음과 같이 powershell에서 동적 함수를 정의했습니다.

> function test { dir -r -fil *.vbproj | ft directory, name }

그런 다음 test해당 기능을 입력 하고 실행할 수 있으며 다른 명령으로 파이프하는 등 매우 편리합니다.

명령 정의를 얻을 수있는 방법이 있습니까? 함수 코드를 반향 할 수 있습니까 test? (내 역사를 다시 정의하지 않은 곳으로 되돌릴 필요가 없습니까?)

답변:


21

라는 함수의 경우 test:

$function:test

또는 함수 이름에 하이픈 (예 :)이 포함 된 경우 test-function:

${function:test-function}

또는

(Get-Command test).Definition

5
(Get-Command Test).Definition

이것이 제가 일반적으로 정의를 얻는 방법입니다.


$profile function def ($funcname) { (Get-Command $funcname).Definition }
콜롭 캐년

3

현재 답변은 로컬로 작성된 기능에 대해서만 작동합니다. 예를 들어와 같은 기본 함수의 정의를 볼 수 있습니다 Get-EventLog.

사용 가능한 모든 기능의 목록을 보려면 다음을 실행할 수 있습니다.

Get-ChildItem Function::

이들의 상관에 전달 될 수 ${function:myFn}또는(Get-Command myFn).Definition

네이티브 함수를 들여다보고 싶다면 다음 코드를 실행할 수 있습니다 .

$metadata = New-Object system.management.automation.commandmetadata (Get-Command Get-EventLog)
[System.management.automation.proxycommand]::Create($MetaData) | out-file C:\Get-EventLog.ps1
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.