我有用于检测输入参数的Python代码 - 如何在Powershell中执行类似操作

时间:2013-08-06 13:22:36

标签: python powershell

我有Python代码的snippit,AUTOMATICALLY检测到外部程序正在发送的许多参数之一。

在这种情况下,参数名称是日期发送的

__name__="__main__"
import sys, os, traceback
import commands

# Switch this to 0 when in production mode.
debugMode = 1

def main(args):
    try:
        attributeMap = parseInput(args)
        dateSent = attributeMap["date-sent"]

        print "Script-attribute=script value"
        return

    except:
        error()
        print "something went wrong!"
        return "something went wrong!"

def parseInput(args):
    attributeMap = {}
    delimiter = "="
    for item in args:
        if delimiter in item:
            tuple = item.split(delimiter)
            attributeMap[tuple[0]] = tuple[1]
    return attributeMap

def error():
    # "SCRIPT PROCESSING ERROR"
    if(debugMode):
        traceback.print_exc(file=sys.stdout)
    return ""

#-----------------------------------------------------------------
# DOS-style shells (for DOS, NT, OS/2):
#-----------------------------------------------------------------
def getstatusoutput(cmd):
    """ Return (status, output) of executing cmd in a
    shell."""

    pipe = os.popen(cmd + ' 2>&1', 'r')
    text = pipe.read()
    sts = pipe.close()
    if sts is None: sts = 0
    if text[-1:] == '\n': text = text[:-1]
    return sts, text

#-----------------------------------------------------------------
# Entry Point
#-----------------------------------------------------------------
if __name__ == "__main__":
    if(len(sys.argv) == 0):
        error()
    else:
        main(sys.argv)

如何在powershell中执行此操作,即我无法控制多个变量发送的外部程序,即发送数据,发件人姓名,发件人等等。如何使程序检测仅发送 - 数据,就像这个Python代码一样。

1 个答案:

答案 0 :(得分:1)

您在Powershell中使用$args,类似于Python示例中使用的sys.argv

在此处阅读更多内容 - http://technet.microsoft.com/en-us/library/ff730958.aspx