tech.guitarrapc.cóm

Technical updates

PowerShellのType Acceleratorsについて

キャストや.Netリソースを利用する際に[String]や[System.IO.Path]などという形で、型を指定することが多々あります。 ご存じのとおり、[string]はFullNameでは[System.String]であり、短縮した表記になります。 今回は、このPoiwerShellにおけるType Acceleratorについてみてみました。 ====

既存のType Accelerators一覧

PowerShell V1.0におけるType Accesleratorsの一覧はここに紹介されています。

Windows PowerShell Blog - Type Shortcuts

PoewrShell V1.0では 27 のType Acceleratorsが存在しています。

[int]         typeof(int) 
[int[]]       typeof(int[]) 
[long]        typeof(long) 
[long[]]      typeof(long[]) 
[string]      typeof(string) 
[string[]]    typeof(string[]) 
[char]        typeof(char) 
[char[]]      typeof(char[]) 
[bool]        typeof(bool) 
[bool[]]      typeof(bool[]) 
[byte]        typeof(byte) 
[double]      typeof(double) 
[decimal]     typeof(decimal) 
[float]       typeof(float)
[single]      typeof(float)
[regex]       typeof(System.Text.RegularExpressions.Regex)
[array]       typeof(System.Array)
[xml]         typeof(System.Xml.XmlDocument)
[scriptblock] typeof(System.Management.Automation.ScriptBlock)
[switch]      typeof(System.Management.Automation.SwitchParameter)
[hashtable]   typeof(System.Collections.Hashtable)
[type]        typeof(System.Type)
[ref]         typeof(System.Management.Automation.PSReference)
[psobject]    typeof(System.Management.Automation.PSObject)
[wmi]         typeof(System.Management.ManagementObject)
[wmisearcher] typeof(System.Management.ManagementObjectSearcher)
[wmiclass]    typeof(System.Management.ManagementClass)

PowerShell V2.0ではどうでしょうか。 31個のType Acceleratorsが存在しますね。

int                   System.Int32                                          
long                  System.Int64                                          
string                System.String                                         
char                  System.Char                                           
bool                  System.Boolean                                        
byte                  System.Byte                                           
double                System.Double                                         
decimal               System.Decimal                                        
float                 System.Single                                         
single                System.Single                                         
regex                 System.Text.RegularExpressions.Regex                  
array                 System.Array                                          
xml                   System.Xml.XmlDocument                                
scriptblock           System.Management.Automation.ScriptBlock              
switch                System.Management.Automation.SwitchParameter          
hashtable             System.Collections.Hashtable                          
type                  System.Type                                           
ref                   System.Management.Automation.PSReference              
psobject              System.Management.Automation.PSObject                 
pscustomobject        System.Management.Automation.PSObject                 
psmoduleinfo          System.Management.Automation.PSModuleInfo             
powershell            System.Management.Automation.PowerShell               
runspacefactory       System.Management.Automation.Runspaces.RunspaceFactory
runspace              System.Management.Automation.Runspaces.Runspace       
ipaddress             System.Net.IPAddress                                  
wmi                   System.Management.ManagementObject                    
wmisearcher           System.Management.ManagementObjectSearcher            
wmiclass              System.Management.ManagementClass                     
adsi                  System.DirectoryServices.DirectoryEntry               
adsisearcher          System.DirectoryServices.DirectorySearcher            
psprimitivedictionary System.Management.Automation.PSPrimitiveDictionary   

そしてPowerShell V3.0です。 大幅拡充されて70個もあります。

