-1

I need to remove a program, and need to prompt for a specific time otherwise it automatic select "No" for example :

I need if user do not enter input for 10 second it will take choice "N". Is This Possible ?

$app = "Microsoft.WindowsSoundRecorder"

$Choice = Read-Host  "Do You Want to Uninstall SoundRecoder App [YN] ?"
Switch ($Choice) {
    Y {"You Press Yes"}
    N {"You Press No"}

}
  • 1
    What have you tried so far and what specific issues are you getting? A stopwatch and a seperate runspace/thread would do this – I.T Delinquent Jun 28 '19 at 15:45
  • https://stackoverflow.com/questions/150161/waiting-for-user-input-with-a-timeout this may be useful – Ben Waid Jun 28 '19 at 16:06

2 Answers2

0

Instead of using Read-Host, you could also use the graphical Wscript.Shell popup method to show a messagebox that automatically times-out after a certain number of seconds:

function Show-Popup {
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullorEmpty()]
        [string]$Message,

        [string]$Title = 'Please choose..',

        [ValidateSet('OK','OKCancel','AbortRetryIgnore','YesNoCancel','YesNo','RetryCancel')]
        [string]$Buttons='OK',

        [ValidateSet('Error','Question','Warning','Information' )]
        [string]$Icon='Information',

        [ValidateSet('First','Second','Third' )]
        [string]$DefaultButton = 'First',       # as seen from left to right

        [int]$SecondsToWait = $null
    )
    # get the numeric value for the $Buttons (0..5)
    [uint32]$typeFlags = [array]::IndexOf(@('OK','OKCancel','AbortRetryIgnore','YesNoCancel','YesNo','RetryCancel'), $Buttons)

    # add the numeric value for the $Icon (16, 32, 48, 64)
    $typeFlags += 16 * ([array]::IndexOf(@('Error','Question','Warning','Information'), $Icon) + 1)

    # add the value for the default button
    $typeFlags += 256 * ([array]::IndexOf(@('First','Second','Third','Fourth'), $DefaultButton))

    try {
        $objShell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
        # show the popup and convert the returned int value to string
        switch ($objShell.Popup($Message, $SecondsToWait, $Title, $typeFlags)) {
            1       { return 'OK ' }
            2       { return 'Cancel ' }
            3       { return 'Abort ' }
            4       { return 'Retry ' }
            5       { return 'Ignore ' }
            6       { return 'Yes ' }
            7       { return 'No' }
            default { return 'TimeOut' }  # -1
        }
    }
    catch {
        Write-Warning "Could not create Wscript.Shell object. `r`n$($_.Exception.Message)"
    }
    finally {
        if ($objShell) { 
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
            [System.GC]::Collect()
            [System.GC]::WaitForPendingFinalizers()
            $objShell = $null
        }
    }
}

With that function in place, call on it like:

$answer = Show-Popup -Message 'Do you want to uninstall SoundRecoder app?' -Buttons YesNo -Icon Question -DefaultButton Second -SecondsToWait 10
if ($answer -eq 'TimeOut') {
    Write-Host "You did not respond.."
}
else {
    Write-Host "You answered $answer"
}

This will show up like this:

popup

Note: I'm running this on a Dutch machine, so the 'Yes' and 'No' translate to 'Ja' and 'Nee'

Theo
  • 57,719
  • 8
  • 24
  • 41
0

Here is the solution:-

[System.Reflection.Assembly]::LoadWithPartialName("microsoft.visualbasic") | Out-Null
$box = New-Object -ComObject "Wscript.shell"
$Value = $box.popup("Do You Want to Remove Adobe Reader ?" , 15 , "Please Confirm.." , 4 + 64)
switch ($Value) {
    "6" {
        Remove-Adobe
    }
    "7" {
    }
    "-1" {
    }
}