Question Propriété Error d'un Job

Plus d'informations
il y a 13 ans 2 mois #14353 par JONDON Cyril
[code:1]$Job1 = start-job {get-service}
if ($job1.error -eq $null) {Echo NOERROR} else {Echo ERROR}
if ($job1.error -ne $null) {Echo ERROR} else {Echo NOERROR}[/code:1]

$Job1 | gm
Error : {}

Commande 1 retourne ERROR
Commande 2 retourne NOERROR

Comment tester correctement ce type de proriété ?

Merci

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

Plus d'informations
il y a 13 ans 2 mois #14354 par SiSMik
Réponse de SiSMik sur le sujet Re:Propriété Error d'un Job
Bonjour,

Tu peux jeter un oeil ici

Bonne soirée

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

Plus d'informations
il y a 13 ans 2 mois #14355 par JONDON Cyril
Réponse de JONDON Cyril sur le sujet Re:Propriété Error d'un Job
En fait je pose cette question car j'ai exécuté un invoke-command -asjob sur 3 serveurs et les 3 jobs enfant du job parent sont avec etat \"Completed\".

[code:1]PS C:\Windows\System32\WindowsPowerShell\v1.0> (Get-Job -Name Job1).childjobs

Id Name PSJobTypeName State HasMoreData Location Command
-- ----




46 Job46 Completed True DCW2K301 get-content $env:TEMP\...
47 Job47 Completed True TEST-PC get-content $env:TEMP\...
48 Job48 Completed True JONDON-KU6OVP3E get-content $env:TEMP\...[/code:1]

Or si je récupère via un receive-job le contenu un à un des jobs enfants, un des job enfants (le premier) retourne une erreur (c'est tout rouge).

[code:1]PS C:\Windows\System32\WindowsPowerShell\v1.0> (Get-Job -Name Job1).childjobs | receive-job -keep
Impossible de trouver le chemin d'accès « C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\test.txt », car il n'existe pas.
+ CategoryInfo : ObjectNotFound: (C:\DOCUME~1\ADM...1\Temp\test.txt:«»String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
+ PSComputerName : DCW2K301

TEST-PC
JONDON-KU6OVP3E[/code:1]


Donc je ne voudrais récupérer que les jobs qui ne sont pas en erreur cad qui n'ont rien ds leur propriété Error par la syntaxe suivante

[code:1]PS C:\Windows\System32\WindowsPowerShell\v1.0> foreach ($i in (Get-Job -Name Job1).childjobs) {if ($i.error -eq $null) {Echo NOERROR} else {Echo ERROR}}
ERROR
ERROR
ERROR

PS C:\Windows\System32\WindowsPowerShell\v1.0> foreach ($i in (Get-Job -Name Job1).childjobs) {if ($i.error -ne $null) {Echo ERROR} else {Echo NOERROR}}
ERROR
NOERROR
NOERROR[/code:1]

avec -eq c'est incohérent
avec -ne c'est cohérent

C'est bizarre s'agit-il d'un bug ?? Cette propriété a peut-être une particularité qui m'échappe ? Peut être que $null n'est pas le bon test à faire qd la propriété error a ce contenu ? :

Error : {}

Un grand merci pour votre retour et bonne soirée également.

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

Plus d'informations
il y a 13 ans 2 mois #14362 par xyz
Réponse de xyz sur le sujet Re:Propriété Error d'un Job
cxvd7391 écrit:

C'est bizarre s'agit-il d'un bug ?

Non, il faut étudier la structure de l'objet, la proprièté Error est une collection vide ou renseignée avec n erreurs.
Ensuite, tu dois tester l'état et la propriété Error, un job 'Failed' peut avoir la collection Error vide.

[code:1]
&{ function Visu {
Write-Host \"`tValidation avec Count\"
$Job1.childjobs|
Foreach {
if ($_.Error.count -eq 0 )
{Echo NOERROR}
else
{Echo ERROR}
}
Write-Host \"`tValidation avec Count + State\"
$Job1.childjobs|
Foreach {
if ( ($_.Error.count -eq 0) -and ($_.State -ne 'Failed'))
{Echo NOERROR}
else
{Echo ERROR}
}
}
remove-job job1
$job1 = invoke-command -computerName localhost,bocalhost,localhost -scriptblock {dir c:\} -jobname Job1 -AsJob
$job1.ChildJobs|? {$_.JobStateInfo -eq 'Failed'}
Wait-job job1
Write-Host \"Test server offline\"
Visu

remove-job job1
$job1 = invoke-command -computerName localhost,bocalhost,localhost -scriptblock {Write-Error \"test\"} -jobname Job1 -AsJob
Wait-job job1
Write-Host \"Test server offline + Write-Error\"
Visu

remove-job job1
$job1 = invoke-command -computerName localhost,bocalhost,localhost -scriptblock {Throw \"test\"} -jobname Job1 -AsJob
Wait-job job1
Write-Host \"Test server offline + Exception\"
Visu
}
[/code:1]

Tutoriels PowerShell

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

Plus d'informations
il y a 13 ans 2 mois #14363 par JONDON Cyril
Réponse de JONDON Cyril sur le sujet Re:Propriété Error d'un Job
OK mais c'est bien sur.
C excellent c'est exactememt ce qui me manquait comment tester une collection (quel con ben bien sur la méthode count) une fois qu'on a la solution ça parait évident.

Merci.

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

Plus d'informations
il y a 13 ans 2 mois #14364 par xyz

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

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