Funktionen mit IF-Schleifen

leonardo
Beiträge: 30
Registriert: 03 Mai 2016, 22:51

Funktionen mit IF-Schleifen

Beitrag von leonardo »

Hallo zusammen,

wie im Thread (viewtopic.php?f=6&t=9718) bereits angedeutet, bin ich gerade dabei, die neuen Funktionen mit lokalen Variablen auszuprobieren.

Dabei ist mir noch nachfolgender Bug aufgefallen, wenn in der ausgelagerten Funktion IF-Schleifen vorkommen. Dann wird das eigentliche Hauptscript nicht mehr weiterverarbeitet, wenn hier die Funktion ebenfalls innerhalb einer IF-Schleife aufgerufen wird.

Hier das Beispiel Hauptscript:

Code: Alles auswählen

encoding=UTF-8
[Actions]
 
DefVar $Var1$
DefVar $Var2$
DefVar $exitcode$

set $Var1$ = "1"
set $Var2$ = "2"

importLib "function-global-test.opsiscript"

if ($Var1$ = "1")
   set $exitcode$ = function-test($Var1$)
   comment $exitcode$

   if ($Var2$ = "2")
      comment $Var2$
   endif
endif
Hier die dazugehörige function:

Code: Alles auswählen

DefFunc function-test($VarSub1$ : string) : string
   set $result$ = $VarSub1$

   if ($result$ = "1")
      comment "Result ist okay"
   else
      comment "Result ist nicht okay"
   endif
endfunc
Ausgabe der Logdatei:

Code: Alles auswählen

============ Version 4.12.0.7 script "p:\test\setup.opsiscript"
[1] [Nov 28 12:00:54:321] [test]              used script encoding: UTF-8
[1] [Nov 28 12:00:54:321] [test]              used system encoding: cp1252
[1] [Nov 28 12:00:54:321] [test]              start: 2017-11-28  12:00:54 
[1] [Nov 28 12:00:54:321] [test]              installing product: test_1.0-1
[1] [Nov 28 12:00:54:321] [test]              on client named    "xxx"
[1] [Nov 28 12:00:54:321] [test]              loggedin user    ""
[1] [Nov 28 12:00:54:321] [test]              opsi-script running as    "SYSTEM"
[1] [Nov 28 12:00:54:321] [test]              opsi-script running with admin privileges
[1] [Nov 28 12:00:54:321] [test]              opsi-script running in standard script mode
[1] [Nov 28 12:00:54:321] [test] executing: "C:\Program Files (x86)\opsi.org\opsi-client-agent\opsi-winst\winst32.exe"
[1] [Nov 28 12:00:54:321] [test] system infos:
[1] [Nov 28 12:00:54:326] [test] xxx  -  PC hardware address
[1] [Nov 28 12:00:54:326] [test] xxx  -  IP name 
[1] [Nov 28 12:00:54:326] [test] xxx  -  IP address
[1] [Nov 28 12:00:54:326] [test] DEU  -  System default locale 
[1] [Nov 28 12:00:54:326] [test] MS Windows 6.1 64 Bit, Edition: Server Enterprise Edition (full installation)
[1] [Nov 28 12:00:54:326] [test] opsi service version : 4
[1] [Nov 28 12:00:54:326] [test] 
[7] [Nov 28 12:00:54:327] [test] Registry started readonly
[7] [Nov 28 12:00:54:327] [test] Registry started without redirection (64 Bit)
[6] [Nov 28 12:00:54:328] [test] Registry key [HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion]  opened
[6] [Nov 28 12:00:54:328] [test] Key closed
[6] [Nov 28 12:00:54:418] [test] Set  $Var1$ = "1"
[6] [Nov 28 12:00:54:418] [test]   The value of the variable "$Var1$" is now: "1"
[6] [Nov 28 12:00:54:418] [test] Set  $Var2$ = "2"
[6] [Nov 28 12:00:54:418] [test]   The value of the variable "$Var2$" is now: "2"
[7] [Nov 28 12:00:54:418] [test] Found ImportLib statement for file: function-global-test.opsiscript and function: 
[5] [Nov 28 12:00:54:439] [test] Imported all functions from file: p:\lib\function-global-test.opsiscript
[6] [Nov 28 12:00:54:441] [test] Added defined function:: function-unzip to the known functions
[6] [Nov 28 12:00:54:441] [test] If
[7] [Nov 28 12:00:54:441] [test]   $Var1$ = "1"   <<< result true
[6] [Nov 28 12:00:54:442] [test]   ($Var1$ = "1")   <<< result true
[6] [Nov 28 12:00:54:442] [test] Then
[6] [Nov 28 12:00:54:442] [test]   Set  $exitcode$ = function-test($Var1$)
[6] [Nov 28 12:00:54:442] [test]   Set  $result$ = $VarSub1$
[6] [Nov 28 12:00:54:442] [test]     The value of the variable "$result$" is now: "1"
[6] [Nov 28 12:00:54:442] [test]   If
[7] [Nov 28 12:00:54:442] [test]   $result$ = "1"   <<< result true
[6] [Nov 28 12:00:54:442] [test]   ($result$ = "1")   <<< result true
[6] [Nov 28 12:00:54:442] [test] Then
[5] [Nov 28 12:00:54:442] [test]   comment: Result ist okay
[6] [Nov 28 12:00:54:442] [test] Else
[6] [Nov 28 12:00:54:442] [test] EndIf
[6] [Nov 28 12:00:54:442] [test]     The value of the variable "$exitcode$" is now: "1"
[6] [Nov 28 12:00:54:442] [test]   If
[6] [Nov 28 12:00:54:442] [test]   Then
[6] [Nov 28 12:00:54:442] [test]   EndIf
[6] [Nov 28 12:00:54:442] [test] EndIf
[1] [Nov 28 12:00:54:443] [test] ___________________
[1] [Nov 28 12:00:54:443] [test] script finished: success
[1] [Nov 28 12:00:54:443] [test] 0 errors
[1] [Nov 28 12:00:54:443] [test] 0 warnings
[1] [Nov 28 12:00:54:443] [test] 
[1] [Nov 28 12:00:54:443] [test] installed product: test Version: 1.0-1
[1] [Nov 28 12:00:54:443] [test] 
Nach Abarbeitung der Funktion werden nachfolgende Zeilen des Hauptscriptes übergangen:

