如何在超时时实现 Get-Credential

How can I implement Get-Credential with a timeout

在我的一些脚本中,我有一个后备方法来通过 Get-Credential cmdlet 获取 PSCredential 对象。这在 运行 交互式地使用这些脚本时非常有用,尤其是。在测试等期间

如果我 运行 通过任务调度程序运行这些脚本,我想确保它们不会在等待交互式输入时卡住,如果它们没有必要的凭据就会失败.请注意,如果在正确的应用程序帐户下将任务设置为 运行,则永远不会发生这种情况。

有谁知道一种方法可以让我提示输入 超时 (并且可能是 NULL 凭证对象),这样脚本就不会卡住?

很高兴考虑使用 Read-Host 而不是 Get-Credential 的更一般情况。

我在一些基于读取主机超时的脚本中使用了类似的东西,代码在这里:

Function Read-HostTimeout {
#  Description:  Mimics the built-in "read-host" cmdlet but adds an expiration timer for
#  receiving the input.  Does not support -assecurestring

# Set parameters.  Keeping the prompt mandatory
# just like the original
param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$prompt,

    [Parameter(Mandatory=$false,Position=2)]
    [int]$delayInSeconds=5,

    [Parameter(Mandatory=$false,Position=3)]
    [string]$defaultValue = 'n'
)

# Do the math to convert the delay given into milliseconds
# and divide by the sleep value so that the correct delay
# timer value can be set
$sleep = 250
$delay = ($delayInSeconds*1000)/$sleep
$count = 0
$charArray = New-Object System.Collections.ArrayList
Write-host -nonewline "$($prompt):  "

# While loop waits for the first key to be pressed for input and
# then exits.  If the timer expires it returns null
While ( (!$host.ui.rawui.KeyAvailable) -and ($count -lt $delay) ){
    start-sleep -m $sleep
    $count++
    If ($count -eq $delay) { "`n"; return $defaultValue}
}

# Retrieve the key pressed, add it to the char array that is storing
# all keys pressed and then write it to the same line as the prompt
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp").Character
$charArray.Add($key) | out-null

# Comment this out if you want "no echo" of what the user types
Write-host -nonewline $key

# This block is where the script keeps reading for a key.  Every time
# a key is pressed, it checks if it's a carriage return.  If so, it exits the
# loop and returns the string.  If not it stores the key pressed and
# then checks if it's a backspace and does the necessary cursor 
# moving and blanking out of the backspaced character, then resumes 
# writing. 
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
While ($key.virtualKeyCode -ne 13) {
    If ($key.virtualKeycode -eq 8) {
        $charArray.Add($key.Character) | out-null
        Write-host -nonewline $key.Character
        $cursor = $host.ui.rawui.get_cursorPosition()
        write-host -nonewline " "
        $host.ui.rawui.set_cursorPosition($cursor)
        $key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
    }
    Else {
        $charArray.Add($key.Character) | out-null
        Write-host -nonewline $key.Character
        $key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
    }
}
Write-Host ""
$finalString = -join $charArray
return $finalString
}