locked
Write warning message RRS feed

  • Question

  • Morning,

    I need to write a warning message so that user know what will happen if he execute the command. Something like this

    $name = "Something"

    $warning = "If you proceed you will remove the group and all the resources within the group as well" 

    remove-<command> -name $name 

    When use press enter it should give a warning message and do you want to continue or something like that? Is this possible.

    I can use -WarningAction Inquire but that will not give me the warning message that I want to use.


    • Edited by medy5 Tuesday, April 17, 2018 8:26 AM
    Tuesday, April 17, 2018 8:25 AM

All replies

  • Hi,

    you can use the Write-Warning cmdlet

    "test" | Write-Warning

    In your case:

    $warning | Write-Warning
    Write-Host "Type "yes" if you are really sure that this is what you want otherwise the script will exit!"
    $answer = Read-Host
    If ($answer -eq "yes") { DO WHAT NEEDS TO BE DONE

    } else {

    DO SOMETHING DIFFERENT
    }

    hth


    Tuesday, April 17, 2018 8:29 AM
  • Hi Markus,

    I know but but if I have Write-Warning like this how to execute it after the command that will give the user warning message

    $name = "Something"

    $warning = Write-Warning"If you proceed you will remove the group and all the resources within the group as well" 

    remove-<command> -name $name (How to use this warning variable)

    Tuesday, April 17, 2018 8:31 AM
  • In your case you have to construct around that with the warning up front (with somethin I posted before).

    In other words:
    remove-item .... $SomeWarning
    This command would have removed something and after that it would warn about it which makes no sense.

    hth

    Markus

    Tuesday, April 17, 2018 9:28 AM
  • Morning,

    I need to write a warning message so that user know what will happen if he execute the command. 


    There is usually -whatif parameter ... 

    Tuesday, April 17, 2018 8:00 PM
  • Hi,

    You can also try the following example to generate a confirmation dialog:
    $message = ('This action will perform balabala. Do you want to continue?')
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes'
    $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No'
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $result = $host.UI.PromptForChoice($null, $message, $choices, 1)
    if (!$result) {
        # Do something
    }
    else {
        return
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 18, 2018 1:54 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script meet your needs?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 23, 2018 6:00 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 25, 2018 9:25 AM