How can I run my Powershell Script inside of Visual Studio Code? - powershell

I have a few powershell Scripts (simple ones) I want to execute within Visual Studio Code.
When I run it with F5 the VSC turns orange (Debug Mode) for a few seconds and then turns back to normal without showing me anything.
This is the console output:
PS C:\Users\Huberdo\Documents\WindowsPowerShell> c:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox\HD-Mailbox.ps1
PS C:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox>
The Script should ask me to make a choice with Write-Host but it doesn't.
Here is the main script:
function Show-Menu() {
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
do {
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
cls
'You chose option #1'
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
}
I have installed and activated the Powershell Extension.
I googled and searched here on Stackoverflow for possible solutions or similiar questions, but couldn't find any.
Am I doing something wrong? I tried it with x86 and x64 PS.

You are missing a closing bracket in the Show-Menu function.
function Show-Menu() {
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
}
do {
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
cls
'You chose option #1'
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
If that does not solve your issue - then I would look into verifying that you installed the powershell extension properly and when you create the powershell script in VS you are choosing:
File -> New Project -> Powershell -> Powershell Script Project

Related

Return to menu after the input result

I'm working on a Show-Menu PS script. After the user gives in his input and he gets the result. He should see again the Menu and not "Enter the user ID: "
I know this is possible by removing the Show-Menu but than my user input is not selected for my commands.
Thanks in advance!
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
Read-Host
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
do {
$userName = Show-Menu
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')
If Show-Menu is only supposed to show a menu, then prompting a user for input with Read-Host is clearly not appropriate. Move that part out of the Show-Menu function:
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$userName = Read-Host
do {
Show-Menu
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')
[you may find it simpler to just use the Out-Gridview cmdlet. there is a text-only console version available if you can install such.]
the following is a slightly different approach. it accepts a list of possible menu choices and ONLY accepts those items OR x as valid choices.
i think the code is fairly obvious, so i will just show it ... and the onscreen output. [grin]
function Get-MenuChoice
{
[CmdletBinding ()]
Param
(
[Parameter (
Mandatory,
Position = 0
)]
[string[]]
$MenuList,
[Parameter (
Position = 1
)]
[string]
$Title,
[Parameter (
Position = 2
)]
[string]
$Prompt = 'Please enter a number from the above list or "x" to exit '
)
$ValidChoices = 0..$MenuList.GetUpperBound(0) + 'x'
$Choice = ''
while ([string]::IsNullOrEmpty($Choice))
{
Write-Host $Title
foreach ($Index in 0..$MenuList.GetUpperBound(0))
{
Write-Host ('{0} - {1}' -f $Index, $MenuList[$Index])
}
$Choice = Read-Host -Prompt $Prompt
Write-Host ''
if ($Choice -notin $ValidChoices)
{
[System.Console]::Beep(1000, 300)
Write-Warning ''
Write-Warning (' [ {0} ] is not a valid selection.' -f $Choice)
Write-Warning ' Please try again.'
Write-Warning ''
$Choice = ''
pause
}
}
# send it out to the caller
if ($Choice -eq 'x')
{
'Exit'
}
else
{
$Choice
}
} # end >>> function Get-MenuChoice
'***** demo usage below *****'
$MenuList = #(
'An Item'
'Some Other Item'
'Middle Menu Item'
'Yet Another Item'
'The Last Choice'
)
$Choice = Get-MenuChoice -MenuList $MenuList
'You chose [ {0} ] giving you [ {1} ].' -f $Choice, $MenuList[$Choice]
output for one invalid & one valid input ...
0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 66
WARNING:
WARNING: [ 66 ] is not a valid selection.
WARNING: Please try again.
WARNING:
Press Enter to continue...:
0 - An Item
1 - Some Other Item
2 - Middle Menu Item
3 - Yet Another Item
4 - The Last Choice
Please enter a number from the above list or "x" to exit : 1
You chose [ 1 ] giving you [ Some Other Item ].

Prompted multiple times to exit menu

trying to write a PowerShell script which uses a menu and sub menus. When I first run the code and press Q to quit the 'Main Menu', it quits back to console. However, if I go into 'Menu 1' then quit back to 'Main Menu' and then quit once more to the console, it runs this code again $input = Read-Host "Please choose an option"
I've tried looking for different methods of quitting the menu, but haven't found much luck as of yet. Any advice would be appreciated
See example code below:
function showMainMenu {
function showMenu1 {
do {
Write-Host "================ Menu 1 ================"
Write-Host "1: Option 1"
Write-Host "Q: Back to the main menu"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
function showMenu2 {
Write-Host "================ Menu 2 ================"
Write-Host "1: Option 1"
Write-Host "Q: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
Write-Host "================ Main Menu ================"
Write-Host "1: Menu 1"
Write-Host "2: Menu 2"
Write-Host "3: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
showMenu1
} '2' {
showMenu2
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
}
showMainMenu
Its stuck in a loop due to using do{}until().
function showMainMenu {
function showMenu1 {
do {
Write-Host "================ Menu 1 ================"
Write-Host "1: Option 1"
Write-Host "Q: Back to the main menu"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
function showMenu2 {
Write-Host "================ Menu 2 ================"
Write-Host "1: Option 1"
Write-Host "Q: Press Q to quit"
do {
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
Write-Host "Some text"
} 'q' {
showMainMenu
}
}
}
until ($input -eq 'q')
}
Write-Host "================ Main Menu ================"
Write-Host "1: Menu 1"
Write-Host "2: Menu 2"
Write-Host "3: Press Q to quit"
$input = Read-Host "Please choose an option"
switch ($input) {
'1' {
showMenu1
} '2' {
showMenu2
} 'q' {
break
}
}
}
showMainMenu

PowerShell User Input, Menu, Sub Menu,

I'm trying to create an interactive PowerShell script that will do the following:
Menu 1 - Prompt user for file path. Then based on file path I will cd into the directory
Menu 2 - Once user input is done I would have a second menu that would prompt user to pick which file to parse
One user selects option it will output file and then restart from Menu 2
I'm not understanding how to only show the first menu, then once user input is submitted jump to second menu, and once user selects and file is parsed - come back to second menu until "Q".
$Filepath = Read-Host -Prompt 'Please Enter File Path'
do
cd $FilePath
function Show-Menu {
Clear-Host
Write-Host "1: Press '1' for parsing test.txt"
Write-Host "2: Press '2' for parsing test2.txt"
Write-Host "3: Press '3' for parsing test3.txt"
Write-Host "Q: Press 'Q' to quit."
}
do {
Show-Menu $selection = Read-Host "Please make a selection"
switch ($selection) {
'1' {
'You chose option #1'
Clear-Host
Import-Csv txt.file -Delimiter '|' -Header '1' ,'2' | Out-GridView
}
}
pause
} until ($selection -eq 'q')
You don't have two menus in your post. You only have one. Unless you are saying you are considering that Read-Host a menu.
Is this what you are trying to accomplish?
Clear-Host
$Filepath = Read-Host -Prompt "`nPlease Enter File Path"
Push-Location -Path $Filepath
$MenuOptions = #'
"Press '1' for parsing test1.txt"
"Press '2' for parsing test2.txt"
"Press '3' for parsing test3.txt"
"Press 'Q' to quit."
'#
"`n$MenuOptions"
while(($selection = Read-Host -Prompt "`nSelect a option") -ne 'Q')
{
Clear-Host
"`n$MenuOptions"
switch( $selection )
{
1 { 'Code for doing option 1 stuff' }
2 { 'Code for doing option 2 stuff' }
3 { 'Code for doing option 3 stuff' }
Q { 'Quit' }
default {'Invalid entry'}
}
Pop-Location
}
Rather than using a loop, the other option is to have the 'menu' function call itself to show the menu again after you've completed the parsing.
You could also use Host.ChoiceDescription to build and show the menu for you.
$Filepath = Read-Host -Prompt 'Please Enter File Path'
Set-Location $FilePath
function Get-Selection {
$Title = "Please make a selection"
$Message = "Select File for parsing"
$Option1 = New-Object System.Management.Automation.Host.ChoiceDescription "test.txt"
$Option2 = New-Object System.Management.Automation.Host.ChoiceDescription "test2.txt"
$Option3 = New-Object System.Management.Automation.Host.ChoiceDescription "test3.txt"
$Option4 = New-Object System.Management.Automation.Host.ChoiceDescription "Quit"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($Option1,$Option2,$Option3,$Option4)
$host.ui.PromptForChoice($title, $message, $options, 0)
}
function Show-Menu {
switch (Get-Selection) {
0 {
Write-Host 'You chose option #1'
Clear-Host
Import-Csv txt.file -Delimiter '|' -Header '1' ,'2' | Out-GridView
Show-Menu # Show menu for next choice
}
1 {
Write-Host 'You chose option #2'
Clear-Host
# Import-Csv
Show-Menu # Show menu for next choice
}
2 {
Write-Host 'You chose option #2'
Clear-Host
# Import-Csv
Show-Menu # Show menu for next choice
}
3 {
Write-Host 'Quit'
}
}
}
Show-Menu # Load menu when script is run

double loop Quit first or repeat

I would like to be able to exit the script after using option 1 and Q.
R for repeat works and any other key would bring me back to the
Menu before. Can i just use another while ($response -eq "Q") ? or is the do while loop just wrong for that? i already tried a if else version but im doing it kinda wrong. Any help please?
function Show-Menu
{
param (
[string]$Title = 'Who? '
)
cls
Write-Host "================ $Title ================"
Write-Host "1: 1"
Write-Host "2: 2"
Write-Host "3: 3"
Write-Host "4: 4"
Write-Host "5: 5"
Write-Host "6: 6"
Write-Host "7: 7"
Write-Host "8: 8"
Write-Host "Q: Q to Quit."
}
do
{
Show-Menu
$input = Read-Host "Please choose."
switch ($input)
{
'1' {
cls
'You chose option #1'
do {
$response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
while ($response -eq "R")
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
} '3' {
cls
'You chose option #1'
} '4' {
cls
'You chose option #2'
} '' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
how about:
'1'
{
cls
'You chose option #1'
do
{
$response = Read-Host "R to repeat, Q to Quit or anything else to go back"
}
while ($response -eq "R")
if ($response -eq "q")
{
exit
}
}
Just make an if statement. If response contains the Char Q then return.
function Show-Menu
{
param (
[string]$Title = 'Who? '
)
cls
Write-Host "================ $Title ================"
Write-Host "1: 1"
Write-Host "2: 2"
Write-Host "3: 3"
Write-Host "4: 4"
Write-Host "5: 5"
Write-Host "6: 6"
Write-Host "7: 7"
Write-Host "8: 8"
Write-Host "Q: Q to Quit."
}
do
{
Show-Menu
$input = Read-Host "Please choose."
switch ($input)
{
'1' {
cls
'You chose option #1'
do {
$response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
while ($response -eq "R")
if($response.Contains("Q"))
{return}
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
} '3' {
cls
'You chose option #1'
} '4' {
cls
'You chose option #2'
} '' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
One way to do it

Exiting from a powershell menu

I've created a powershell menu script but occasionally I don't want to use the menu function I have created I want to come out and run a custom command, or even better add a menu feature which allows me to write a custom command.
Can anyone help me achieve this please?
write-host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
write-host "Exchange Online Management"
write-host
write-host "Press 1: To assign Full Access To A Mailbox"
write-host "Press 2: Get Mailbox Size"
write-host "Press 3: Custom Exchange Command"
write-host "Press Q: To Leave Exchange Management"
write-host
write-host
write-host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
$SM2 = Read-Host "Please Make A Selection"
Switch ($SM2) {
'1'{
DO something
}
'2' {
$Mailboxuser = read-host "Who's Mailbox are you trying to query?"
Get-MailboxStatistics $Mailboxuser | ft DisplayName, TotalItemSize, ItemCount
}
'3' {
return
}
'Q' {
Remove-PSSession $Session
return
}
}
}
until ($SM -eq 'Q')
The PowerShell console window is closing after it has finished executing your script.
You can get around this by running another powershell process with your script:
'Q' {
Remove-PSSession $Session
& powershell
}
The other option is to create a shortcut to your script and add the -NoExit switch, which stops the window from closing when the script is finished.
PowerShell -NoExit "C:\folder\script.ps1"