繁体   English   中英

PowerShell 中的多个输入

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM