Prompt user to continue or stop the execution of powershell script

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Vern Anderson Vern Anderson 3 years, 7 months ago.

  • Author
    Posts
  • #10335
    Profile photo of Robin16
    Robin16
    Participant

    Hi,

    I'm trying to put a condition in my script to prompt the user to continue or stop the execution.
    I have used read-host cmdlet, it asks for user input and two tabs 'Ok' and 'Cancel'. If i press Ok the script execution continues and if i click on Cancel, execution stops at that point but i believe that i not the purpose of read-host ( i expects an input from user ). Is there any cmdlet or procedure to prompt the user to just stop or continue the execution of powershell script.

    Please let me know , thanks in advance

  • #10336
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Does the cmdlet you using have a -Confirm parameter?

    if so you could use that

    It will ask for confirmation to proceed

    However it only works for a cmdlet & won't break you out of the script
    if you are responding to an error look at the Inquire option on -ErrorAction parameter

  • #10347
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    If the user will be running from powershell version 3 or greater the pause command is back and it works. However it didn't exist in PowerShell Version 1 or 2

    if ($PSVersionTable.PSVersion.Major -ge 3) {Get-Command Pause}

    also. . .
    http://blogs.technet.com/b/heyscriptingguy/archive/2013/09/14/powertip-use-powershell-to-wait-for-a-key-press.aspx

    -VERN

  • #10349
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    You might want to look at the definition of the pause command

    Read-Host 'Press Enter to continue...' | Out-Null

    Its not really that different to what you were doing.

    The more things change the more they stay the same

  • #10384
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    Thanks Richard I will look again. Last time I tried using that method to pause a script in PS v2 it continued after a few seconds.

You must be logged in to reply to this topic.