2013-07-02 109 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  
相关问题