Code: Alles auswählen

comment $exitcode$

   if ($Var2$ = "2")
      comment $Var2$
   endif
Wenn in der Funktion keine IF-Schleife vorkommt, funktioniert das Hauptscript wie gewünscht.

Bitte um Info, wenn ich das Problem zu umständlich erklärt habe :)

Versionen:
opsi Server = 4.0.7.45
opsi-client-agent = 4.0.7.23-4
opsi-winst = 4.12.0.7-1

Viele Grüße
Leonardo
leonardo
Beiträge: 30
Registriert: 03 Mai 2016, 22:51

Re: Funktionen mit IF-Schleifen

Beitrag von leonardo »

Und noch ein Problem. Diesmal geht es aber nur um die Schönheit :)

Wenn die Funktion eine Switch Weiche enthält, wird der Inhalt nur noch sporadisch in die Logdatei ausgegeben.

Hauptscript:

Code: Alles auswählen

encoding=UTF-8
[Actions]
 
DefVar $Var1$
DefVar $Var2$
DefVar $exitcode$

set $Var1$ = "1"
set $Var2$ = "2"

importLib "function-global-test.opsiscript"

if ($Var1$ = "1")
   set $exitcode$ = function-exitcode_check("0","false","Reboot")
   comment $exitcode$

   if ($Var2$ = "2")
      comment $Var2$
   endif
endif
Beispielfunktion:

Code: Alles auswählen

DefFunc function-exitcode_check($exitcode$ : string, $Reboot$ : string, $RebootMode$ : string) : string
   Message "Teste Erfolg der Installation via exitcode "+$exitcode$
   set $result$ = $exitcode$

   Switch $exitcode$
           Case "0"
                   comment "Looks good: setup program gives exitcode zero"
           EndCase
           Case "2"
                   logError "Fatal: The system cannot find the file specified: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
           DefaultCase
                   logError "Fatal: Setup gibt einen unbekannten exitcode ungleich 0: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
   EndSwitch
endfunc
Ausgabe:

Code: Alles auswählen

[6] [Nov 28 13:03:12:909] [test]   Set  $exitcode$ = function-exitcode_check("0","false","Reboot")
[5] [Nov 28 13:03:12:909] [test]   message Teste Erfolg der Installation via exitcode 0
[6] [Nov 28 13:03:12:915] [test]   Set  $result$ = $exitcode$
[6] [Nov 28 13:03:12:915] [test]     The value of the variable "$result$" is now: "0"
[6] [Nov 28 13:03:12:916] [test]   Switch : 0
[6] [Nov 28 13:03:12:916] [test]     Case : 0
[6] [Nov 28 13:03:12:916] [test]     Case match: 0 = 0
Die Funktion wird richtig ausgeführt, nur der Kommentar erscheint nicht in der Logdatei.

