2016-10-26 1 views
1

si essayer de faire quelque chose dans ineracvie powershell, est-il possible d'attraper une erreur speific? je veux dire:Catch Erreurs dans interacive Powershell

powershell.exe 
PS C:>_ 
PS C:>fuu 
fuu: this is not a cmdlet .... BLA BLA BLA 
+ ~~~ 
    + CategoryInfo   : ObjectNotFound: (fuu:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

Ce comportement est normal de ineractive Powershell. Puis-je mettre quelque chose dans mon Profile.ps1 ou alors que tout reste comme ça mais pour une erreur SPECIFIQUE, par exemple ces CommandNotFoundException, seulement sur ces erreurs je change le comportement de la powershell? peut-être un nouveau wirte-host ou autre chose. Donc oui, je sais essayer attraper et ainsi de suite -> IN SCRIPTS mais je veux dire interactivement comme comportement par défaut!

grâce

[UPDATE]

Pour éclaircir les choses. le cas réel est la codification. ExecutionPolicy est AllSignt et si j'oublie de signer TestCode ofcourse vous avez l'erreur:

+ ~~~~~~~~~~~~~~ 
    + CategoryInfo   : Sicherheitsfehler: (:) [], PSSecurityException 
    + FullyQualifiedErrorId : UnauthorizedAccess 

mais dans ce cas précis, je ne veux pas que cette erreur, je veux la question, VOULEZ-VOUS SIGNER? Y/N

.... et de le faire.

Répondre

0

utiliser cette structure:

$Error.Clear() 

fooo 

$Errormessage = $Error | Out-String 
$Errormessage 

vous recevrez:

fooo : The term 'fooo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
path is correct and try again. 
At line:5 char:1 
+ fooo 
+ ~~~~ 
    + CategoryInfo   : ObjectNotFound: (fooo:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

et quand vous avez chaîne vous pouvez faire parser et rendre votre gestionnaire d'erreur personnalisé comme

$customerror = $Errormessage.reaplace("fooo : The term 'fooo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
path is correct and try again.","Foo is not functin") 

write-output $customerror 
+0

mais comment je les ajoute au comportement normal de PS? Je ne veux pas ajouter des lignes de code à mon appel "fuu". je veux manipuler la gestion des erreurs de la coquille ineracive –

+0

@AnUser votre erreur d'édition moyenne powershell par defult? . Je ne sais pas c'est possible. Désolé mon ami . je vote juste votre question pour venir la question supérieure. et si tu veux, je peux enlever ma réponse. – saftargholi

+0

merci, non, ne l'enlevez pas, peut-être que cela efface quelque chose. –