Obtener la definición de función y repetir el código

Obtener la definición de función y repetir el código

He definido una función dinámica en PowerShell, como esta:

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

Luego puedo simplemente escribir testy ejecutar esa función, canalizándola a otros comandos, etc. Muy útil.

¿Hay alguna manera de obtener la definición del comando? ¿Puedo repetir el código de mi función test? (¿Sin tener que retroceder por mi historia hasta donde lo definí?)

Respuesta1

Para una función llamada test:

$function:test

O si el nombre de la función contiene un guión (por ejemplo test-function):

${function:test-function}

Alternativamente:

(Get-Command test).Definition

Respuesta2

(Get-Command Test).Definition

Así es como normalmente obtengo definiciones.

Respuesta3

Ambos enfoques, ${function:myFn}o (Get-Command myFn).Definition, solo funcionarán para funciones que se hayan creado localmente.

También puede ver la definición de funciones nativas como Get-EventLog(por ejemplo). con elCommandMetadatayProxyCommandcomandos en elSystem.Management.Automationespacio de nombres como este:

using namespace System.Management.Automation

$cmd = Get-Command Get-EventLog
$meta = New-Object CommandMetadata($cmd)
$src = [ProxyCommand]::Create($meta)
$src | Write-Output

Ver también:¿Podemos ver el código fuente de los cmdlets de PowerShell?

información relacionada