Trouble getting script to run

Hi everyone,

I’m hoping somebody can help me with this. I am working on learning Powershell and I’m having a very difficult time with getting scripts to run. I just finished a Powershell course in Udemy.
I am able to get scripts to run in the ISE in a limited way. The scripts will echo and will fully display, but will not do their function. For instance, I made a script that goes through menus and it doesn’t.
I try running a script outside the ISE and a Powershell box comes up for a few seconds and closes. I put a pause at the end of it and it says the local execution policy is preventing it from running.
Everything is unrestricted.
The course that I used set up a Windows server 2019 server with a GPO set to unrestricted for machine policy. I followed the chorus to the letter to set it up . The course showed user policy also unrestricted, but mine showed only machine policy unrestricted.
I have been fighting with this for over a week. Any help is greatly appreciated.

Hi, welcome to the forum :wave:

Please post the output of Get-ExecutionPolicy -List. When posting code and output, please use the </> button as it helps with readability.

This is what it currently is on my Windows Server 2019. I have a GPO set for machine and user.
< Scope ExecutionPolicy
----- ---------------
MachinePolicy Unrestricted
UserPolicy Unrestricted
Process Undefined
CurrentUser Unrestricted
LocalMachine Unrestricted>

I am currently running fresh install of Windows Server 2019 as a DC. It is the only server in the domain. I tried this as fresh install without all updates and with all updates.

Are you running as administrator? You may run into additional challenges if you are not when running on a DC.

I tried as the local administrator and created an AD Account, put it in the domain admin‘s group and try to that way too.
If there’s a nuance you would like me to try, I am all for it.

What happens if you try the following from a command or PowerShell - modify the necessary fields to reference your script:

PowerShell -ExecutionPolicy Bypass C:\Path\To\My\PowerShell\Script.PS1
1 Like

<powershell.exe -executionpolicy bypass C:\Users\Administrator\Desktop\FunctionMenu.ps1

    Scope ExecutionPolicy
    ----- ---------------

MachinePolicy Unrestricted
UserPolicy Unrestricted
Process Bypass
CurrentUser Unrestricted
LocalMachine RemoteSigned>
I also want to let you know the server crashed. I built a new one. Windows Server 2019, DC logging in as administrator and GPO allowing all scripts to run for machine and user.

I am confused. Is that the EXACT output from running your script? If so, can you share the script contents?

Yes. I ran it from ISE.
Here is the contents
<Function MainMenu($Message) {
cls
echo “*** Main Menu ***”
echo “Please choose an option below:”
echo “1) Sub menu 1”
echo “2) Sub menu 2”
echo “3) Quit”
Echo “”
echo “$Message”

# Use the switch statment to choose an option
Switch (Read-Host) {
    1 {Submenu1}
    2 {Submenu2}
    3 {Break}
    default {MainMenu("Error: You did not choose a valid option")}
} 

}

Function SubMenu1($Message) {
cls
echo “*** Sub Menu 1 ***”
echo “Please choose an option below:”
echo “1) Sub menu 1”
echo “2) Sub menu 2”
echo “3) Go Back”
echo “'n$Message”

# Use the switch statment to choose an option
Switch (Read-Host) {
    1 {Submenu1("You chose option 1")}
    2 {Submenu2("You chose option 2")}
    3 {MainMenu}
    default {Submenu1("Error: You did not choose a valid option")}
} 

}

Function MainMenu($Message) {
cls
echo “*** Sub Menu 2 ***”
echo “Please choose an option below:”
echo “1) Option 1”
echo “2) Option 2”
echo “3) Go back”
Echo “”
echo “'n$Message”

# Use the switch statment to choose an option
Switch (Read-Host) {
    1 {Submenu2("You chose option 1")}
    2 {Submenu2("You chose option 2")}
    3 {MainMenu}
    default {Submenu2("Error: You did not choose a valid option")}
} 

}>

Firstly, when posting code in the forum, please can you use the preformatted text </> button and paste your code into the section between the backticks. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

When you write a function, you need to call it. In the code you’ve posted, you never call MainMenu.
You need to add
MainMenu
to the bottom of your script to call the MainMenu function.

One thing to note is that you haven’t defined SubMenu2 so the MainMenu function towards the end of the script, which should be SubMenu2 going by the echo statement, is the function that will run. This will result in an error because regardless of what you input, it tries to call SubMenu 2 which doesn’t exist.

Have a look at the Learn Windows PowerShell in a Month of Lunches book or, if you prefer video learning, the video course:
Don Jones -YouTube

Both of these will teach you the fundamentals.

Thank you. I cant believe I missed that. Its actually a copy and paste from a course I finished and I missed the very last line, which is calling the mainmenu function. Thanks again to everyone.