简体   繁体   English

PowerShell 中的多个输入

[英]Multiple inputs in PowerShell

I have script where I ask for the user ID and I use $userName in all my commands.我有脚本,我在其中询问用户 ID,并在所有命令中使用 $userName。

Now I would like to also have the option to enter the PC name and turn back to the user ID, (if the user ID cannot be found it displays a custom error message).现在我还想选择输入 PC 名称并返回用户 ID(如果找不到用户 ID,则会显示自定义错误消息)。 The problem is that $userName is unique and should contain the user ID and not the PC name.问题是 $userName 是唯一的,应该包含用户 ID 而不是 PC 名称。 Side-note: PC names are imported with a CSV file.旁注:PC 名称是使用 CSV 文件导入的。 Also all pc names are starting with BN..此外,所有电脑名称都以 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)

To get the user ID with the PC name it should be like this:要获取带有 PC 名称的用户 ID,它应该是这样的:

 write-host "enter PC Name" $PCnaming = Read-Host $userName = $Computernames.Where({$_. "PC name" -like $PCnaming })."User ID" $userName

You can ask the user to prefix PC names with something that'll allow you to distinguish it from a user ID:您可以要求用户在 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)

If the user enters someusername it'll be stored in $userName , but if the user enter pc:computername , the loop will attempt to extract the user ID associated with computername如果用户输入someusername ,它将被存储在$userName中,但如果用户输入pc:computername ,循环将尝试提取与computername关联的用户 ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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