1

Hi I normally just right click and edit my scripts, then just run them through PowerShell ISE using the green arrow.

But I have a need to start /wait a script in a batch file. I want my script to run and then have the rest of the batch file to wait till the PowerShell script is closed. (Hence the start /wait)

And it works fine, but my issue is this: it opens up fine but no matter if I choose the letters by the options or the numbers I set in the choice script it will either restart or close out depending on the choice.

**I had nice pictures to go with this but I don't have enough rep so here is a bit of code :(

powershell.exe Set-ExecutionPolicy -ExecutionPolicy Bypass


#Main Choice Script

$IP = New-Object System.Management.Automation.Host.ChoiceDescription '&Edit IP', 'Change IP 
Address'
$Intro= New-Object System.Management.Automation.Host.ChoiceDescription '&Change Introscreen', 
'Change Introscreen'
$Gecko = New-Object System.Management.Automation.Host.ChoiceDescription '&Replace Gecko', 
'Change Gecko Folder'
$PCName = New-Object System.Management.Automation.Host.ChoiceDescription '&Host Name', 'Fix 
Host Name'
$Firewall = New-Object System.Management.Automation.Host.ChoiceDescription '&Firewall 
Settings', 'Fix Firewall Setting'
$Close = New-Object System.Management.Automation.Host.ChoiceDescription '&Close', 'Exit'



$options = [System.Management.Automation.Host.ChoiceDescription[]] 
($IP,$Intro,$Gecko,$PCName,$Firewall,$Close)

$title = 'IT Tool'
$message = 'What do you want to do?'
$result = $host.ui.PromptForChoice($title, $message, $options,-1)

switch ('$result')
{
    0 { "IP" }
    1 { "Intro" }
    2 { "Gecko" }
    3 { "PCName" }
    4 { "Firewall" }
    5 { "Close" }
    }

I cant seem to get the options to function right, I'm thinking:

  1. CMD is too basic to open a prompt for choice window.
  2. My code isn't setup to run outside ISE

** I'm fine that the cmd window is just text and not a popup, I just would like it to work.

Any help or tips would be appreciated.

2
  • There's nothing telling it to run more than once. You'll need to surround it with a while loop, then use break when you want it to end. Commented Dec 2, 2021 at 23:44
  • You should not need to use start /wait to run a PowerShell command or file, the next command should not run until that command has reported that it has finished. Are you wanting the PowerShell command or file to run in a completely separate console window? as opposed to the cmd.exe window your batch file is already running within. Commented Dec 3, 2021 at 0:36

2 Answers 2

0

As you mentioned you are willing to use batch-file I decided to post an option.

If your options are single commands per entry (or if you are willing to chain commands) you can preset the commands and use choice to select the option.

Note! Here I added some dummy commands to demonstrate how it works:

@echo off & cls
setlocal enabledelayedexpansion
set "sel="Edit IP","Change Introscreen","Replace Gecko",Hostname,Firewall,Close"
set "opt_1=ping localhost"
set "opt_2=echo something"
set "opt_3=echo Replace command here"
set "opt_4=hostname"
set "opt_5=echo firewall command here"
set "opt_6=exit /b"
:menu
set cnt=
for %%i in (%sel%) do (
    set /a cnt+=1
    echo !cnt!^) %%~i
    set "_opt!cnt!=%%~i"
)
for /l %%c in (1,1,%cnt%) do set ch=!ch!%%c
choice /C %ch% /M "Enter Selection:"
echo You chose !_opt%errorlevel%!
!opt_%errorlevel%!
pause
set ch= & cls & goto :menu
Sign up to request clarification or add additional context in comments.

Comments

0

You could of course just use a plain text menu directly in your batch file, by using the built-in choice.exe utility.

@Echo Off

:Menu
ClS
Echo 1. Edit IP
Echo 2. Change Introscreen
Echo 3. Replace Gecko
Echo 4. Host Name
Echo 5. Firewall
Echo 6. Close
%SystemRoot%\System32\choice.exe /C 123456 /M "What do you want to do"
GoTo Item%ERRORLEVEL%

:Item1
Echo Changing IP Address
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
GoTo :Menu

:Item2
Echo Changing Introscreen 
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
GoTo :Menu

:Item3
Echo Changing Gecko Folder
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
GoTo :Menu

:Item4
Echo Fixing Host Name
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
GoTo :Menu

:Item5
Echo Fixing Firewall Setting
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
GoTo :Menu

:Item6
Echo Exiting
%SystemRoot%\System32\timeout.exe /T 2 1>NUL
Exit /B

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.