Powershell Parameter validation from SQL table

Welcome Forums General PowerShell Q&A Powershell Parameter validation from SQL table

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 4 months ago.

  • Author
    Posts
  • #29568

    Participant
    Points: 4
    Rank: Member

    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
    )

  • #29586

    Participant
    Points: 57
    Rank: Member

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

  • #29591

    Participant
    Points: 21
    Team Member
    Rank: Member

    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!

  • #29703

    Participant
    Points: 4
    Rank: Member

    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 🙂

The topic ‘Powershell Parameter validation from SQL table’ is closed to new replies.