不是 PowerShell 方面的專家,但仍然學習。我發現 2 行或 2 個命令可以真正幫助我更快地完成作業。
目標是使用 PowerShell 解鎖用戶,如果它被鎖定,解鎖它,我就完成了。很簡單。
不過我該怎么做。它背后的邏輯是這樣的:
- 顯示用戶是否被鎖定
- 如果用戶被鎖定
- 然后解鎖
- 否則退出
- 如果用戶被鎖定
我使用的那兩條線是這樣的:
Unlock-ADAccount -Identity "usernamehere"
Get-ADUser -Identity 'usernamehere' -Properties LockedOut | Select-Object Name, Lockedout
我知道我可以使用Read-Host
來從用戶那里獲得一些輸入,但這就是我停止的地方。
我能想到的最好的是:
$user = read-host -prompt 'Enter Username'
$lockedout = get-aduser $user -property lockedout | foreach { $_.LockedOut }
Write-Output "Account Locked: $($LockedOut)"
但它不問我是否要解鎖
uj5u.com熱心網友回復:
行內注釋應該可以幫助您理解代碼的邏輯。PSHostUserInterface.PromptForChoice
方法對這類事情很好,這種方法將允許您通過附加幫助訊息提示用戶輸入,并且還將驗證用戶輸入,這意味著用戶必須提供正確的輸入(Y
或N
)。
$query = Read-Host -Prompt 'Enter Username'
$adUsr = Get-ADUser $user -Properties LockedOut
# if the user is locked
if($adUsr.LockedOut) {
# what do we do here?
$choice = $ExecutionContext.Host.UI.PromptForChoice(
'Locked User',
('"{0}" is locked, would you like to unlock it?' -f $adUsr.SamAccountName),
('&Yes', '&No'),
0 # => Default choice is `Yes`
)
# if choice was `Yes`
if($choice -eq 0) {
'"{0}" was sucessfully unlocked.' -f $adUsr.SamAccountName
}
# user was locked but it was decided to not unlock it
else {
# you can decide what to do here
}
}
# else, if the user is not locked
else {
'"{0}" is not locked. Goodbye.' -f $adUsr.SamAccountName
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530767.html
標籤:电源外壳活动目录