[英]Multiple inputs in PowerShell
我有腳本,我在其中詢問用戶 ID,並在所有命令中使用 $userName。
現在我還想選擇輸入 PC 名稱並返回用戶 ID(如果找不到用戶 ID,則會顯示自定義錯誤消息)。 問題是 $userName 是唯一的,應該包含用戶 ID 而不是 PC 名稱。 旁注:PC 名稱是使用 CSV 文件導入的。 此外,所有電腦名稱都以 BN 開頭。
Do {
# Check if a username is entered on the command prompt
Do {
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$userName = Read-Host
} Until ($userName)
# Check if the user exists in Active Directory
$user = $(
try {
Get-ADUser -Server domainlocal -Filter "samaccountname -eq '$userName'"
}
catch {
$null
}
)
# Output the result for the check in Active Directory
If (!$user) {
Write-host "The user does not exist, please try again." -ForegroundColor Red
}
Else {
Write-host "User found in Active Directory, loading options..." -ForegroundColor Yellow -NoNewline
''
}} Until ($user)
要獲取帶有 PC 名稱的用戶 ID,它應該是這樣的:
write-host "enter PC Name" $PCnaming = Read-Host $userName = $Computernames.Where({$_. "PC name" -like $PCnaming })."User ID" $userName
您可以要求用戶在 PC 名稱前加上一些前綴,以便您將其與用戶 ID 區分開來:
Do {
Write-Host "Enter the user ID (or enter a computer name prefixed with 'PC:'): " -ForegroundColor Cyan -NoNewline
$userName = Read-Host
if($userName -like 'PC:*'){
$PCnaming = $userName -replace '^PC:'
$userName = $Computernames.Where({$_. "PC name" -like $PCnaming })."User ID"
}
} Until ($userName)
如果用戶輸入someusername
,它將被存儲在$userName
中,但如果用戶輸入pc:computername
,循環將嘗試提取與computername
關聯的用戶 ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.