0

Hello please forgive me if my question duplicate, I've searched previous questions and nothing seems to be quite the same. I'm working on a program that will scan a specific folder and search for specific file types to create a menu for a user to select. Once the user select the menu option the the corresponding file which is a power shell script. Currently My program does everything but run even a simple power shell script. I've attempted several configuration and it's not working. It would be great if someone can see what I may be doing wrong or provide me with some pointers. Code below.

##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))
Jebby
  • 1,845
  • 1
  • 12
  • 25
  • 1
    Recently I see an increasing number of questions with code lines wrapped in arbitrary places. Is this a new fad or something? Please don't do that. Post your code *exactly* as it's in your script. – Ansgar Wiechers Jan 18 '18 at 23:15
  • Possible duplicate of [What's the best way to execute PowerShell scripts from Python](https://stackoverflow.com/questions/47094207/whats-the-best-way-to-execute-powershell-scripts-from-python) – wwii Jan 18 '18 at 23:21
  • Ansgar, I'm not understanding are you saying that I've wrapped my code wrong. I've only asked a few questions and it seemed that I am follow the direction if not can you provide more details so that I can correct my error. – DragonShadow Jan 19 '18 at 00:09
  • Does the code you posted look *exactly* like the code in your actual script (for instance lines 15 through 19)? If it doesn't (which is my guess): fix that. We can't help you when code you posted is different from the code that is actually causing the problem. – Ansgar Wiechers Jan 19 '18 at 00:13
  • Ansgar - The code in all section is the same. I have a couple of methods that I have not called to in my loop for now as I was working on the ability to call a power shell script. I will admit i'm no where near the best coder but I've tried to provide every thing I'm doing to try and get an answer for what I may be doing wrong. – DragonShadow Jan 23 '18 at 21:12

1 Answers1

0

You have probably already figured this out, but I the problem is in the subprocess.call() line. You are concatenating the powershell.exe path and the target file name together. See here:

>>> 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

Above, the two strings are stuck together without a space between them. Windows can't make sense of what you're trying to execute.

Put a space between the two two and subprocess.call() will understand what you're trying to do:

>>> print(powershellExe + ' ' + scriptToRun)
c:\windows\system32\windowspowershell\v1.0\powershell.exe c:\users\Username\Documents\WindowsPowerShell\classtestscript.ps1
veefu
  • 2,820
  • 1
  • 19
  • 29