Alias                  System.Management.Automation.AliasAttribute                 
AllowEmptyCollection   System.Management.Automation.AllowEmptyCollectionAttribute  
AllowEmptyString       System.Management.Automation.AllowEmptyStringAttribute      
AllowNull              System.Management.Automation.AllowNullAttribute             
array                  System.Array                                                
bool                   System.Boolean                                              
byte                   System.Byte                                                 
char                   System.Char                                                 
CmdletBinding          System.Management.Automation.CmdletBindingAttribute         
datetime               System.DateTime                                             
decimal                System.Decimal                                              
adsi                   System.DirectoryServices.DirectoryEntry                     
adsisearcher           System.DirectoryServices.DirectorySearcher                  
double                 System.Double                                               
float                  System.Single                                               
single                 System.Single                                               
guid                   System.Guid                                                 
hashtable              System.Collections.Hashtable                                
int                    System.Int32                                                
int32                  System.Int32                                                
int16                  System.Int16                                                
long                   System.Int64                                                
int64                  System.Int64                                                
wmiclass               System.Management.ManagementClass                           
wmi                    System.Management.ManagementObject                          
wmisearcher            System.Management.ManagementObjectSearcher                  
ciminstance            Microsoft.Management.Infrastructure.CimInstance             
NullString             System.Management.Automation.Language.NullString            
OutputType             System.Management.Automation.OutputTypeAttribute            
Parameter              System.Management.Automation.ParameterAttribute             
pscredential           System.Management.Automation.PSCredential                   
PSDefaultValue         System.Management.Automation.PSDefaultValueAttribute        
pslistmodifier         System.Management.Automation.PSListModifier                 
psobject               System.Management.Automation.PSObject                       
pscustomobject         System.Management.Automation.PSObject                       
psprimitivedictionary  System.Management.Automation.PSPrimitiveDictionary          
ref                    System.Management.Automation.PSReference                    
PSTypeNameAttribute    System.Management.Automation.PSTypeNameAttribute            
regex                  System.Text.RegularExpressions.Regex                        
sbyte                  System.SByte                                                
string                 System.String                                               
SupportsWildcards      System.Management.Automation.SupportsWildcardsAttribute     
switch                 System.Management.Automation.SwitchParameter                
cultureinfo            System.Globalization.CultureInfo                            
ipaddress              System.Net.IPAddress                                        
mailaddress            System.Net.Mail.MailAddress                                 
bigint                 System.Numerics.BigInteger                                  
securestring           System.Security.SecureString                                
timespan               System.TimeSpan                                             
uint16                 System.UInt16                                               
uint32                 System.UInt32                                               
uint64                 System.UInt64                                               
uri                    System.Uri                                                  
ValidateCount          System.Management.Automation.ValidateCountAttribute         
ValidateLength         System.Management.Automation.ValidateLengthAttribute        
ValidateNotNull        System.Management.Automation.ValidateNotNullAttribute       
ValidateNotNullOrEmpty System.Management.Automation.ValidateNotNullOrEmptyAttribute
ValidatePattern        System.Management.Automation.ValidatePatternAttribute       
ValidateRange          System.Management.Automation.ValidateRangeAttribute         
ValidateScript         System.Management.Automation.ValidateScriptAttribute        
ValidateSet            System.Management.Automation.ValidateSetAttribute           
version                System.Version                                              
void                   System.Void                                                 
xml                    System.Xml.XmlDocument                                      
scriptblock            System.Management.Automation.ScriptBlock                    
type                   System.Type                                                 
psmoduleinfo           System.Management.Automation.PSModuleInfo                   
powershell             System.Management.Automation.PowerShell                     
runspacefactory        System.Management.Automation.Runspaces.RunspaceFactory      
runspace               System.Management.Automation.Runspaces.Runspace             

Type Accelerators一覧を取得する

さて、現在のType Acceleratorsを取得するにはどうやればいいのでしょうか。 格納されている場所は、 System.Management.Automation.TypeAccelerators です。 しかし、そのまま読み込もうとすると…

[System.Management.Automation.TypeAccelerators]

アセンブリがないよー、とエラーが返ってきます。

型 [System.Management.Automation.TypeAccelerators] が見つかりません。この型を含むアセンブリが読み込まれていることを確認してください。
発生場所 C:\Users\acquire\AppData\Local\Temp\39890610-caf8-4fa5-a60b-772906b212e6.ps1:1 文字:1
+ [System.Management.Automation.TypeAccelerators]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Manageme...ypeAccelerators:TypeName) []、RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

ということで、[PSObject].Assembly.GetType()経由で、::Get.GetEnumerator()にて一覧が取得できます。 上記のPowerShell V2.0 / 3.0の一覧もこのコードで取得しています。

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get.GetEnumerator()

格納されている情報をGetType()で見てみると

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get.GetType().FullName

