Passing enum values to a function in PowerShell

后端 未结 4 1820
悲&欢浪女
悲&欢浪女 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:22

    It's a little bit unexpected - you need to wrap it in parenthesis so that the value is evaluated:

    > IsItFriday ([System.DayOfWeek]::Monday)
    

    also it is possible to pass only strings like this:

    > IsItFriday Monday
    no
    > IsItFriday Friday
    yes
    

    PowerShell will convert it to the enum type. Handy, isn't it :)

提交回复
热议问题