Viele Grüße
Leonardo
Benutzeravatar
d.oertel
uib-Team
Beiträge: 3319
Registriert: 04 Jun 2008, 14:27

Re: Funktionen mit IF-Schleifen

Beitrag von d.oertel »

Hallo leonardo,

vielen Dank für den Bug Report.
Ich war in Urlaub und habe mir jetzt dazu ein internes Ticket aufgemacht.
Ich gehe mal davon aus, das es in den nächsten 2 Wochen eine Fix gibt .....

Ich melde mich dann.

gruß
d.oertel
opsi support - uib gmbh

For productive opsi installations we recommend support contracts.
http://www.uib.de
http://www.opsi.org
Benutzeravatar
d.oertel
uib-Team
Beiträge: 3319
Registriert: 04 Jun 2008, 14:27

Re: Funktionen mit IF-Schleifen

Beitrag von d.oertel »

Hi,

hier ist der Fix:
https://download.uib.de/opsi4.0/experim ... .10-1.opsi

gruß
d.oertel
opsi support - uib gmbh

For productive opsi installations we recommend support contracts.
http://www.uib.de
http://www.opsi.org
leonardo
Beiträge: 30
Registriert: 03 Mai 2016, 22:51

Re: Funktionen mit IF-Schleifen

Beitrag von leonardo »

Hi,

die verschachtelten IF-Schleifen funktionieren jetzt bestens. Vielen Dank dafür.
Der Schönheitsfehler mit den fehlenden Einträgen in der Logdatei besteht immer noch. Das ist aber kein Problem.

Ich hätte noch eine Frage zu den neuen Funktionen:
Ich benutzte in den Hauptscripten den Befehl "isSuccess" sehr häufig, um das Script zu verlassen.
Gibt es für die Funktionen etwas vergleichbares, um die Funktion zu verlassen und nicht das ganze Script?

Viele Grüße
Leonardo
Benutzeravatar
d.oertel
uib-Team
Beiträge: 3319
Registriert: 04 Jun 2008, 14:27

Re: Funktionen mit IF-Schleifen

Beitrag von d.oertel »

Hallo Leonardo,

schön das der Fix Funktioniert.
Der Schönheitsfehler mit den fehlenden Einträgen in der Logdatei besteht immer noch. Das ist aber kein Problem.
Kannst Du das bitte nochmal das Problem beschreiben. Ich habe es in den Therads nicht gefunden.
Gibt es für die Funktionen etwas vergleichbares, um die Funktion zu verlassen und nicht das ganze Script?
Nein - im Moment nicht.
Ich mache mir dazu mal gedanken (und ein internes Ticket)

gruß
d.oertel
opsi support - uib gmbh

For productive opsi installations we recommend support contracts.
http://www.uib.de
http://www.opsi.org
leonardo
Beiträge: 30
Registriert: 03 Mai 2016, 22:51

Re: Funktionen mit IF-Schleifen

Beitrag von leonardo »

Es geht um folgenden Beitrag viewtopic.php?p=43438#p43181

Bitte um Info, wenn ich es genauer beschreiben soll.

Viele Grüße
Leonardo
Benutzeravatar
d.oertel
uib-Team
Beiträge: 3319
Registriert: 04 Jun 2008, 14:27

Re: Funktionen mit IF-Schleifen

Beitrag von d.oertel »

Hallo Leonardo,

ich kann das Problem nicht nach vollziehen.
Zumindest nicht mit der aktuellen Version:
https://download.uib.de/opsi4.0/testing ... .12-1.opsi

bei mir sieht der Log so aus:

Code: Alles auswählen

