Sto facendo qualcosa di stupido qui?PowerShell non può utilizzare il tipo enum corrispondente?
specifico che una funzione accetta un particolare tipo enum come argomento:
PS> add-type -AssemblyName System.ServiceProcess
PS> function test([System.ServiceProcess.ServiceControllerStatus]$x) { Write-host $x $x.gettype() }
Il tipo è sicuramente portata poiché posso accedere a istanze di essa (e importato il gruppo manualmente):
PS> [System.ServiceProcess.ServiceControllerStatus]::Stopped
Stopped
Poi quando provo per passare la funzione un'istanza di detto enum, esso errori fuori:
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
E 'molto felice costringere una stringa, però:
PS> test 'Stopped'
Stopped System.ServiceProcess.ServiceControllerStatus
Cosa sta succedendo?
(messaggi di errore PowerShell sono così disordinato cercando. = /)
Abbastanza sicuro che sia davvero un vecchio consiglio dai giorni PS1 o qualcosa del genere ... Come si vede nelle altre risposte, la sintassi nella domanda funziona come previsto. È la chiamata che sta avendo problemi di analisi. – jpmc26