python调用本地powershell方法

来源:互联网 发布:如何举报学校网络垄断 编辑:程序博客网 时间:2024/04/28 02:58

# -*- coding: utf-8 -*-import subprocess def python_call_powershell(ip):    try:        args=[r"powershell",r"D:\jzhou\test_ping.ps1",ip]  #args参数里的ip是对应调用powershell里的动态参数args[0],类似python中的sys.argv[1]        p=subprocess.Popen(args, stdout=subprocess.PIPE)        dt=p.stdout.read()        return dt    except Exception,e:        print e    return Falseif __name__=="__main__":    ip=["127.0.0.1"]    print python_call_powershell(ip)如果报错,说禁止执行脚本,是因为没有权限,所以,把上面的一行代码换成 args=[r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe","-ExecutionPolicy","Unrestricted", r"D:\jzhou\test_ping.ps1",ip]


0 0
原创粉丝点击