PowerShell can't use the appropriate enumeration type?

Am I doing something stupid here?

I indicate that the function takes a certain type of enumeration as an argument:

PS> add-type -AssemblyName System.ServiceProcess PS> function test([System.ServiceProcess.ServiceControllerStatus]$x) { Write-host $x $x.gettype() } 

The type is most definitely in scope since I can access its instances (and I imported the assembly manually):

 PS> [System.ServiceProcess.ServiceControllerStatus]::Stopped Stopped 

Then, when I try to pass an instance of the specified enumeration to the function, it throws an error:

 PS> test [System.ServiceProcess.ServiceControllerStatus]::Stopped test : Cannot process argument transformation on parameter 'x'. Cannot convert value "[System.ServiceProcess.ServiceControllerStatus]::Stopped" to type "System.ServiceProcess.ServiceControllerStatus". Error: "Unable to match the identifier name [System.ServiceProcess.ServiceControllerStatus]::Stopped to a valid enumerator name. Specify one of the following enumerator names and try again: Stopped, StartPending, StopPending, Running, ContinuePending, PausePending, Paused" At line:1 char:6 + test [System.ServiceProcess.ServiceControllerStatus]::Stopped + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [test], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,test 

It is quite happy that imposes a rope:

 PS> test 'Stopped' Stopped System.ServiceProcess.ServiceControllerStatus 

What's happening?

+7
source share
4 answers

You use some information about parsing modes. You can put parens around the argument and it will work:

 test ([System.ServiceProcess.ServiceControllerStatus]::Stopped) 

As an alternative, conversions from string to enum occur naturally, so you can write:

 test Stopped 

Here are a couple of good links that discuss parsing modes:

http://technet.microsoft.com/en-us/library/hh847892.aspx http://rkeithhill.wordpress.com/2007/11/24/effective-powershell-item-10-understanding-powershell-parsing-modes /

+10
source

You can pass the enumeration value as a string, but you will not pass the type name as part of the argument, for example. this works great:

 PS> test Stopped Stopped System.ServiceProcess.ServiceControllerStatus 

However, when I call .NET methods, I prefer to use only the full enum value instead of the string. This is because .NET methods tend to have multiple overloads, and those that accept strings can confuse PowerShell when you need to choose the right overload.

+2
source

Apparently, PowerShell believes that I am sending it a string, not an enumeration object. You get the same error message if you provide the full name:

 PS> test '[System.ServiceProcess.ServiceControllerStatus]::Stopped' test : Cannot process argument transformation on parameter 'x'. Cannot convert value "[System.ServiceProcess.ServiceControllerStatus]::Stopped" to type "System.ServiceProcess.ServiceControllerStatus". Error: "Unable to match the identifier name [System.ServiceProcess.ServiceControllerStatus]::Stopped to a valid enumerator name. Specify one of the following enumerator names and try again: Stopped, StartPending, StopPending, Running, ContinuePending, PausePending, Paused" At line:1 char:6 + test '[System.ServiceProcess.ServiceControllerStatus]::Stopped' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [test], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,test 

Putting the enumeration in parentheses to make PowerShell evaluate it first does the trick:

 PS> test ([System.ServiceProcess.ServiceControllerStatus]::Stopped) Stopped System.ServiceProcess.ServiceControllerStatus 
0
source

You are not setting the $x parameter correctly. It needs to be wrapped in expression param() .

 function test { param( [ServiceProcess.ServiceControllerStatus] $x ) Write-host $x $x.gettype() } 

Alternatively, you can omit the System. part System. any type name.

-1
source

Source: https://habr.com/ru/post/1202109/


All Articles