Home > Software engineering >  Running PowerShell Script in CMD (with popup window)
Running PowerShell Script in CMD (with popup window)

Time:12-03

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.

CodePudding user response:

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

CodePudding user response:

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