Question

I want to call a wlst/jython script from powershell with arguments. (Here is a simplified version)

hello.ps1

 param (
    [string]$cmd = "none"
 )

echo "cmd - $cmd"
Switch ($cmd) {
     "hello" {
             Write-Host "Start hello ..."
             $script='C:\_WORK_\hello.py'
             & java -cp C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar weblogic.WLST $script
         }
     "main" {
             Write-Host "w main $cmd" 
             $cmd='-c "hello"'
             $script="C:\_WORK_\hello_main.py"
             Write-Host "script: $script"
             & java -cp C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar weblogic.WLST $script $cmd
     }
     Default {Write-Warning "Invalid Choice. Try again."
                  sleep -milliseconds 750}
    } #switch

hello.py

print "Hello without main/args"

hello_main.py

import getopt

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hc:v", ["help", "command="])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    command = None
    verbose = False
    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-c", "--command"):
            command = a
        else:
            assert False, "unhandled option"
    print 'command: '+command
    if (command == 'hello'):
        print "if hello"

if __name__ == 'main':
    main()

With "hello.ps1 -cmd main" the script terminates after "print 'command: '+command" i cannot figure out why

>powershell c:\_WORK_\SAS\hello.ps1 -cmd main
cmd - main
w main main
script: C:\_WORK_\SAS\hello_main.py

Initializing WebLogic Scripting Tool (WLST) ...

command:  hello

>powershell c:\_WORK_\SAS\hello.ps1 -cmd hello
cmd - hello
Status ...

Initializing WebLogic Scripting Tool (WLST) ...

Hello without main/args
Was it helpful?

Solution

 "main" {
         Write-Host "w main $cmd" 
          $arg0 = "C:\_WORK_\SAS\hello_main.py"
          $arg1 = "-c"
          $arg2 = "hello"

          $allArgs = @($arg0, $arg1, $arg2)

         & java -cp C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar weblogic.WLST $allArgs
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top