Getting Python code to call a PowerShell script and run it

时间:2018-03-08 22:09:41

标签: python powershell

I could really use some help on this python script that is to call and run existing PowerShell scripts that are in a specific folder. From want I can see on from many of the articles on this site I've read my code seems correct. First a little background I'm trying to write a python script that will take Powershell scripts in a targeted folder and create a menu that can be selected 1, 2, 3 etc. The use makes the selection and that corresponding Powershell script is run. Now the issue is when I place the code on a server and run it with some test PowerShell scripts I get the following error: The term "Filename.ps1" is not recognized as the name of a cmdlet, function, script file, or operable program. And of course the Powershell scripts won't run. A copy of my code is below. Can anyone see any issue.

## ENOC Text Menu Dynamic test
##version 1
## Created By MTDL Marcus Dixon
## Code produce in Notpad++ For python v3.4.4
import os, subprocess, time, pathlib, logging, fnmatch, 

re, sys, io

## Directory Enumerator
fileFolderLocationFilter = fnmatch.filter(os.listdir

('C:\\Users\\MTDl\\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





##initiating loop
loop = True
while loop:
    try:
        choice = ENOC_menu()
        scriptToRun = choice
        powershell = 'C:\\WINDOWS\\system32\

\WindowsPowerShell\\v1.0\\powershell.exe'
        print ('\n' +'You selected '+ choice 

+'\n')
        subprocess.call(powershell + ' ' + 

choice, shell=True)
    except OSError as err:
        logger.error(err) ##continue to work on 

logging this renders but may be incorrect.
        print ('OS error: {0}' .format(err))
    except ValueError:
        print ('Oops we had an issue try again')
    else:
        print ('---' +  choice + '---' + '\n')

0 个答案:

没有答案