関数を定義するとき、カスタム列挙型をどのように参照できますか?
これが私が試していることです:
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