Obtaining prompt response - Remove-ADComputer

This topic contains 3 replies, has 3 voices, and was last updated by  Rohn Edwards 3 weeks, 3 days ago.

  • Author
    Posts
  • #101364

    Ivan
    Participant

    Is there any way to capture the reply to the prompt when executing the Remove-ADComputer cmdlet?

    Remove-ADComputer -Identity $Computer -Confirm:$True
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

    Thanks.

  • #101371

    Don Jones
    Keymaster

    Nope. That response is returned to the internals of that command.

    You could probably write a proxy command to wrap it, but what's the goal here?

    • #101373

      Ivan
      Participant

      I'm actually wrapping it to make it easier for some folks, I want to have a Confirm and Non-Confirm, but the one with the prompt is the one causing trouble, it doesn't remove it in case the user answers NO, but the result is misleading since it will display REMOVED when it's not, capturing the response will allow me to display REMOVED, CANCELLED or ERROR accordingly.

      But I guess I will have to create a proxy-prompt like you mentioned Don.

      Thanks

    • #101377

      Rohn Edwards
      Participant

      Sounds like you might want to look into enabling 'SupportsShouldProcess.' That will make your wrapper function have -WhatIf and -Confirm parameters. You have to actually implement a $PSCmdlet.ShouldProcess() call if you do that, though, so you'll want to read up on that. You might also look into $PSCmdlet.ShouldContinue(), too, which gives a prompt like ShouldProcess(), except it always prompts (so you normally implement it with a -Force switch to suppress the prompt).

      Here's a simple example of what I'm talking about. It won't actually call Remove-ADComputer, so it's safe to play around with it and see the different warnings and strings it writes to get an idea of how it's supposed to work. Note that the whole if/else block where ShouldContinue() is used is optional (it is there to always prompt unless -Force is used, which can be very helpful sometimes).

      function Test-RemoveAdComputer {
          [CmdletBinding(SupportsShouldProcess, ConfirmImpact="Medium")]
          param(
              [Parameter(Mandatory)]
              [string] $ComputerName,
              [switch]$Force
          )
      
          if($PSCmdlet.ShouldProcess(
              "Removed AD computer '${ComputerName}",
              "Remove AD computer '${ComputerName}'?",
              "Removing Computer" 
          )) {
              if($Force -Or $PSCmdlet.ShouldContinue("Are you sure you want to remove '${ComputerName}' from AD?", "Removing Computer")) {
                  "Here's where you'd call the real command (make sure you pass -Confirm:`$false so that the user doesn't get prompted again"
              }
              else {
                  Write-Warning "The user answered no to the prompt, so you can do some sort of alternative action..."
              }
          }
          else {
              Write-Warning "Either -WhatIf was specified or `$ConfirmPreference is Low and user answered no to prompt, so do some sort of alternative action..."
          }
      }
      

You must be logged in to reply to this topic.