Populate parameter list from XML

后端 未结 3 842
盖世英雄少女心
盖世英雄少女心 2021-01-05 22:29

I need to pull a list of IDs into a powershell paramater validateset like so:

function Do-Stuff {
   [Cmdletbinding()] 
   param(
        [ValidateSet(\"Sea         


        
3条回答
  •  抹茶落季
    2021-01-05 23:29

    Alternatively, if you want to use an file to validate against you can use ValidateScript instead of ValidateSet like such:

    function Do-Stuff {
       [Cmdletbinding()] 
       param(
            [ValidateScript({if(([xml](gc c:\temp\config.xml)).getElementsByTagName("City").Name -contains $_){$true}else{throw ([xml](gc c:\temp\config.xml)).getElementsByTagName("City").Name}} )]
            [String]$Site
        )
    

    That's based on the ridiculously simplistic XML file:

    
    
        
        
    
    

    So, anyway, you can use ValidateScript to run a scriptblock as validation, in which you could load things from an XML file. ValidateSet on the other hand has to have a pre-defined array of strings to validate against.

提交回复
热议问题