skahle85
Goto Top

Powershell GUI - Write-Error to Outputbox

Hallihallo,

meine GUI schreitet immer weiter voran und ich hänge jetzt an der Fehlerausgabe...

Ich möchte das wenn eine Aktion nicht ausgeführt worden ist weil z.B. kein Computername eingetragen wurde die Fehler in meiner OutputBox sehen...

Hier mal ein Beispiel der Abfrage:
#Funktion GPO
function GPO {
$wks=$InputBox.text; #takes the text from the input box into the variable $wks
$GPOResult=Invoke-Command -ComputerName $wks -ScriptBlock { Get-GPO -all } | fl | out-string;
$outputBox.text=$GPOResult #send the results to the output box
If ($outputBox.Text.Contains("PSComputerName")) {$ButtonGPO.BackColor = [System.Drawing.Color]::LightGreen}  
Else {$ButtonGPO.BackColor = [System.Drawing.Color]::RED}
              } #end

Irgendwer einen nützlichen Hinweis?

Vielen Dank

Content-Key: 248924

Url: https://administrator.de/contentid/248924

Printed on: April 19, 2024 at 23:04 o'clock

Member: colinardo
Solution colinardo Sep 11, 2014 updated at 12:33:26 (UTC)
Goto Top
Hallo merlin,
das kannst du entweder über den allgemeinen Parameter -ErrorVariable machen der in fast jedem Befehl verfügbar ist, und dann den Inhalt an deine Textbox leiten.
Invoke-Command -ComputerName $wks .............  -ErrorVariable myErrorvar
if ($myErrorvar){
  $deineTextbox.Text = $myErrorvar
}
Oder du baust um den Befehl ein Try ..Catch Konstrukt und fängst den Fehler im Catch ab und trägst die Meldung in deine Textbox ein.
Try {
   Invoke-Command -ComputerName $wks ............. -ErrorAction Stop
} catch {
  $deineTextbox.Text = $_.Exception.Message
}
Es gibt noch andere Methoden, z.B. die globale Variable $error - mehr dazu und eine Einführung in das Errorhandling mit Powershell kannst du hier nachlesen.

Grüße Uwe
Member: skahle85
skahle85 Sep 11, 2014 updated at 11:40:00 (UTC)
Goto Top
Ey da such ich stundenlang nach der Antwort und dabei ist sie so naheliegend :'(.

Werde die Try and catch Variante bevorzugen!!
TIPPITOPPIGALOPPI und Abfahrt!

THANKS

Basti