0

関数を定義するとき、カスタム列挙型をどのように参照できますか?

これが私が試していることです:

Add-Type -TypeDefinition @"
   namespace JB
   {
       public enum InternetZones
       {
          Computer
          ,LocalIntranet
          ,TrustedSites
          ,Internet
          ,RestrictedSites
       }
   }
"@ -Language CSharpVersion3

function Get-InternetZoneLogonMode
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]   
        [JB.InterfaceZones]$zone
    )
    [string]$regpath = ("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\{0}" -f [int]$zone)
    $regpath
    #...
    #Get-PropertyValue 
}

Get-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites

しかし、これはエラーを与えます:

Get-ZoneLogonMode : Unable to find type [JB.InterfaceZones]. Make sure that the assembly that contains this type is loaded.
At line:29 char:1
+ Get-ZoneLogonMode -zone [JB.InternetZones]::TrustedSites
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (JB.InterfaceZones:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

注意:ValidateSet同様の機能に使用できることは承知しています。ただし、名前の値しか持たないという欠点があります。バックグラウンドでintにマップされるフレンドリ名を使用してプログラムできるようにするのとは対照的です(そのためのコードを書くこともできますが、可能であればenumの方が適切だと思われます)。

私は Powershell v4 を使用していますが、理想的には、ほとんどのユーザーがデフォルトでそのバージョンを使用しているため、PowerShell v2 と互換性のあるソリューションが必要です。

アップデート

タイプミスを修正しました (PetSerAl に感謝します。よくわかりました)。 [JB.InterfaceZones]$zoneに変わりました[JB.InternetZones]$zone。今、私はエラーが表示されています:

Get-InternetZoneLogonMode : Cannot process argument transformation on parameter 'zone'. Cannot convert value "[JB.InternetZones]::TrustedSites" to type 
"JB.InternetZones". Error: "Unable to match the identifier name [JB.InternetZones]::TrustedSites to a valid enumerator name.  Specify one of the following 
enumerator names and try again: Computer, LocalIntranet, TrustedSites, Internet, RestrictedSites"
At line:80 char:33
+ Get-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-InternetZoneLogonMode], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-InternetZoneLogonMode
4

2 に答える 2

3

ISE はこれをくれましたが、試みた構文は完全に間違っているわけではありません。私はこれを行い、それを機能させることができました。

Get-InternetZoneLogonMode -Zone ([JB.InternetZones]::TrustedSites)

繰り返しますが、ハイライトを見ると、私がどのようにその結論に達したかがわかります。

構文の強調表示

于 2015-03-13T13:27:50.050 に答える
0

PetSerAl & CB のコメントによると:

  • 関数定義の誤字を修正

    • から[JB.InterfaceZones]$zone
    • [JB.InternetZones]$zone
  • 関数呼び出しの変更

    • からGet-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites
    • Get-InternetZoneLogonMode -zone TrustedSites
于 2015-03-13T12:44:08.177 に答える