【发布时间】:2019-08-29 22:10:49
【问题描述】:
我正在尝试使用 $? 在 PowerShell 中创建一个循环以加入 Windows 域,如果最后一个命令返回错误,它应该返回 false/true。
这是我得到的:
Add-Computer -DomainName "domainname.local" -Credential "admin"
$MaxAttempts = 0
do {
if ($? -like "false" -and $MaxAttempts -lt 5) {
$MaxAttempts += 1
Write-Host "Attempt" $MaxAttempts "out of 5"
Add-Computer -DomainName "domainname.local" -Credential "admin"
} else {
Write-Host "test"
}
问题是当我在 do/until 循环之外有第一个命令时,即使我收到错误,它也会返回 true。
但是当我将此命令添加到 do/until 循环时,它会按预期返回 false,但这样我会运行该命令两次,这不是我想要的。
谁能给我解释一下这个魔法?
【问题讨论】:
标签: windows powershell