0

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')
  • Could you copy the full error please? Also, have you seen [this answer](https://stackoverflow.com/questions/762927/powershell-ps1-file-is-not-recognized-as-a-cmdlet-function-operable-program)? It could be an issue with file paths on the server. Is your python script running in the same directory as the power shell scripts? Are you sure all absolute paths exist on both your machine and the server? It'd probably be a good idea to use relative paths instead. – ConorSheehan1 Mar 08 '18 at 22:30
  • The entire error is: Enoc_Service-Checker.ps1: The term 'Enoc_Service-Checker.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of teh name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 Enoc_Service-Checker.ps1 +CategoryInfo : ObjectNotFound: (ENOC_Service-Checker.ps1:String[], CommandNotFoundException + FullyQualifiedErrorID : CommandNotFoundException Also I've confirmed the path and file location are correct. – DragonShadow Mar 08 '18 at 22:54
  • You have a typo in the error. 'Check the spelling of **teh** name'. Can you directly copy and paste the entire error instead of typing out part of it? – ConorSheehan1 Mar 08 '18 at 23:07
  • You selected Enoc_Practice-Items.ps1 Enoc_Practice-Items.ps1 : The term 'Enoc_Practice-Items.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Enoc_Practice-Items.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Enoc_Practice-Items.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – DragonShadow Mar 08 '18 at 23:21
  • That's a powershell exception, not a command prompt or python one, so this Python code in your question is finding and running your Enoc_Practice-Items.ps1 script. Inside that script, reading the error message, the first line is `Enoc_Practice-Items.ps1` which is not a valid command - and even if it was it would run itself in an infinite loop. Why is the first line of your powershell script the name of the script file? Should it have a `#` before it to make it a comment? – TessellatingHeckler Mar 09 '18 at 09:50
  • That's unreadable. Please [edit] your question. – Ansgar Wiechers Mar 09 '18 at 11:22
  • Weird because this is just a proof of concept script so the Powershell script is just a "Hello World" script # sample PowerShell Function hello { Write-Host "Hi from the hello function : )" } Function bye { Write-Host "Goodbye" } Write-Host "PowerShell sample says hello." So as you can see there is no name of the script in the first line. – DragonShadow Mar 09 '18 at 17:54
  • Thanks I figured ed out what was cause the issue it's the way I'm calling Powershell script incorrectly. I'm working on the resolution. – DragonShadow Mar 09 '18 at 22:59

0 Answers0