首页 > 解决方案 > 如何在 Powershell 中询问您是否要解锁此帐户(是/否)

问题描述

我是 powershell 的新手,我在问当你将用户信息从 AD 提取到变量 $User 后如何提示问题

例子:

$User = Read-Host -Prompt 'Input employee ID'
$LockedOut = Get-ADUser $User -Property LockedOut | foreach { $_.LockedOut }
Write-Output "Account Locked: $($LockedOut)"

帐户锁定:假

我怎么能:它应该检查帐户是否被锁定,如果是,它会提示问题你想解锁这个用户吗?(Y/N) 如果不是就提示ok

标签: windowspowershellactive-directory

解决方案


我认为这样的事情会有所帮助:

# get the employeeID from user input. This will return a string; not a ADUser object
$empID = Read-Host -Prompt 'Input employee ID'
if (-not [string]::IsNullOrWhiteSpace($empID)) {
    # try and find the user having property 'EmployeeID' set to $empID
    $user = Get-ADUser -Filter "EmployeeID -eq '$empID'" -Properties LockedOut, DisplayName
    if ($user) {
        # if we found the user and he/she is locked out
        if ($user.LockedOut) {
            $action = Read-Host -Prompt "Would you like to unlock user $($user.DisplayName)? (Y/N)"
            if ($action -eq 'Y') {
                $user | Unlock-ADAccount -Confirm:$false
            }
        }
        else {
            Write-Host "User $($user.DisplayName) is not locked out" -ForegroundColor Green
        }
    }
    else {
        Write-Warning "Could not find user with EmplyeeID '$empID'"
    }
}

推荐阅读