【问题标题】:Getting my Python program to run Power Shell Script让我的 Python 程序运行 Powershell 脚本
【发布时间】:2018-01-20 20:32:40
【问题描述】:

您好,如果我的问题重复,请原谅我,我已经搜索了以前的问题,但似乎没有完全相同的问题。我正在开发一个程序,该程序将扫描特定文件夹并搜索特定文件类型以创建供用户选择的菜单。一旦用户选择菜单选项,相应的文件就是一个power shell脚本。目前我的程序除了运行一个简单的 power shell 脚本之外什么都做。我尝试了几种配置,但它不起作用。如果有人能看到我可能做错了什么或为我提供一些指示,那就太好了。代码如下。

##Text Menu Dynamic test
##version 1
## Created By Dragonshadow
## Code produce in Notpad++ For python v3.4.4
import os
import subprocess
import time
import pathlib
import logging
import fnmatch
import re


## Directory Enumerator
fileFolderLocationFilter = fnmatch.filter(os.listdir('C:\\Users\\myfolder\\Documents\\Automation_Scripts\\ENScripts\\'),"*.ps1")
selectedFile=""


## Menu defined setting veriables
def ENOC_menu():
    files = fileFolderLocationFilter
    counter = 1
    print (20 * "=" , "Enoc Quick Menu" , 20 * "=")
    enumFiles = list(enumerate(files))
    for counter, value in enumFiles:
        str = repr(counter) + ") " + repr(value);
        print(str)
    str = repr(counter+1) + ") Exit";
    print(str)
    print (57 *    "_")
    str = "Enter your choice [1 - " + repr((counter+1)) + "]:"
    choice = int(input("Please Enter a Selection: "))
    selectedFiles = enumFiles[choice]
    return(selectedFiles[1])
    if choice > counter : 
        choice = -1
    elif choice != counter :
        print("Please selecte a valid choice")

    else:
        selectedFiles = enumFiles[choice]
        print(selectedFiles[1])
        ##selectedFiles = selectedFiles[1]
    return choice

def you_sure():
    opt = input("Are you sure Yes or No: ")
    if opt=="Yes":
        print("Continuing please wait this may take a moment...")

    elif opt=="No":
        print("returnig to Enoc Menu")


    else: ##Stays in loop
        print ("Please choose yes or no")

##count_down
def count_down ():
    count_down =  10
    while (count_down >= 0):
        print(count_down)
        count_down -= 1
        if count_down == 0:
            print("Task will continue")

            break



##initiating loop
loop = True
while loop:
    choice = ENOC_menu()
    print ("\n" +"You selected "+ choice +"\n")
    subprocess.call("C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe" + choice, shell=True)
    ##print ("---" +str(selectedFile))

【问题讨论】:

  • 最近我看到越来越多的问题,代码行包含在任意位置。这是一种新时尚还是什么?请不要那样做。 准确地发布您的代码,就像它在您的脚本中一样。
  • Ansgar,我不明白你是说我把我的代码包装错了。我只问了几个问题,如果没有,我似乎按照指示进行操作,您能否提供更多详细信息以便我纠正错误。
  • 您发布的代码是否与实际脚本中的代码(例如第 15 到 19 行)完全一致?如果没有(这是我的猜测):修复它。当您发布的代码与实际导致问题的代码不同时,我们无法为您提供帮助。
  • Ansgar - 所有部分的代码都是相同的。我有几个方法我现在没有在我的循环中调用,因为我正在研究调用 power shell 脚本的能力。我承认我离最好的程序员还差得很远,但我已经尽力提供我正在做的每一件事,试图为我可能做错的事情找到答案。

标签: python powershell


【解决方案1】:

您可能已经想通了,但我的问题出在subprocess.call() 行。您将 powershell.exe 路径和目标文件名连接在一起。见这里:

>>> scriptToRun = "c:\\users\\Username\\Documents\\WindowsPowerShell\\classtestscript.ps1"
>>> powershellExe = "c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe"
>>> print(powershellExe + scriptToRun)
c:\windows\system32\windowspowershell\v1.0\powershell.exec:\users\Username\Documents\WindowsPowerShell\classtestscript.ps1

在上面,两个字符串粘在一起,它们之间没有空格。 Windows 无法理解您要执行的操作。

在两者之间留一个空格,subprocess.call() 会明白你要做什么:

>>> print(powershellExe + ' ' + scriptToRun)
c:\windows\system32\windowspowershell\v1.0\powershell.exe c:\users\Username\Documents\WindowsPowerShell\classtestscript.ps1

【讨论】:

    猜你喜欢
    • 2014-03-23
    • 2012-11-24
    • 2019-12-02
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多