从powershell调用wlst / jython

时间:2013-01-16 15:42:41

标签: python powershell jython wlst

我想从powershell使用参数调用wlst / jython脚本。 (这是简化版)

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()

使用“hello.ps1 -cmd main”脚本在“print”命令后终止:'+ command“ 我无法弄清楚为什么

>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

1 个答案:

答案 0 :(得分:0)

 "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