Powershell Parameter validation from SQL table

This topic contains 3 replies, has 3 voices, and was last updated by  Darwin Reiswig 2 years, 8 months ago.

  • Author
  • #29568

    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

    Peter Jurgens

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

  • #29591

    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

    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.