Powershell Parameter validation from SQL table

I’m trying to build a PowerShell script that has a parameter validated from a SQL table. It performs validation, but does not have tab completion from the list. Is it possible to have this do list validation and tab completion? The contents of the list in the SQL table can change over time, so I cannot hard code the parameter list into the script.

Here’s the code I’m trying to use to make sure the value for the parameter LoginID exists in the SQL table. Again, the validation works, but there is no tab completion. So if I don’t know the LoginID, I have to look it up elsewhere before I run the script instead of tab completion helping me out.

Param (
[parameter()]
[ValidateScript({$_ -in ((Invoke-Sqlcmd -ServerInstance SQLServer -Database DB1 -Query “Select * from LookupTable”).LoginID)})]
[string]
$LoginID
)

I am 99% sure that only the ValidateSet property is able to return values for tab completion.

Hi!

Look into dynamic parameters.

You should definitely consider whether you want to add this complexity and dependency though; is there another way you could approach this? Write a Get-Something that can pipe to your command? Use a configuration file/variable of sorts?

Some references, if you do go with dynamic params:

Cheers!

I decided to make the script copy details from another user, which means I won’t need the parameters to populate after all. I just read them from the SQL tables for the user being copied. Better script that way.

Thanks for the advice :slight_smile: