Powershell Parameter validation from SQL table

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Darwin Reiswig Darwin Reiswig 1 year, 1 month ago.

  • Author
  • #29568
    Profile photo of Darwin Reiswig
    Darwin Reiswig

    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 (
    [ValidateScript({$_ -in ((Invoke-Sqlcmd -ServerInstance SQLServer -Database DB1 -Query "Select * from LookupTable").LoginID)})]

  • #29586
    Profile photo of Peter Jurgens
    Peter Jurgens

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

  • #29591
    Profile photo of Warren Frame
    Warren Frame


    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:


  • #29703
    Profile photo of Darwin Reiswig
    Darwin Reiswig

    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 🙂

You must be logged in to reply to this topic.