2013-07-02 151 views
0

我試圖通過Powershell將2個參數傳遞給python腳本。如何:通過Powershell將參數傳遞給Python腳本

CODE:

$env:PATHEXT += ";.py" 

[Environment]::SetEnvironmentVariable("Path", "$env:Path;c:\Program Files\lcpython15\", "User") 

$args1 = "Test1" 
$args2 = "Test2" 
$Python_SetAttrib = "c:\ProgramData\set_cust_attr.py " 

python $Python_SetAttrib $args1 $args2 

USAGE FROM CMD.exe: 
c:\ProgramData\set_cust_attr.py <custom attribute name> <custom attribute value> 

錯誤:

PS C:\ProgramData> python $Python_SetAttrib + $args1 + $args2 
usage: c:\ProgramData\set_cust_attr.sh <custom attribute name> <custom attribute value> 
OR 
usage: c:\ProgramData\Opsware\set_cust_attr.sh --valuefile <path to file with value  in  it> <custom attribute name> 

python.exe : Got more than one custom attribute name. 
At line:1 char:7 
+ python <<<< $Python_SetAttrib + $args1 + $args2 
    + CategoryInfo   : NotSpecified: (Got more than one custom attribute  name.:String) [], RemoteException 
    + FullyQualifiedErrorId : NativeCommandError 

Unhandled exception in thread started by 
Traceback (most recent call last): 
    File ".\client\__init__.py", line 88, in pumpthread 
    File "C:\Program Files\lcpython15\lib\site-packages\pythoncom.py", line 3, in ? 
    pywintypes.__import_pywin32_system_module__("pythoncom", globals()) 
    File "C:\Program Files\lcpython15\Lib\site-packages\win32\lib\pywintypes.py", line  68, in __import_pywin32_system_module__ 
    import _win32sysloader 
ImportError: No module named _win32sysloader 
+0

從錯誤消息來看,問題似乎與您的Python代碼,而不是您的PowerShell代碼。 –

+0

爲什麼在參數('python $ Python_SetAttrib + $ args1 + $ args2')之間使用'+'運算符?你想連接參數(如果是這樣,你需要用parens包圍它們,'($ Python_SetAttrib + $ args1 + $ args2)')?如果不是,我不知道你的意思。如果你的意思是讓它們成爲python腳本的獨立參數,那麼你完全不需要'+'。 – mojo

+0

當我通過()傳遞參數時,它給了我:「表達式或語句中的意外標記'args1'。」 – user2542412

回答

0

明白了!單引號.....

$env:Path += ";c:\Program Files\lcpython15"; 
$env:PATHEXT += ";.py"; 
$arg1 = "Test3" 
$arg2 = "Testing" 
$arg3 = 'c:\ProgramData\set_cust_attr.py' 
python $arg3 $arg1 $arg2  
相關問題