I have a function accepting an enum value as a parameter, for example, consider this:
(ps)> Function ISItFriday ([System.DayOfWeek] $ dayOfWeek) {If ($ dayOfWeek -q [System.DayOfWeek] :: Friday) {"yes"} and {"no"}} Now, if I call it this way, then everything is fine:
(ps)> gt; $ M = [System.DayOfWeek] :: Monday (PS) & gt; IItFriday $ m no But if I call the function directly to the enum value, I get an impeccable error:
(PS) & Gt; IsItFriday [System.DayOfWeek] :: Monday IsItFriday: The value can not be changed because "System.DayOfWeek: :: Monday" to type "System.DayOfWeek" due to invalid calculation values. Specify one of the following enumeration values and try again. Possible calculation values are "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday". In line: 1 character: 11+ ISITFride & lt; & Lt; & Lt; & Lt; [System.DayOfWeek] :: Monday What is the difference between starting a variable with an Enum value and passing it directly to the enum value?
Yes, this is a misleading error message. I think you would better understand with an example:
Get-ChildItem -Path C: \ Note that is around C There is no quote: \ Because, one, it is implicitly converted to a string, and two, it is not necessary to attach that path in which the bridge passes the path in the form of a bridge Time does not include spaces.
then lets you go back to your function, and change it slightly:
function Iitfried ($ dayOfWeek) {$ dayOfWeek.GetType () if ($ dayOfWeek -q [System.DayOfWeek]:: Friday) {"yes"} and {"no"}} IsItFriday [System.DayOkWeek] :: Monday ... and output: < / P>
IsPublic IsSerial name basetipe -------- -------- ---- -------- True true string system. No object Look what happened there? PowerShell thinks that you are passing in a calculation value instead of a string, so you can not get the "code" "[System.DayOfWeek] :: Monday" because it is the string of the letter that Is passed
Comments
Post a Comment