[Powershell] Function Error Return Code

jan4321

Lt. Commander
Registriert
Juli 2007
Beiträge
1.929
Hi,
ich hab gerade angefangen ein wenig mit PowerShell herum zu spielen und hab auch schon einige Funktionen erstellt.
Nun hab ich aber gerade eine Funktion geschrieben, die für mich einige Hundert
Operationen macht, allerdings brauche ich zwingend dafür einen error return code, damit ich beim Fehler das Script stoppen kann.
am besten wäre da natürlich boolean, was ich einfach danach ab prüfen kann.

Irgendwie finde ich in google nix dazu^^
 
Hier ist eine Auflistung der Errorvariablen, die dir zur Verfügung stehen:

Error Variables

There are several global variables and global preference variables related to errors. Here is a brief primer on them:

■$? - contains the execution status of the last operation. True indicates the operation succeeded without any errors. False indicates either complete failure or partial success. Note: for Windows executables the exit code is examined. An exit code of 0 will be interpreted as success and non-zero as failure. Some Windows console apps don’t honor this convention so it is usually better to inspect $LASTEXITCODE such that you can determine for yourself success or failure based your interpretation of the exit code.
■$LASTEXITCODE – exit code of the last Windows executable invoked from this session.
■$Error – collection (ArrayList to be specific) of errors that have occurred in the current session. Errors are always inserted at the beginning of the collection. As a result, the most recent error is always located at index 0.
■$MaximumErrorCount – determines the size of the $Error collection. Defaults to 256 which is the minimum value allowed. Max value is 32768.
■$ErrorActionPreference – influences the dispatching of non-terminating errors. The default is ‘Continue’ which adds an entry to the $Error collection and displays the error on the host’s console.
■$ErrorView – specifies one of two views for error records when they’re displayed on the host. The default is ‘NormalView’ which displays several lines of information. For production environments, you can set this to ‘CategoryView’ to get a succinct one line error message. Remember that all the details are still available in the $Error collection.



Hilft dir das schon weiter, oder brauchste mehr?
 
Hallo,

du kannst (wie bei fast jedem Commandlet) die Fehlermeldung auch in eine Variable leiten:

Code:
Remove-Item -Errorvariable fehler

Zugriff dann einfach mit $fehler

Und wenn du das Skript im Fehlerfall einfach anhalten willst:

Code:
Remove-Item -Erroraction Stop


Gruß
 
ne danke, das reicht mir schon Thx ;)

Remove-Item -Erroraction Stop ist zwar ganz nett, aber ich möchte nach einem Fehler noch eine E-Mail abschicken, das ich mitbekomme, dass da ein Fehler ist^^
 
Zuletzt bearbeitet:
jan4321 schrieb:
ne danke, das reicht mir schon Thx ;)

Remove-Item -Erroraction Stop ist zwar ganz nett, aber ich möchte nach einem Fehler noch eine E-Mail abschicken, das ich mitbekomme, dass da ein Fehler ist^^


Mit "-Erroraction Stop" geht es aber auch, du musst halt einen try und catch Block drum herum bauen :)

Also so:

Code:
try
{
   remove-item $file.fullname -erroraction Stop
}
catch
{
  Write-Warning "Fehler beim löschen der Datei"
}
 
Zurück
Oben