Passing enum values to a function in PowerShell

后端 未结 4 1887
悲&欢浪女
悲&欢浪女 2021-02-19 20:52

I have a function accepting an enum value as parameter. As an example, consider something like:

(PS) > function IsItFriday([System.DayOfWeek] $dayOfWeek) { 
          


        
4条回答
  •  感动是毒
    2021-02-19 21:43

    Even handier is that strings will get converted to enum values if valid:

    function IsItFriday([System.DayOfWeek] $dayOfWeek) {   
        if($dayOfWeek -eq [System.DayOfWeek]::Friday) {  
            "yes"  
        } else {  
            "no"  
        }   
    }
    
    PS 7> IsItFriday Monday
    no
    PS 8> IsItFriday Friday
    yes
    

提交回复
热议问题