ã¿ã¤ãã«ã¯ä¸åº¦ãã£ã¦ã¿ããã£ãã ãã§ããçææ°è¨ã£ã¦ããããªããã
ä»ã®è¨èªåæ§ãPowerShell ã«ãä¸æ¬¡é åãããã¾ãããããªãã¤ã
PowerShell ã¯ãåãæã£ã¦ããã®ã§ Object[]
以å¤ã«ã T[]
(åã®é
å) ãªã©ãããã®ã§ãããä»è¨èªããè¦ãã¨é
åã®æ±ãã«çãããããã«æãã¾ããã¾ã¨ã¾ã£ãè¨äºã«ãããã¨ãªãã£ãã®ã§ãç(æåãç¥ããªããã°ç½ ã«æãã)ã«ã¤ãã¦ã¾ã¨ãã¾ãã
ç®æ¬¡
- ç®æ¬¡
- TL;DR
- ä½ããã¾ãã®
- ç½ ã¨ãªããã¤ã³ã
- æé»ã®åå¤æ
- ç°¡ç¥åãããé å宣è¨
- è¦ç´ ã®é£çµããªãã¬ã¼ã¿ã«ãã£ã¦ã¯é ã
- æ¨æºåºåã§ã®é ååã®è¦ç´ ãåä½ãªå ´åã®èªåçãªåå¤æ
- ãªãã¬ã¼ã¿ã®é åã¨åä½ã§ã®æåã®éã
- PowerShell ãã¯ããã¦ãããããã£
- ã¸ã£ã°é åå ã§ã®é åç¶æã«,ãå¿ è¦
- ã¾ã¨ã
- ãªãã§ãã®è¨äºæ¸ããã®
TL;DR
PowerShell ã®ä¸æé åã¯ã
- 左辺ã«å³è¾ºãåããããããæé»ã®åå¤æã
- @() ã使ãã¨Tã®ä¸æé åãObjectã«åå¤æããã
- += ã¯è¦ç´ ãå¤ããªãã¨é ããªãã®ã§ List[T] ãArrayListã使ã£ã¦ããªã .Add() ã使ãã¾ããã
@(1,2,3) -eq 1
ãªã©ãªãã¬ã¼ã¿ã¼(-eq)ã®é åã«å¯¾ããããã£ã«ã¿ãªã³ã°ããé²æ¢ããã«ã¯ã¨ã¼ãè¨æ³1 -eq @(1,2,3)
ã使ãã¾ããã- PowerShell ãã¯ããã¦ããããã㣠(.Count) ã«ã¯æ³¨æ
- ã¸ã£ã°é
åå
ã§ã®é
åç¶æã«ã¯åé
ã®
,
ãªãã¬ã¼ã¿ã¼ã使ãã¾ããã
ä½ããã¾ãã®
é åã¨é åã®æ¯è¼ããããæã£ããéã£ãï¼ã¨ãããã®ã¯ããããããããªãã§ããããã
é åã¯ç¹ã«ããæ±ããã¼ã¿åã§ããããã®æåãç´æã¨ããããã¨ãå¤ãã¨ããã®è¨èªãªã«ãã¨ãªãã§ãããã
ãã®è¨äºã¯èªèã«ãããããæ確ã«ãããã¨ã§ãPowerShellãæã£ãããã«æ±ããã¨ãç®çã«ãã¦ãã¾ãã
ç½ ã¨ãªããã¤ã³ã
çµé¨ä¸ãPowerShell ã§ä¸æ¬¡é åã§å°ãã£ã¦ããã®ã¯ã©ãã*1ã«å½ã¦ã¯ã¾ã£ã¦ãã¾ããã*2
ãä¸è¨ãããã¨ããã§ããã
ã¯ã¾ããã¤ã³ã | ä¸è¨ | å¶æªåº¦*3 |
---|---|---|
æé»ã®åå¤æ | ä»æ¹ãªã | 3/10 |
ç°¡ç¥åãããé åå®£è¨ | ããæ¨ã¦ä»¥å¤ã¯åã«ç¸ã£ã¦ãã(ãããããã©ã¤ | 4/10 |
è¦ç´ ã®é£çµããªãã¬ã¼ã¿ã«ãã£ã¦ã¯é ã | ä»æ§ãå¤ããã§ããã¡ã½ãã使ã£ã¦ | 1/10 |
æ¨æºåºåã§ã®é ååã®è¦ç´ ãåä½ãªå ´åã®èªåçãªåå¤æ | ç¥ã£ã¦ã¦ãã»ã¼åé¿ã§ããªãããææªãã£ï¼ | 10/10 |
ãªãã¬ã¼ã¿ã®é åã¨åä½ã§ã®æåã®éã | åè¦ã§æå¾ ããåããããªããçã¾ãå¤ãã£ã¦ãã | 8/10 |
PowerShell ãã¯ããã¦ããããã㣠| ãã¯ãçããã¦ãããããã£ä½¿ããããªã | 6/10 |
ã¸ã£ã°é åå ã§ã®é åç¶æã«,ãå¿ è¦ | ç½ ã¨ããè¨ããªã | 9/10 |
ã¯ããã«ãã£ã¦ããã¾ããPowerShell ã¯åãæã£ã¦ããã¨ããã¾ãããåºæ¬ Object
| Object[]
ã«ããããã¾ãããããè¦ãã¦ããã¦ãã ããã*4
é çªã«ããã¾ãã
æé»ã®åå¤æ
ããã¯ä¸æ¬¡é åã«éãã¾ãããããã©ããæ··åããã¦ããã±ã¼ã¹ãå¤ãããã§ãããããããåä»ãªããã§ããã
PowerShell ã¯ãåçåä»ãè¨èªãªã®ã§å®è¡æã«åã決å®ããã¾ãããã®æé»ã®åå¤æã«ã¯ååãããã®ã§ããããããç¥ããªãã¨åã§ä½ãããã®æä½ããããã¨æå¾ ããæã«ãæå¾ ã¨ç°ãªãåã«ãªã£ã¦ãããããã¨ãã
.GetType()
ã¡ã½ããã§ã®åã§ã®å¤å¥ãäºæããªãçµæã«ãªã- function (é¢æ°) ã§åæå®ã§åãããã¨ãã¦ãæå³ããåã§ãªããã®ã渡ã£ã¦ãã¦ã¨ã©ã¼ã«ãªã
æé»ã®åå¤æã®ã«ã¼ã«
ããã ãè¦ãã¦ããã¦ãã ããã
å®è¡æã«ãªãã¬ã¼ã¿ã®å·¦è¾º(å·¦ãªãã©ã³ã)ã¨å³è¾º(å³ãªãã©ã³ã)ã®åãéãå ´åãã左辺ã®åã«å³è¾ºã®åãå¤åããã¾ãã
å¤æ°ã®åã¯ã.GetType()
ã¡ã½ããã§èª¿ã¹ãããã®ã§ä½¿ã£ã¦ããã¾ãããã
ããã¤ãä¾ãè¦ã¦ã¿ã¾ãã
ã·ã³ãã«ãªåå¤æä¾
ã¾ãã¯ç°¡åãªä¾ã§ãã
ä¸çªä¸ã®$hoge
ã®åãèãã¾ãã
ãã®ä¾ã§ã¯ã左辺㫠System.String åãå³è¾ºã« System.Int32 å ã®å¤æ°ãAppend ãã¦ãã¾ãããã®ãããã«ã¼ã«éã $hoge
㯠System.Stringå ã¨ãªãã¾ãã
å¤æ°$b
ã System.Int32 å (1) ãã System.String å ("1")ã«æé»ã«åå¤æãããã®ã§ããã
æé»ã®åå¤æã®å¤±æä¾
å ã»ã©ã®å·¦è¾ºã¨å³è¾ºãéã«ãã¦ã¿ã¾ããããããã¨æé»ã®åå¤æã«å¤±æãã¾ãã
å ã»ã©ã¨åæ§ã«ã左辺㮠System.Int32 å ã« System.String å ãæé»ã«åå¤æãããã¨ããã®ã§ããã
int -> string ã¯æé»ã«åå¤æå¯è½ã§ãããstring -> int ã¯ã§ããªãã®ã§ã¨ã©ã¼ã¨ãªã£ã訳ã§ãã
ä¸æ¬¡é åã®åå¤æ
ãã¦ãSystem.Int32 åãé
åã«å
¥ãããã¨æã£ã¦ã@()
ã§æ¬ã£ããã©ããªåã«ãªãã§ããããï¼
é
åã®ä¸èº«ã¯ System.Int32 åã¾ã¾ã§ãããé
åå
¨ä½ã¯ System.Int32[]
ã§ã¯ãªããSystem.Object[]
ã¨ãªãã¾ããããããæå¾
ããå㯠System.Int32[]
ã ã¨æãã®ã§éåæãããã®ã§ã¯ãªãã§ããããã
å®éã®ã¨ããã@()
ã¯ã[object[]]()
ã¨å義ã§ããç¥ã£ã¦ããã°ããããªãã®ããã§ãããã·ã©ãã¤ã¨å¥½ã¾ãããªãã¨æããã§ãããã
åããããµãã«ãã£ã¦äºæ
ã£ã¦ãåããã£ã¡ããããã¨ãããã¨ã¦ã使ãã«ãããç¹ã«ä¸æ¬¡é
åã¯ãªãã§ã Object[]
ã«ããããã®ã§æ±ãã«ãããç®ã«ã¤ãã¦ãã¾ãã¾ãã
åé¿ç
左辺ã«åããã¦å³è¾ºã®åã¯æé»ã«å¤æãããããããè¦ãã¦ããã¾ããããã¤ã³ããªã»ã³ã¹ã§ããããã®ææãã¦ã»ããã§ãããã
ScriptAnalyzer ã§ã®éçãªè§£æããã¡ããã§ããã
PowerShell Tools for Visual Studio ã¯... ã»ããç¾ç¶æã¤æãªããªã®ã§PRæãã¦ãã ãããç¾å¨ã¯ Microsoft ãéçºã«åå ãã¦å®è£ é度ãä¸ãã£ã¦ã¾ãã
ç°¡ç¥åãããé å宣è¨
次㯠é å宣è¨ã§ãã
PowerShell ã§é åã宣è¨ããæ¹æ³ã¯ããã¤ãããã¾ãã
ããããç°¡ç¥ãªæ¹æ³
ä¸çªå¤ã表ç¾ãããæ¹æ³ã¯å
ã»ã©ã®ã@()
ã§ãããã
å
ã»ã©èª¬æããéããSystem.Object[]
ã«ãªãã®ã§æ³¨æã§ãã
æ示çãªå®£è¨
ä»è¨èªã§ã¯ãC# ã§ãã var
ã®ãããªåæ¨è«ã使ã£ã¦ãã¦ãã¤ã³ããªã»ã³ã¹ãããã®ã§å°ãã¾ãããããããPoweShell ã PowerShell ISE ã®ã¤ã³ããªã»ã³ã¹ã¯åãµãã¼ãã貧弱ãªã®ã§ããããã¨åãææ¡ã§ããªããªãã¾ãããã£ãã®ä¾ã System.Object[]
ã«ãªãã®ã¯ãã®ä¾ã§ãã
ããããã¨ãã¯ãåãæ示ãã¦ããã¨å®å¿ã§ããã§ãããã(ãã®ãã¨èª¬æããè¦ç´ ãåæ°ã®å ´åãé¤ãã¦ã¯)
åæ°ãä¸æ¬¡é åã«ãã
ãããã¯ã¾ãã©ããã§ããããåé¿çã§ãã
1
㯠System.Int32 åã§ããã@(1)
㯠System.Object[]åã§ãããããç°¡ç¥ãªæ¹æ³ã§ãããã@()
ã§åºåãå²ãã¨é
åæ±ãã«ãªãã¾ãã
ããããã®å¾ã触ããã®ã§ããçµæ§ããã¤ãªãã§ãããå¹æã®ããããæ¹ã§ããPoweShell ã®è¦ããããªããã©ä¾¿å©ãªããæ¹ é »åºããã5ã«å ¥ããããªãããã
è¦ç´ ã®é£çµããªãã¬ã¼ã¿ã«ãã£ã¦ã¯é ã
é åã«è¦ç´ ã追å ããã¨ãã«ã©ããã¦ãã¾ããï¼
ããããã¦ã+=
ã使ã£ã¦ã¾ãããï¼è¦ç´ ãå¢ããã¨ãã«æ»ã«ã¾ãããããã
åé¿ç
ã¸ã§ããªã¯ã¹ã® List[T] ã使ã£ã¦ããªã .Add
ã¡ã½ãããã¤ãã£ã¦ãã ãããå»æ£ç©ã®ArrayListã§ãåæ§ã§ãã
詳ããã¯ä»¥åã¾ã¨ããã®ã§ã©ããã
æ¨æºåºåã§ã®é ååã®è¦ç´ ãåä½ãªå ´åã®èªåçãªåå¤æ
ç§ãæã PowerShell ã®é åã®ç½ ã§æç¶ãããã¯ããã§ããããã¤ãªãã¤ãã¤ã
ãã®æåå°é·ä»¥å¤ã®ãªãã¦å¼ã¹ã°ãããã§ããããä¸æ¬¡é åã¯çµæãåæ°ã®å ´åãé åã§ã¯ãªããªã£ã¦ãã¾ãã¾ãã*5
åé¿ç
ããã§ãå
ã»ã©ã®åæ°ãé
åã«ãã ã¨ãã @()
ãç¾ãã¾ãã*6
ã¯ãã
ãªãã¬ã¼ã¿ã®é åã¨åä½ã§ã®æåã®éã
ããã§ãããªãã¬ã¼ã¿ã¯ç¹ã«-eq
| -ceq
æ¼ç®åãæãã¾ãã
ãã®ãªãã¬ã¼ã¿ãæ¯è¼å¤å®ãã¦ããã¨æãã¾ãããï¼
é åã«ãã£ã¦ã¿ã¾ãããã
é å 1,2,3,4,5,6,7,8,9,10 ã¯ã1ã¨ã¯éãã®ã§æå¾ ããåä½ã¯ false ãè¿ã£ã¦ãããã¨ã§ãã....?
ç¥ããªã人ã«ã¯ã¾ããã® 1 ãè¿ã£ã¦ãã¾ãã
ããã¯ã-eq
ãªãã¬ã¼ã¿ãé
åã«å¯¾ãã¦ã¯ãã£ã«ã¿ãªãã¬ã¼ã¿ã ããã§ãã
ãã®ããã左辺㮠1..10 ã®ä¸ã« 1 ãå«ã¾ãã¦ããã®ã§ã該å½ãã 1ãã¨ãã ããã¾ããã
ãããifæã®ä¸ã§ãã£ã¦ããã¨ã bool ã«å¯¾ã㦠1 ã¯æé»ã« true ã«å¤æãããã®ã§ã¾ããã®éã«çµæã«ãªãã¾ãã
ã§ã¯ãå³è¾ºãé åã®å·¦è¾ºã«å«ã¾ããªããã®ã ã£ãå ´åã¯ï¼
çµæã¯ç©ºã§ããããããã®çµæãã¾ãåä»ã§ãã
ãªãã¨ãå¤å®ãããã¦ãããã¦ã
ç´°ãã解説ã¯ã¯ããã¡ã¿ã«ã»ã³ã»ã¼ãã
null ã«é¢ãã¦ã¯ãä¸è¨ããã°ã ãã§ã¯æ°ã¥ãã«ãããã¿ã¼ã³ãããã¾ããä¾ãã°ãããçµæã«æ°ã¥ãã¾ãããï¼
åé¿ç
ååã«ã¼ã«ãæãã ãã¦ãã ããã左辺ã«æé»ã®åå¤æãããã®ã§ããããã¦é
åã«å¯¾ãã¦ã§ãªããã°ã-eq
ã¯æ¯è¼å¤å®ã«ä½¿ãã¾ãã
å¤å®ãããå¤ã左辺ã«æã£ã¦ããã®ãåé¿çã«ãªãã¾ãã
null ã左辺ã«æã£ã¦ãã¾ãããã
PowerShell ãã¯ããã¦ãããããã£
PowerShell ã¯ã.NET æ¨æºã®åã«ç¬èªã®ããããã£ãã¡ã½ãããçããã¦ãã¾ãã*7
CodePropertyãScriptPropertyãNotePropertyãAliasProperty ãªã©ã¨ãã£ãã.NET æ¨æºã§ã¯ãªã PowerShell ãçããã¦ããããããã£ã§ãã
TypeName: System.Int32 Name MemberType Definition ---- ---------- ---------- pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=ne... psadapted MemberSet psadapted {CompareTo, Equals, GetHashCode, ToString, GetTypeCode, GetType, ToBoolean, ToChar, ToS... psbase MemberSet psbase {CompareTo, Equals, GetHashCode, ToString, GetTypeCode, GetType, ToBoolean, ToChar, ToSByt... psextended MemberSet psextended {} psobject MemberSet psobject {BaseObject, Members, Properties, Methods, ImmediateBaseObject, TypeNames, get_BaseObjec... CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo(System.Ob... Equals Method bool Equals(System.Object obj), bool Equals(int obj), bool IEquatable[int].Equals(int other) GetHashCode Method int GetHashCode() GetType Method type GetType() GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode() ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider) ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider) ToChar Method char IConvertible.ToChar(System.IFormatProvider provider) ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider) ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider) ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider) ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider) ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider) ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider) ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider) ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider) ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provide... ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider) ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider) ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider) ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ç¹ã«Get-Process
ãªã©ã¯ããããããã§ãããã試ãã¦ã¿ã¦ãã ããã
ç´°ããç¥ããã人ã¸
id:aetos382 ããã®ç¥ã¾ã¨ããè¦ãã°ãªãã¨ãªãç解ãæ·±ã¾ãã§ãããã
ç½ ã«ãªããããä¾
PowerShell ã§ãé
åã®é·ããåãã¨ãã«ãè¯ãç¨ããããã®ã è¬ã®.Count
ããããã£ã§ãã
ã§ããããLength
ããããã£ã® AliasProperty
ã¨ã㦠PowerShell ãé
åã«ã®ã¿è¨å®ãã¦ãããã§ããã
ãã®ãããé åãããªãåã«ã¯åå¨ãã¾ããã
ããã§ãæ¨æºåºåã§ã®é
ååã®è¦ç´ ãåä½ãªå ´åã®èªåçãªåå¤æ ãæãåºãã¦ãã ããããããçµæã @()
ã§å²ãã§é
åã«å¼·å¶å¤æããè¬ææ³ã使ããªãã¨ã.Count ãåããªãï¼ã¨ããç¶æ³ã«ãªã£ãããããã§ããã*8
åé¿ç
ç§ãã¾ã @()
好ããããªãã®ã§ãçµæãå°ãªãã¨ããã£ã¦ãããªã Measure-Object
ã§æ°ãããã¨ãå¤ãã§ãã
.Length
ããããã£ã¢ã¯ã»ã¹ã¨æ¯ã¹ã¦ã³ã¹ããé«ãã®ã§å«ãªãã§ãããæ°ãå°ãªããã°èª¤å·®ãªã®ã§ã
ãããåããã£ããé åã確èªãã¦ãLengthããããã£ã使ãã¨ããã§ãããã
ã¸ã£ã°é åå ã§ã®é åç¶æã«,ãå¿ è¦
ã¸ã£ã°é
åã¯ããã®é
åè¦ç´ ãé
åã§ããé
åã§ããã¤ã¾ã@(@(1,2,3),@(4,5,6))
ãã®ãããªãã®ã§ãããã®ä¾ã®ã¸ã£ã°é
åè¦ç´ ã¸ã®ã¤ã³ããã¯ã¹ã¢ã¯ã»ã¹ã¯ã¤ã¡ã¼ã¸éãã«ã§ãã¾ãã
@(@()) 㯠@() ã¨ããã¾ããã®ç½
ããããPowerShell ã¯ã@(@(<é
å>))
ã¯@(<é
å>)
ã¨ã¿ãªãã¾ããã¤ã¾ããããªã£ã¡ããã¾ãã
解決ç
ããã§å©ç¨ããã®ããåé
é
åæ¼ç®åã®,
ã§ãã,
ã対象é
åã®åã«ç½®ããã¨ã§ã¸ã£ã°é
åã§ãé
åãç¶æãã¾ãã
åä»ã§ããï¼
ã¾ã¨ã
ã©ããç¥ã£ã¦ããã°åé¿ã¯ã§ãã¾ããããããåé¡ã§ã¯ãªãï¼ããã§ããã
ã¾ãæ°è»½ã«2,3 è¡æ¸ããããã·ã§ã«ä¸ã§Cmdlet ã1ã¤å®è¡ããã ãã®ã·ã¼ã³ãå¤ãã¨æ¥½ãªãã§ãããããã*9
ãªãã§ãã®è¨äºæ¸ããã®
ç½ ãå¤ãã¨ãã声ããããã®ã§æ¸ãã¨ãã天ã®å£°ãèããã¾ããã
ãããããPowerShellã®ä¸æ¬¡å
é
åã«ã¤ãã¦ã²ã¨ãã¨è¨ã£ã¦ããããã¨ããè¨äºãããããã¦å¿
è¦ãªã®ã§ã¯ãªãã ãããã
— çç°å£å¤§ä» (@mutaguchi) September 3, 2015
*1:ãããã¯è¤æ°
*2:ä»ã«ããã£ããæãã¦ãã ãã
*3:å人ã®ææ³ã§ã
*4:ãªã¼ã«ãåããç¶æ
*5:ä½ããã£ã¦ããã
*6:æ¶ãããªã
*7:ã²ã£
*8:PowreShell 3.0 以éã¯åä½ã§ã1ãåãã¾ãããSet-StrictMode -Latest ããã¨ã¨ã©ã¼ã«ãªãæå³ããªãæåã¨ãªãå¯è½æ§ãããã¾ã
*9:ã ã10000è¡ã¨ãæ¸ãã¨åãå³å¯ã«ç¸ããªãã¨æ»ã¬