Question Fonction dans une autre fonction

Plus d'informations
il y a 16 ans 4 mois #5608 par Arnaud
Bonjour,

Je suis assez débutant au niveau scripting Powershell et j'aurai besoin d'effectuer la tache suivante:

J'ai besoin d'appeller une fonction par son nom.
Comme c'est plus parlant en montrant du code voici ce que j'ai:

Function f1 {
}

Function f2 {
}

Function f3 {
}


J'ai besoin d'écrire une fonction qui appele les autres fonctions par leur nom:

Function MasterFunction {
param ($CalledFunction)

Do-SomethingFirst

$CalledFunction

Do-SomethingLast
}


L'idée serait d'appeller la fonction principale en utilisant le nom de l'autre fonction comme argument. Celle ci sera executé à l'intérieur de la fonction principale.

Comme ceci:

MasterFunction f1

ou

MasterFunction f3

Merci d'avance!

Arnaud<br><br>Message édité par: n0n0, à: 12/11/09 11:53

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 16 ans 4 mois #5609 par Laurent Dardenne
Salut,
avec les fonctions suivantes :
[code:1]Function f1 {
write-host \&quot;Dans F1\&quot;
}

Function f2 {
write-host \&quot;Dans F2\&quot;
$i=0
15/$i
}

Function f3 {
write-host \&quot;Dans F3\&quot;
}[/code:1]
Première passe, on utilise le provider Function :
[code:1]
Function MasterFunction {
param ([string]$FunctionName)

$CodeFonction=(get-item function:$FunctionName).ScriptBlock
&amp;$CodeFonction
}
\&quot;F1\&quot;,\&quot;F3\&quot;|%{ MasterFunction $_}
[/code:1]
Seconde passe, on se protége de possibles erreurs :
[code:1]
Function MasterFunction {
param ([string]$FunctionName)
if (Test-Path function:$FunctionName)
{
.{
trap
{
\&quot;Erreur dans la fonction $FunctionName\&quot;
throw $_.Exception.Message
}
&amp;(get-item function:$FunctionName).ScriptBlock
}
}
else {Throw \&quot;La fonction $FunctionName n'existe pas.\&quot;}
}
\&quot;F1\&quot;,\&quot;F2\&quot;,\&quot;F3\&quot;,\&quot;F0\&quot;|%{ MasterFunction $_}
[/code:1]
Troisième passe, on pousse le bouchon en passant un nombre inconnu de paramètres à la fonction appelée :
[code:1]
Function f5 ([Type] $Class, [Switch] $Full) {
write-host \&quot;Dans F5\&quot;
if ($Full)
{$Class.FullName}
else {$Class.Name}
}

Function MasterFunction {
param ([string]$FunctionName)
if (Test-Path function:$FunctionName)
{
.{
trap
{
\&quot;Erreur dans la fonction $FunctionName\&quot;
throw $_.Exception.Message
}
For ($i=0;$i -lt $Args.Count;$i++)
{$MyArgs+=\&quot;$($args[$I]) \&quot;}
Write-Debug \&quot;&amp;(get-item function:$FunctionName).ScriptBlock $MyArgs\&quot;
Invoke-Expression \&quot;&amp;(get-item function:$FunctionName).ScriptBlock $MyArgs\&quot;
}
}
else {Throw \&quot;La fonction $FunctionName n'existe pas.\&quot;}
}
#OK
#F5 Int32 -Full
#F5 Int32

$Class=[Int32]
MasterFunction \&quot;F5\&quot; $Class -Full
MasterFunction \&quot;F5\&quot; $Class
#Error
#MasterFunction \&quot;F5\&quot; [int32]
MasterFunction \&quot;F5\&quot; int32
[/code:1]
La dernière approche est à tester, mais le principe est là.

Belle idée ;)

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.045 secondes
Propulsé par Kunena