5] [Dec 28 11:53:58:404] Imported all functions from file: /home/detlef/gitwork/opsi-script-test/CLIENT_DATA/standalone-tests/comment_in_imported_function_test_lib.opsiscript
[6] [Dec 28 11:53:58:409] Added defined function:: function-exitcode_check to the known functions
[6] [Dec 28 11:53:58:413] If
[6] [Dec 28 11:53:58:417]   ($Var1$ = "1")   <<< result true
[6] [Dec 28 11:53:58:418] Then
[6] [Dec 28 11:53:58:422]   Set  $exitcode$ = function-exitcode_check("0","false","Reboot")
[5] [Dec 28 11:53:58:426]   message Teste Erfolg der Installation via exitcode 0
[6] [Dec 28 11:53:58:433]   Set  $result$ = $exitcode$
[6] [Dec 28 11:53:58:441]     The value of the variable "$result$" is now: "0"
[6] [Dec 28 11:53:58:446]   Switch : 0
[6] [Dec 28 11:53:58:451]     Case : 0
[6] [Dec 28 11:53:58:455]     Case match: 0 = 0
[5] [Dec 28 11:53:58:459]       comment: Looks good: setup program gives exitcode zero
[6] [Dec 28 11:53:58:462]       EndCase
[6] [Dec 28 11:53:58:466]     Case : 2
[5] [Dec 28 11:53:58:471]   comment: 0
[6] [Dec 28 11:53:58:474]   If
[6] [Dec 28 11:53:58:478]     ($Var2$ = "2")   <<< result true
[6] [Dec 28 11:53:58:478]   Then
[5] [Dec 28 11:53:58:482]     comment: 2
[6] [Dec 28 11:53:58:485]   EndIf
[6] [Dec 28 11:53:58:489] EndIf
[1] [Dec 28 11:53:58:493] ___________________
[1] [Dec 28 11:53:58:493] script finished: success
[1] [Dec 28 11:53:58:493] 0 errors
gruß
d.oertel
opsi support - uib gmbh

For productive opsi installations we recommend support contracts.
http://www.uib.de
http://www.opsi.org
leonardo
Beiträge: 30
Registriert: 03 Mai 2016, 22:51

Re: Funktionen mit IF-Schleifen

Beitrag von leonardo »

Hallo,

das Beispiel war unglücklich gewählt. Hier ein besseres mit Deiner neuen Version getestet.

Leicht abgeändertes Hauptscript, es wird jetzt ein anderer Exitcode ausgewertet:

Code: Alles auswählen

encoding=UTF-8
[Actions]
 
DefVar $Var1$
DefVar $Var2$
DefVar $act_reboot_counter$
DefVar $exitcode$

set $Var1$ = "1"
set $Var2$ = "2"

importLib "function-global-test.opsiscript"

if ($Var1$ = "1")
   set $exitcode$ = function-exitcode_check("2359302",$act_reboot_counter$,"15","true","ImmediateReboot","test")
   comment $exitcode$

   if ($Var2$ = "2")
      comment $Var2$
   endif
endif
Aufgerufene Funktion:

Code: Alles auswählen

DefFunc function-exitcode_check($exitcode$ : string, ref $act_reboot_counter$ : string, $max_reboot_counter$ : string, $Reboot$ : string, $RebootMode$ : string, $ProductId$ : string) : string
   Message "Teste Erfolg der Installation via exitcode "+$exitcode$

   ; --- Workflow ---
   ; informations to exit codes see
   ; http://msdn.microsoft.com/en-us/library/aa372835(VS.85).aspx
   ; http://msdn.microsoft.com/en-us/library/aa368542.aspx
   Switch $exitcode$
           Case "0"
                   comment "Looks good: setup program gives exitcode zero"
           EndCase
           Case "2"
                   logError "Fatal: The system cannot find the file specified: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
           Case "3"
                   logError "Fatal: The system cannot find the path specified: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
           Case "1605"
                   comment "ERROR_UNKNOWN_PRODUCT  1605"
                   comment "This action is only valid for products that are currently installed."
                   comment "Uninstall of a not installed product failed - no problem"
           EndCase
           Case "1641"
                   comment "looks good: setup program gives exitcode 1641"
                   comment "ERROR_SUCCESS_REBOOT_INITIATED 1641"
                   comment "The installer has initiated a restart."
                   comment "This message is indicative of a success."
                   if ($Reboot$ = "true")
                      set $act_reboot_counter$ = calculate($act_reboot_counter$+"+1")
                      Registry_SaveRebootCounter
                      opsiServiceCall_set_prop_act_reboot_counter /preloginservice
                      if ($RebootMode$ = "ImmediateReboot")
                         ExitWindows /ImmediateReboot
                      else
                         ExitWindows /Reboot
                      endif
                   endif
           EndCase
           Case "3010"
                   comment "looks good: setup program gives exitcode 3010"
                   comment "ERROR_SUCCESS_REBOOT_REQUIRED 3010"
                   comment "A restart is required to complete the install."
                   comment "This message is indicative of a success."
                   if ($Reboot$ = "true")
                      set $act_reboot_counter$ = calculate($act_reboot_counter$+"+1")
                      Registry_SaveRebootCounter
                      opsiServiceCall_set_prop_act_reboot_counter /preloginservice
                      if ($RebootMode$ = "ImmediateReboot")
                         ExitWindows /ImmediateReboot
                      else
                         ExitWindows /Reboot
                      endif
                   endif
           EndCase
           Case "3011"
                   comment "looks good: setup program gives exitcode 3011"
                   comment "ERROR_SUCCESS_REBOOT_REQUIRED 3011"
                   comment "A restart is required to complete the install."
                   comment "This message is indicative of a success."
                   if ($Reboot$ = "true")
                      set $act_reboot_counter$ = calculate($act_reboot_counter$+"+1")
                      Registry_SaveRebootCounter
                      opsiServiceCall_set_prop_act_reboot_counter /preloginservice
                      if ($RebootMode$ = "ImmediateReboot")
                         ExitWindows /ImmediateReboot
                      else
                         ExitWindows /Reboot
                      endif
                   endif
           EndCase
           Case "5100"
                   logError "Fatal: The user's computer does not meet system requirements: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
           Case "14098"
                   logError "Fatal: The Component Store has been corrupted: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
           Case "2359302"
                   comment "SUS_S_ALREADY_INSTALLED  240006"
                   comment "The update to be installed is already installed - no problem"
           EndCase
           DefaultCase
                   logError "Fatal: Setup gibt einen unbekannten exitcode ungleich 0: " + $exitcode$
                   isFatalError "Exit Code: "+ $exitcode$
           EndCase
   EndSwitch

   [Registry_SaveRebootCounter]
   openKey [HKLM\SOFTWARE\opsi.org\winst]
   set "RebootFlag" = "$act_reboot_counter$"

   [opsiServiceCall_set_prop_act_reboot_counter]
   "method": "setProductProperty"
   "params": [
   "$ProductId$",
   "act_reboot_counter",
   "$act_reboot_counter$",
   "%hostid%"
   ]
