Question
Fonction dans une autre fonction
- Arnaud
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 1
- Remerciements reçus 0
il y a 16 ans 4 mois #5608
par Arnaud
Fonction dans une autre fonction a été créé 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
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.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 16 ans 4 mois #5609
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Fonction dans une autre fonction
Salut,
avec les fonctions suivantes :
[code:1]Function f1 {
write-host \"Dans F1\"
}
Function f2 {
write-host \"Dans F2\"
$i=0
15/$i
}
Function f3 {
write-host \"Dans F3\"
}[/code:1]
Première passe, on utilise le provider Function :
[code:1]
Function MasterFunction {
param ([string]$FunctionName)
$CodeFonction=(get-item function:$FunctionName).ScriptBlock
&$CodeFonction
}
\"F1\",\"F3\"|%{ 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
{
\"Erreur dans la fonction $FunctionName\"
throw $_.Exception.Message
}
&(get-item function:$FunctionName).ScriptBlock
}
}
else {Throw \"La fonction $FunctionName n'existe pas.\"}
}
\"F1\",\"F2\",\"F3\",\"F0\"|%{ 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 \"Dans F5\"
if ($Full)
{$Class.FullName}
else {$Class.Name}
}
Function MasterFunction {
param ([string]$FunctionName)
if (Test-Path function:$FunctionName)
{
.{
trap
{
\"Erreur dans la fonction $FunctionName\"
throw $_.Exception.Message
}
For ($i=0;$i -lt $Args.Count;$i++)
{$MyArgs+=\"$($args[$I]) \"}
Write-Debug \"&(get-item function:$FunctionName).ScriptBlock $MyArgs\"
Invoke-Expression \"&(get-item function:$FunctionName).ScriptBlock $MyArgs\"
}
}
else {Throw \"La fonction $FunctionName n'existe pas.\"}
}
#OK
#F5 Int32 -Full
#F5 Int32
$Class=[Int32]
MasterFunction \"F5\" $Class -Full
MasterFunction \"F5\" $Class
#Error
#MasterFunction \"F5\" [int32]
MasterFunction \"F5\" int32
[/code:1]
La dernière approche est à tester, mais le principe est là.
Belle idée
avec les fonctions suivantes :
[code:1]Function f1 {
write-host \"Dans F1\"
}
Function f2 {
write-host \"Dans F2\"
$i=0
15/$i
}
Function f3 {
write-host \"Dans F3\"
}[/code:1]
Première passe, on utilise le provider Function :
[code:1]
Function MasterFunction {
param ([string]$FunctionName)
$CodeFonction=(get-item function:$FunctionName).ScriptBlock
&$CodeFonction
}
\"F1\",\"F3\"|%{ 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
{
\"Erreur dans la fonction $FunctionName\"
throw $_.Exception.Message
}
&(get-item function:$FunctionName).ScriptBlock
}
}
else {Throw \"La fonction $FunctionName n'existe pas.\"}
}
\"F1\",\"F2\",\"F3\",\"F0\"|%{ 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 \"Dans F5\"
if ($Full)
{$Class.FullName}
else {$Class.Name}
}
Function MasterFunction {
param ([string]$FunctionName)
if (Test-Path function:$FunctionName)
{
.{
trap
{
\"Erreur dans la fonction $FunctionName\"
throw $_.Exception.Message
}
For ($i=0;$i -lt $Args.Count;$i++)
{$MyArgs+=\"$($args[$I]) \"}
Write-Debug \"&(get-item function:$FunctionName).ScriptBlock $MyArgs\"
Invoke-Expression \"&(get-item function:$FunctionName).ScriptBlock $MyArgs\"
}
}
else {Throw \"La fonction $FunctionName n'existe pas.\"}
}
#OK
#F5 Int32 -Full
#F5 Int32
$Class=[Int32]
MasterFunction \"F5\" $Class -Full
MasterFunction \"F5\" $Class
#Error
#MasterFunction \"F5\" [int32]
MasterFunction \"F5\" 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
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Fonction dans une autre fonction