0

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

你好.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

你好.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: '+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
4

1 回答 1

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
于 2013-01-21T11:13:49.453 回答