endfunc
Ausgabe:

Code: Alles auswählen

Imported all functions from file: p:\lib\function-global-test.opsiscript
[6] [Dez 28 15:58:27:430] [test] Added defined function:: function-unzip to the known functions
[6] [Dez 28 15:58:27:441] [test] If
[7] [Dez 28 15:58:27:443] [test]   $Var1$ = "1"   <<< result true
[6] [Dez 28 15:58:27:443] [test]   ($Var1$ = "1")   <<< result true
[6] [Dez 28 15:58:27:443] [test] Then
[6] [Dez 28 15:58:27:443] [test]   Set  $exitcode$ = function-exitcode_check("2359302",$act_reboot_counter$,"15","true","ImmediateReboot","test")
[5] [Dez 28 15:58:27:444] [test]   message Teste Erfolg der Installation via exitcode 2359302
[6] [Dez 28 15:58:27:445] [test]   Switch : 2359302
[6] [Dez 28 15:58:27:445] [test]     Case : 0
[6] [Dez 28 15:58:27:445] [test]     EndCase
[5] [Dez 28 15:58:27:447] [test]   comment: 
[6] [Dez 28 15:58:27:447] [test]   If
[7] [Dez 28 15:58:27:447] [test]     $Var2$ = "2"   <<< result true
[6] [Dez 28 15:58:27:447] [test]     ($Var2$ = "2")   <<< result true
[6] [Dez 28 15:58:27:447] [test]   Then
[5] [Dez 28 15:58:27:447] [test]     comment: 2
[6] [Dez 28 15:58:27:447] [test]   EndIf
[6] [Dez 28 15:58:27:447] [test] EndIf
[1] [Dez 28 15:58:27:447] [test] ___________________
[1] [Dez 28 15:58:27:447] [test] script finished: success
[1] [Dez 28 15:58:27:447] [test] 0 errors
[1] [Dez 28 15:58:27:447] [test] 0 warnings
Ich möchte den Exitcode 2359302 überprüfen und dafür müsste die komplette Switch Schleife durchlaufen. Nach "Case : 0" hört die Ausgabe aber auf.

Eigentlich müsste aber noch nachfolgender Textblock erscheinen:

Code: Alles auswählen

Case "2359302"
                   comment "SUS_S_ALREADY_INSTALLED  240006"
                   comment "The update to be installed is already installed - no problem"
           EndCase


Viele Grüße
Leonardo
cgiesers
Beiträge: 44
Registriert: 22 Feb 2017, 02:11

Re: Funktionen mit IF-Schleifen

Beitrag von cgiesers »

Auch wenn das mit dem Bug nichts zu tun hat: Es gibt keine IF-Schleifen, nur IF-Abfragen! Das hat sich hoffentlich noch keiner abgeschaut.
Unser Prof hat damals jeden an den Pranger gestellt, der den (sorry) Unfug von sich gab. Zurecht. ;)
Antworten