Is there a command to stop the running script?

Hi,

I want to know if there is a CTRL+C command, or a code, or something like that, that when the user hits inside a function it just stops the current script but returns to the function.

See about_Break. It’s much easier to provide solutions if you post code examples of what you are attempting to do.

Rob Simmers,

I’ll read the link you gave me.
Here is a part of the code for better understanding:

function MenuPrincipal{
$Loop = $True
While ($Loop){
Clear-Host
Write-Host
Write-Host “Office 365 and Exchange Online management console” -ForegroundColor Green
Write-Host
Write-Host “`t1) Manage lists” -ForegroundColor Yellow
Write-Host “`t2) Manage resources” -ForegroundColor Yellow
Write-Host “`t3) Manage users” -ForegroundColor Yellow
Write-Host “`t4) Verify licenses” -ForegroundColor Yellow
Write-Host “`t5) Quit console” -ForegroundColor Yellow
Write-Host

                        $Option = Read-Host "`tSelect an option [1-5]"
                        if (($Option -lt 1) -or ([int]$Option -gt 5)){
                           Clear-Host
                           Write-Host
                           Write-Host "`tThis option doesn't exists... Try again." -ForegroundColor Red
                           Write-Host
                           Pause
                           Clear-Host
                           $Loop = $True
                           }
                        Switch ($Option){
                               1{
                                MenuLists
                                }

                               2{
                                MenuResources
                                }
                                
                               3{
                                MenuUsers
                                }
                                
                               4{
                                MenuLicenses
                                } 
                                 
                               5{
                                $Loop = $True
                                Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession
                                Clear-Host
                                Set-Location C:\                                    
                                Exit
                                }
                               }
                        }
                  }

function MenuLists{
$Loop = $True
While ($Loop){
Clear-Host
Write-Host
Write-Host “Manage lists” -ForegroundColor Green
Write-Host
Write-Host “`t1) Lists with only one member” -ForegroundColor Yellow
Write-Host “`t2) Lists and members” -ForegroundColor Yellow
Write-Host “`t3) Lists hidden” -ForegroundColor Yellow
Write-Host “`t4) Lists with no members” -ForegroundColor Yellow
Write-Host “`t5) Return to the main menu” -ForegroundColor Yellow

                            $Option = Read-Host "`tSelect an option [1-5]"
                            if (($Option -lt 1) -or ([int]$Option -gt 5)){
                               Clear-Host
                               Write-Host
                               Write-Host "`tThis option doesn't exists... Try again." -ForegroundColor Red
                               Write-Host
                               Pause
                               Clear-Host
                               $Loop = $True
                               }
                            Switch ($Option){
                                   1{
                                    Clear-Host
                                    .\Lists.1-Lists_with_only_one_member.ps1
                                    Pause
                                    }

                                   2{
                                    Clear-Host
                                    .\Lists.2-Lists_and_members.ps1
                                    Pause
                                    }

                                   3{
                                    Clear-Host
                                    .\Lists.3-Lists_hidden.ps1
                                    Pause
                                    }

                                   4{
                                    Clear-Host
                                    .\Lists.4-Lists_with_no_members.ps1
                                    Pause
                                    }
                                                            
                                   5{
                                     $Loop = $True
                                     MenuPrincipal
                                     }
                                   }
                            }
               }

Clear-Host
$Office365Cred = Get-Credential $Mail -Message “Office 365 credentials”

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Office365Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session

Connect-MsolService -Credential $Office365Cred

Set-Location C:\PowerShell\Office365

MenuPrincipal

What I wanted is, if the user enters on Office 365/Exchange Online and wants to get which lists have one member only. The user press 1 and enter to start the script.
But if the user wants to stop that script from running, he need to use CTRL+C and that will not send him to the main menu but to the console. Right?

Hi,

I’m sending a new message on this post again…

I have found this code to block the use of CTRL+C on the console session:

[console]::TreatControlCAsInput = $true

Now I’m trying to find a way to create a shortcut for the “break” command.
Have tried using Set-PSReadlineKeyHandler from the PSReadline module, but on version 1.2 it has some problems with PT-BR keyboard (and there is no function break anyway).

Since I have a menu script which calls other scripts, I wanted to give the user the option to stop only one script from running, but return to the menu, and CTRL+C stops the entire script.

If it were me, I would have the script launch sub-script in a separate Powershell instances.

powershell.exe .\Lists.4-Lists_with_no_members.ps1

Then you aren’t trying to manage the capabilities in the seperate instance and just leveraging CTRL+C would kill a subscript, not the same main script.

Rob Simmers,

Thanks for replying again!
I’m using that approach on the Active Directory menu, when I have cmdlets that need credentials:

Start-Process PowerShell -Credential $ADcred -ArgumentList "-File .\DHCP.3-Reservations.ps1"

But the problem is with the Office 365 menu, where I have a session established and I couldn’t find a way to start another instance of PowerShell using the same session.

As for …

But the problem is with the Office 365 menu, where I have a session established and I couldn't find a way to start another instance of PowerShell using the same session.
What do you mean by this?

When connecting O365, you are literally proxying the Exchange cmdlets to your workstation.

This is an https session connection, and with any SSL enabled website each https connection is separate and unique, requiring a separate logon. This is by design.

If you are trying to do multiple O365, sessions, you are going to have to create new ones each time and set the import to clobber or use a prefixed session. Once you use a prefixed session, you must use the prefiexed cmdlet name not the default ones.

As for…

Now I'm trying to find a way to create a shortcut for the "break" command.

This is not a cmdlet, and can only be used inside a script operation flow (loops, etc…). users don interact with it as it is not a keyboard thing.

This is fully described via the link, which Rib Simmons pointed you to.

postanote,

Thanks for joining!
I’m not trying to do multiple O365 sessions.
I have created a menu so my colleagues can execute some O365 tasks that they aren’t aware how to do it. In that menu they have a lot of options to choose from.
But the problem is, when they start a script, they have to wait until it is finished. If they enter the wrong option, there is no way to “go back” easily. If they press CTRL+C, then the menu is gone and they don’t know how to use PowerShell to continue using it. That’s why Rob Simmers said that it would be better to use other instances, so if the person pressed CTRL+C they will kill only the script that is currently running, and the menu would still be open.

Image:

https://postimg.cc/image/oh35hu0cx/

Hmmm… Since it’s not possible, that’s ok then…

I have seen this solution elsewhere:

do {
1.9999999
# Test to see if escape was pressed within the loop
} while ( -not ( $Host.UI.RawUI.KeyAvailable -and ($Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").VirtualKeyCode -eq 2
7 ) ) ) Write-Host "ok"

If I press the ESC key, it would stop the code and print the “ok” text.

Is this a bad approach?

 

Thanks for your time guys!