0

I have PowerShell console script (Not GUI) that has the following code:

$Servername = Read-host -Prompt "What is the server name?"

However, when running the script I want to type a few characters at time and have it lookup in external text file called servernames.txt for matches and the more characters I have the finer the results (Ideally I want to be able to select the match directly from the dynamic lookup).

The purpose is to facilitate typing names of hundreds of servers, as you wouldn't need to remember every name, because the servernames.txt file will have the whole server inventory.

I thought about Out-GridView, but not sure that would work in console script. Ideally should not pop-up another window.

dan1st
  • 12,568
  • 8
  • 34
  • 67
Elektro Kinetik
  • 57
  • 2
  • 10
  • There is no straight solution to do what you are looking for. However, as this is a console solution I would suggest considering to change your approach. You can create a function `Get-Servernames` with a `-Like` parameter that loads the file that you have your server names and returns the names that match that pattern...then you can pipe your output to other functions in your script that you need to use this server names. – Erick Guillen Nov 05 '19 at 21:56

1 Answers1

1

The Register-ArgumentCompleter cmdlet registers a custom argument completer. An argument completer allows you to provide dynamic tab completion, at run time for any command that you specify.

Here is a function example tailored to your question (assumes C:\servernames.txt)

$scriptBlock = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)

    Get-Content C:\servernames.txt | Where-Object {
        $_ -like "*$wordToComplete*"
    } | ForEach-Object {
        "'$_'"
    }
}
#Register the above scriptblock to the Test-DynamicArguments function ComputerName Parameter
Register-ArgumentCompleter -CommandName Test-DynamicArguments -ParameterName ComputerName -ScriptBlock $scriptBlock

function Test-DynamicArguments {
    [CmdletBinding()]
    param
    (
        $ComputerName
    )

    "You Selected $ComputerName"
} 

Now try Test-DynamicArguments with -ComputerName and part of a server name, you can tab complete to cycle options and also Ctrl-Space to show all.

Read the Register-ArgumentCompleter help page for more info, hope this helps

jfrmilner
  • 1,458
  • 10
  • 11
  • Awesome, Thanks! exactly what I was looking for :) How do I incorporate this into an existing script with the following: $Servername = Read-host -Prompt "What is the server name?" – Elektro Kinetik Nov 06 '19 at 19:32
  • Need to use autocomplete capability with existing program's $Servername = Read-host -Prompt section vs calling separate script/function with parameters... sorry kinda of noob with powershell so not sure how to do that ;) – Elektro Kinetik Nov 06 '19 at 19:40
  • You'll need to make a decision on rewriting as a function to have the dynamic capability or continue as you are with the limitations of `Read-Host`. If you want to learn more about functions then run `Get-Help about_functions` – jfrmilner Nov 07 '19 at 19:49