PowerShell в то время как петля - PullRequest
0 голосов
/ 30 апреля 2018

Приведенный ниже код застревает в цикле, когда он достигает

$choice = read-host "Are you sure you want to disable the following user? (Y/N)" "$name"

Я набираю y, и этот вопрос повторяется.

Я пытался сделать одно и то же выражение IF. Любые идеи о том, как остановить этот цикл?

$choice = ""
$User = read-host "enter username"
$Name = Get-ADuser -Identity $Username | Select-Object Name

while ($choice -notmatch "y/n"){
    $choice = read-host "Are you sure you want to disable the following user? (Y/N)" "$name"

    If ($Choice -eq"Y"){
        Disable-ADAccount $Username
    } 
}

1 Ответ

0 голосов
/ 30 апреля 2018

Вы должны использовать трубу для оператора или, чтобы работать не как косая черта

Этот ...

while ($choice -notmatch "y/n"

... должно быть это ...

while ($choice -notmatch "y|n"

Это неправильно, потому что в опубликованном коде нет заполненной переменной для использования ...

Disable-ADAccount $Username

... исходя из вашего кода, должно быть это ...

Disable-ADAccount $User

Нет необходимости разделять переменную в выборе выбора. Просто сделай это.

$choice = read-host "Are you sure you want to disable the following user? (Y or N) $name"

Пример:

$choice = ""
$User = read-host "enter username"
$Name = $User

while ($choice -notmatch "y|n")
{
    $choice = read-host "Are you sure you want to disable the following user? (Y/N) $Name"

    If ($Choice -eq "y")
    { $User } 
}

enter username: test
Are you sure you want to disable the following user? (Y/N) test: u
Are you sure you want to disable the following user? (Y/N) test: u
Are you sure you want to disable the following user? (Y/N) test: y
test


enter username: test1
Are you sure you want to disable the following user? (Y/N) test1: h
Are you sure you want to disable the following user? (Y/N) test1: h
Are you sure you want to disable the following user? (Y/N) test1: n
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...