Question appel job dans une function

Plus d'informations
il y a 12 ans 1 mois #16902 par smadon
appel job dans une function a été créé par smadon
Bonjour,

J'ai une petite incompréhension... :-/


je voudrais appeler un job (déclaré dans un variable a la \"racine\" du script) dans une fonction, mais la fonction ne la voit pas.
why ????

[code:1]
$StartWaitJob = {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB -ItemType \"File\"
}
$StartWaitJob2 = {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB2 -ItemType \"File\"
}

function startIt {
Write-Host \"Start\"
Start-Job -ScriptBlock { $StartWaitJob } -Name \"startWaitJob\"
Start-Job -ScriptBlock { $StartWaitJob2 } -Name \"startWaitJob2\"

# Get all the running jobs
$jobs = get-job | ? { $_.state -eq \"running\" }
$total = $jobs.count
$runningjobs = $jobs.count

# Loop while there are running jobs
while($runningjobs -gt 0) {
Write-Host \"running Job=\" $runningjobs
# After updating the progress bar, get current job count
$runningjobs = (get-job | ? { $_.state -eq \"running\" }).Count
}
Write-Host \"FINISH : Job Running=\" $(Get-Job -State Running | Measure-Object).count
}

function FirstFunction {
StartIt

}

FirstFunction
[/code:1]

Par contre si je n'utilise pas le job en variable, ça marche.

[code:1]

function startIt {
Write-Host \"Start\"
Start-Job -ScriptBlock {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB -ItemType \"File\"
} -Name \"startWaitJob\"
Start-Job -ScriptBlock {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB2 -ItemType \"File\"
} -Name \"startWaitJob2\"

# Get all the running jobs
$jobs = get-job | ? { $_.state -eq \"running\" }
$total = $jobs.count
$runningjobs = $jobs.count

# Loop while there are running jobs
while($runningjobs -gt 0) {
Write-Host \"running Job=\" $runningjobs
# After updating the progress bar, get current job count
$runningjobs = (get-job | ? { $_.state -eq \"running\" }).Count
}
Write-Host \"FINISH : Job Running=\" $(Get-Job -State Running | Measure-Object).count
}

function FirstFunction {
StartIt

}

FirstFunction
[/code:1]


Question :
Comment faire pour appeller les 2 jobs en les déclarants à la racine?
Pourquoi faire ça me direz vous ? :-)

car StartWaitJob et StartWaitJob2 sont des scripts assez long et je les appelles aussi dans d'autre fonction.

D'avance merci d’éclairer ma lanterne du pourquoi ça passe pas. :-)
smadon

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

Plus d'informations
il y a 12 ans 1 mois #16903 par Philippe
Réponse de Philippe sur le sujet Re:appel job dans une function
Bonjour smadon

ton probleme est un probleme de portée, la portée d'une variable détermine la visibilité d'une variable dans PowerShell.
tu trouvera plus d'explication dans ce chapitre

crée ta variable de cette façon : [code:1]$sript:«»StartWaitJob = ...[/code:1] ca la rendra accessibles depuis les fonctions du script !! B)

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

Plus d'informations
il y a 12 ans 1 mois #16905 par Laurent Dardenne
Salut,
6ratgus écrit:

ton probleme est un probleme de portée,

Oui, mais pour ce code je ne pense pas que l'usage du modificateur Script: soit nécessaire.
Help about_scope :
\"Unless you explicitly make the items private, the items in the parent scope
are available to the child scope. However, items that you create and change
in the child scope do not affect the parent scope, unless you explicitly

specify the scope when you create the items.\"

Les variables sont accessibles dans le code :
[code:1]
$StartWaitJob = {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB -ItemType \"File\"
}
$StartWaitJob2 = {
Start-Sleep -seconds 60
New-Item -Path \"C:\Temp\" -Name WAITJOB2 -ItemType \"File\"
}

function startIt {
Write-Host \"Start 1\"
Write-host \"Code job 1 exist ? $(Test-path variable:«»StartWaitJob)\"
if (Test-path variable:«»StartWaitJob)
{ \"$StartWaitJob\"}
Write-host \"Code job 2 exist ? $(Test-path variable:«»StartWaitJob2)\"
if (Test-path variable:«»StartWaitJob2)
{ \"$StartWaitJob2\"}
}

function startIt2 {
Write-Host \"Start 2\"
Write-host \"Code job 1 exist ? $(Test-path variable:«»StartWaitJob)\"
if (Test-path variable:«»StartWaitJob)
{ \"$StartWaitJob\"}
Write-host \"Code job 2 exist ? $(Test-path variable:«»StartWaitJob2)\"
if (Test-path variable:«»StartWaitJob2)
{ \"$StartWaitJob2\"}
}

function FirstFunction {
StartIt
$StartWaitJob = { write-host \"Job 1 redéclaré\"}
$StartWaitJob2 = { write-host \"Job 2 redéclaré\"}
StartIt2
}

FirstFunction
[/code:1]
smadon écrit:

Par contre si je n'utilise pas le job en variable, ça marche.

Ton premier code, crée un job dont le code utilise une variable déclarée dans l'appelant, cela ne peut pas fonctionner :
[code:1]
Start-Job -ScriptBlock { $StartWaitJob } -Name \"startWaitJob\"
[/code:1]
Ton objectif étant de paramètrer un traitement, utilise directement la variable :
[code:1]
Start-Job -ScriptBlock $StartWaitJob -Name \"startWaitJob\"
[/code:1]
Le mieux étant de paramètrer ta fonction :
[code:1]
function startIt {
param (
[ScriptBlock] $sbJob1,
[ScriptBlock] $sbJob2
)
Start-Job -ScriptBlock $sbJob1 -Name \"startWaitJob\"
...
}

startIt $StartWaitJob $StartWaitJob2
[/code:1]
Sache que si tu paramètres ton code dans des modules tu aura d'autres soucis de portée, mais pour le moment ceci devrait suffire.

Tutoriels PowerShell

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

Plus d'informations
il y a 12 ans 1 mois #16907 par Philippe
Réponse de Philippe sur le sujet Re:appel job dans une function
toujours les bons conseils Laurent

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

Plus d'informations
il y a 12 ans 1 mois #16911 par smadon
Réponse de smadon sur le sujet Re:appel job dans une function
Hello,

Merci pour vos réponses, que je viens juste de voir.

Mais du coup, j'ai avancé un peu autrement.

J ai gardé mes fonctions que j'appelle dans des jobs et je passe mes variables en paramètre.
Est ce correct ?

[code:1]

# Variable
$Arg1 = \"notepad.exe\"
$Arg2 = \"D:\monfichier.txt\"


function StartWait {
param ( $Arg1, $Arg2 )

if (Test-Path $Args1) {
# Start Process
Start-Process -FilePath $Arg1 -ArgumentList \"Arg2\" -Wait
}

Start-Sleep -seconds 5
Write-Host \"startWaitFinish\"
} # End Function
$StartWaitJob = start-job -ScriptBlock ${function:«»StartWait} -Name \"startWaitJob\" -ArgumentList @($Arg1, Arg2)

function startIt {
Write-Host \"Start\"
$StartWaitJob | receive-job


}

function FirstFunction {
StartIt

}

FirstFunction
[/code:1]

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

Plus d'informations
il y a 12 ans 1 mois #16912 par smadon
Réponse de smadon sur le sujet Re:appel job dans une function
Hello,

Merci pour vos réponses, que je viens juste de voir.

Mais du coup, j'ai avancé un peu autrement.

J ai gardé mes fonctions que j'appelle dans des jobs et je passe mes variables en paramètre.
Est ce correct ?

[code:1]

# Variable
$Arg1 = \"notepad.exe\"
$Arg2 = \"D:\monfichier.txt\"


function StartWait {
param ( $Arg1, $Arg2 )

if (Test-Path $Args1) {
# Start Process
Start-Process -FilePath $Arg1 -ArgumentList \"Arg2\" -Wait
}

Start-Sleep -seconds 5
Write-Host \"startWaitFinish\"
} # End Function
$StartWaitJob = start-job -ScriptBlock ${function:«»StartWait} -Name \"startWaitJob\" -ArgumentList @($Arg1, Arg2)

function startIt {
Write-Host \"Start\"
$StartWaitJob | receive-job


}

function FirstFunction {
StartIt

}

FirstFunction
[/code:1]


Et petite question en plus:
Pour récupérer la réponse de ma fonction, je dois faire comment ??
Exemple : récupéré $return de la la function StartWait

[code:1]

# Variable
$Arg1 = \"notepad.exe\"
$Arg2 = \"D:\monfichier.txt\"


function StartWait {
param ( $Arg1, $Arg2 )

if (Test-Path $Args1) {
# Start Process
Start-Process -FilePath $Arg1 -ArgumentList \"Arg2\" -Wait
}

Start-Sleep -seconds 5
$return = \"All Good\"
Write-Host \"startWaitFinish\"
$return
} # End Function
$StartWaitJob = start-job -ScriptBlock ${function:«»StartWait} -Name \"startWaitJob\" -ArgumentList @($Arg1, Arg2)

function startIt {
Write-Host \"Start\"
$StartWaitJob | receive-job

write-Host \"retour de la function StartWaitJob=????\"

}

function FirstFunction {
StartIt

}

FirstFunction
[/code:1]<br><br>Message édité par: smadon, à: 10/02/14 18:40

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

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