Dictionary`2ですね。

System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Get-Memberで見ても確かにそのようです。

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get | Get-Member

ということで、Key(Type Accelerator)で並べてみましょう。

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get.GetEnumerator() | sort Key

少しは見やすくなった?でしょうかw

Key                    Value                                                       
---                    -----                                                       
adsi                   System.DirectoryServices.DirectoryEntry                     
adsisearcher           System.DirectoryServices.DirectorySearcher                  
Alias                  System.Management.Automation.AliasAttribute                 
AllowEmptyCollection   System.Management.Automation.AllowEmptyCollectionAttribute  
AllowEmptyString       System.Management.Automation.AllowEmptyStringAttribute      
AllowNull              System.Management.Automation.AllowNullAttribute             
array                  System.Array                                                
bigint                 System.Numerics.BigInteger                                  
bool                   System.Boolean                                              
byte                   System.Byte                                                 
char                   System.Char                                                 
ciminstance            Microsoft.Management.Infrastructure.CimInstance             
CmdletBinding          System.Management.Automation.CmdletBindingAttribute         
cultureinfo            System.Globalization.CultureInfo                            
datetime               System.DateTime                                             
decimal                System.Decimal                                              
double                 System.Double                                               
float                  System.Single                                               
guid                   System.Guid                                                 
hashtable              System.Collections.Hashtable                                
int                    System.Int32                                                
int16                  System.Int16                                                
int32                  System.Int32                                                
int64                  System.Int64                                                
ipaddress              System.Net.IPAddress                                        
long                   System.Int64                                                
mailaddress            System.Net.Mail.MailAddress                                 
NullString             System.Management.Automation.Language.NullString            
OutputType             System.Management.Automation.OutputTypeAttribute            
Parameter              System.Management.Automation.ParameterAttribute             
powershell             System.Management.Automation.PowerShell                     
pscredential           System.Management.Automation.PSCredential                   
pscustomobject         System.Management.Automation.PSObject                       
PSDefaultValue         System.Management.Automation.PSDefaultValueAttribute        
pslistmodifier         System.Management.Automation.PSListModifier                 
psmoduleinfo           System.Management.Automation.PSModuleInfo                   
psobject               System.Management.Automation.PSObject                       
psprimitivedictionary  System.Management.Automation.PSPrimitiveDictionary          
PSTypeNameAttribute    System.Management.Automation.PSTypeNameAttribute            
ref                    System.Management.Automation.PSReference                    
regex                  System.Text.RegularExpressions.Regex                        
runspace               System.Management.Automation.Runspaces.Runspace             
runspacefactory        System.Management.Automation.Runspaces.RunspaceFactory      
sbyte                  System.SByte                                                
scriptblock            System.Management.Automation.ScriptBlock                    
securestring           System.Security.SecureString                                
single                 System.Single                                               
string                 System.String                                               
SupportsWildcards      System.Management.Automation.SupportsWildcardsAttribute     
switch                 System.Management.Automation.SwitchParameter                
timespan               System.TimeSpan                                             
type                   System.Type                                                 
uint16                 System.UInt16                                               
uint32                 System.UInt32                                               
uint64                 System.UInt64                                               
uri                    System.Uri                                                  
ValidateCount          System.Management.Automation.ValidateCountAttribute         
ValidateLength         System.Management.Automation.ValidateLengthAttribute        
ValidateNotNull        System.Management.Automation.ValidateNotNullAttribute       
ValidateNotNullOrEmpty System.Management.Automation.ValidateNotNullOrEmptyAttribute
ValidatePattern        System.Management.Automation.ValidatePatternAttribute       
ValidateRange          System.Management.Automation.ValidateRangeAttribute         
ValidateScript         System.Management.Automation.ValidateScriptAttribute        
ValidateSet            System.Management.Automation.ValidateSetAttribute           
version                System.Version                                              
void                   System.Void                                                 
wmi                    System.Management.ManagementObject                          
wmiclass               System.Management.ManagementClass                           
wmisearcher            System.Management.ManagementObjectSearcher                  
xml                    System.Xml.XmlDocument      

Type Acceleratorsに新規Type Acceleratorを追加する

さて、::Getメソッドで取得できたなら? そう、追加もできます。 ここにサンプルがあります。

Power Tips » Adding New Type Accelerators

それでは、さっそく追加してみます。

[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Add('Linq',[System.Linq.Enumerable])

すると…

[linq].FullName

ちゃんと追加されていますね。

System.Linq.Enumerable

追加したType Acceleratorに対して、PowerGUIではIntellisenceが効きませんでした。 が、PowerShell ISEではきっちり効いています。

Projectもあるみたい

最終更新が、Mon Apr 30, 2012 at 5:00 PMですが……こんなプロジェクトもあるみたいですね。

PowerShell Type Accelerators

まとめ

PowerShell V3.0で格段にShell環境が充実するに従い、Type Acceleratorsも拡充されました。 自分で追加する事も簡単になりましたね! 野良巫女様曰く、このような注意事項もあるようなので、ご利用は計画的に?というところですがw

せめて [linq] は欲しいので、 [System.Linq.Enumerable] ぐらいは追加しようかな。

参考サイト

PowerShell (v3) - Type Accelerators Cheat (and a lot of Hot Wind) PowerShell ABC's - T is for Type Literals