Is there a command to stop the running script?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 1 year, 7 months ago.

  • Author
    Posts
  • #28271
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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.

  • #28277
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

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

  • #28333
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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?

You must be logged in to reply to this topic.