èªã¿ããããããŒã¹ãããŒã¯ã®æ°ãããããã£ã®é åºãç¶æããããã®èŠä»¶ã«å ããŠãããããã¹ãŠã®ã¡ãœããã¯åºæ¬çã«åãããšãè¡ããŸãã ããã€ãã®åŸ®åŠãªéãïŒãã¯ããã¯1ãããã·ã¥ããŒãã«ã éåžžãæéã®ææ³2ã¯é ããªããææ³3ã¯å€§å¹ ã«é ããªããŸãã
è¯ãã¬ãã¥ãŒã§ã learn-powershell.net / 2010/09/19 / custom-powershell-objects-and-performanceã¯ãMVP Boe Proxãäœæãããªããžã§ã¯ãã®äœæé床ã枬å®ããŸããã 3ã€ã®æ¹æ³ã®é床ãæ¯èŒãããšãAdd-Memberæäœã¯10åé ããªããŸãã ãããã£ãŠãããã¯ãã¯ããã¯1ã®ãã©ã¹ã§ããBoeã®ããã«ãã¯ããã¯4ããã¹ãããããšã¯ãããŸãããããã¹ã4ã¯ãã¯ããã¯1ãã10ïŒ é«éã§ããããšã瀺ãããŠããŸãã
翻蚳èªäœïŒ
21.2ã«ã¹ã¿ã ãªããžã§ã¯ããäœæããããã®æ§æ
PowerShellã§äœããè¡ãæ¹æ³ã¯åžžã«ããã€ããããšããèšãããŸããããããã¯ã«ã¹ã¿ã ãªããžã§ã¯ãã«ãåœãŠã¯ãŸããŸãã ããªãã人çã§ãããã«åºäŒãå¯èœæ§ãé«ãã®ã§ãç§ãã¡ã¯ããªãã«ãã¹ãŠã®äž»ãªæ¹æ³ã玹ä»ããŸãã
第21.1ç« ãã¯ããã¯çªå·1.ããã·ã¥ããŒãã«ã䜿çšããŠã«ã¹ã¿ã ãªããžã§ã¯ããäœæããŸãã
ç¬èªã®ãªããžã§ã¯ããäœæããããããŸããŸãªãªããžã§ã¯ãã®æ å ±ãçµã¿åãããŠããã«åºåãããããå¿ èŠãããå Žåã«é垞䜿çšããããªãã¯ãå§ããŸãããã ãã®ãã¹ãå ¬åŒãŸãã¯æšå¥šãšåŒã³ãŸãã ã³ãŒããç°¡åã«èšè¿°ããèªã¿åãã容æã«ãªããæçµçã«ã¯äœæ¥ãé«éåã§ããã®ã§ãæ£ç¢ºã«äœ¿çšããŸãã
ãã®ã¡ãœããã以äžã®ãªã¹ã21.2ã«ç€ºããŸãã
# , $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 # . $props = @{OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth} # $obj = New-Object âTypeName PSObject âProperty $props Write-Output $obj
ãã®ã³ãŒããå®è¡ãããšã次ã®ãããªçµæãåŸãããŸãã
Manufacturer : Microsoft Corporation OSVersion : 6.3.9600 OSArchitecture : 64-bit BIOSSerial : 036685734653 ComputerName : RSSURFACEPRO2 Model : Surface Pro 2 ProcArchitecture : 64
ãªããªã åºåã«ã¯ã4ã€ä»¥äžã®ããããã£ãæã€ãªããžã§ã¯ãããããŸãã æºãããŠããã ããŸãã
Write-Output $obj | ft
ããŒãã«ãååŸããŸãã å®éã«ã¯ã4ã€ã®ãªããžã§ã¯ãã®æ å ±ãçµã¿åãããŠ1ã€ã®ãªããžã§ã¯ããäœæããããšã«æ³šæããŠãã ããã ãããè¡ãã«ã¯ãç®çã®ããããã£åãç»é²ããããã·ã¥ããŒãã«ãäœæããŸããããããã®å€ã¯ä»ã®ãªããžã§ã¯ãã®ããããã£å€ã§ãã ããã¯ãããã·ã¥ããŒãã«ã§æ¬¡ã®ããšã瀺ãããã®ã§ãã
Manufacturer=$cs.manufacturer
ããã·ã¥ããŒãã«ãšã³ããªã1è¡ã«é 眮ããå Žåã¯ãåããããã£ãã»ãã³ãã³ã§åºåãå¿ èŠããããŸãã åããããã£ãå¥ã ã®è¡ã«é 眮ããå Žåãã»ãã³ãã³ã¯å¿ èŠãããŸãããã³ãŒããèªãã§ç·šéããæ¹ãã¯ããã«ç°¡åã§ãã
@èšå·ãåã«ä»ããŠããæ¬åŒ§ã¯ãããã·ã¥ããŒãã«ãå§ãŸãããšã瀺ããŸãã ãªããªã ããŒãã«ã¯$ propså€æ°ã«ãããæ°ãããªããžã§ã¯ãã®-propertyãã©ã¡ãŒã¿ãŒã§ç°¡åã«æž¡ãããšãã§ããŸãã PSObject㯠ããã®ç®çã®ããã«ç¹å¥ã«æäŸãããŠããŸãã
ãã®ã¢ãããŒãã®å©ç¹ã¯ããã®å Žã§ããã·ã¥ããŒãã«ãç°¡åã«æ§ç¯ããããããå€ãã®ãŠãŒã¶ãŒãªããžã§ã¯ããäœæã§ããããšã§ãã åºåãªããžã§ã¯ãã§ã¯ãããããã£ãããŒãã«ã§å®çŸ©ãããé åºãšåãé åºã§ã¯ãªãããšã«æ°ä»ããããããŸããã å¯èœãªè§£æ±ºçã®1ã€ã¯ãã«ã¹ã¿ã ãªããžã§ã¯ãïŒã©ã®é åºã§è¡šç€ºããããªã©ã説æããç¹å¥ãªXMLãã¡ã€ã«ïŒã®ãã©ãŒããããäœæããããšã§ãããŸãã¯ãpowershell 3以éã䜿çšããå Žåã¯ã orderedããããã£ã䜿çšã§ããŸãã
$props = [ordered]@{ OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth}
ä»ã®ãã¹ãŠã¯åãã§ãã ããã§ããªããžã§ã¯ãã®ããããã£ãæžã蟌ãŸããé åºã§è¡šç€ºãããŸãã Get-Memberã« $ objãæž¡ããšããããPS-CustomObjectã§ããããšãããããŸãã
泚 PowerShellã¯ãæ¢å®ã§ã¯ããã·ã¥ããŒãã«å ã®ã¢ã€ãã ã®é åºã远跡ããŸããã ãã®ãããæçµçãªçµè«ãèŠããšããã®ããããã£ã¯äœæããé åºã§ã¯ãããŸããã PowerShell 3以éã§ã¯ã [ordered]å±æ§ã䜿çšããŠãããä¿®æ£ã§ããŸãã ããã«ãããé åºä»ããããèŸæžïŒå¥ã®ååã¯ããŒãã«ã®ããã·ã¥ïŒãäœæããããã®äžã®èŠçŽ ã®é åºãç¶æãããŸãã
21.2.2ãã¯ããã¯2. Select-Objectã®äœ¿çš
ãã®ã¡ãœããã¯ã PowerShell v1ã§äººæ°ããããä»ã§ã人ã ã䜿çšããŠããŸãã èªãã®ããã£ãšé£ããã®ã§ãç§ãã¡ã¯ããã奜ãã§ã¯ãããŸããã 次ã®ãªã¹ãã¯ã空ã®ãªããžã§ã¯ããäœæãããããã®ããããã£ã®å€ãæžã蟌ãææ³ã瀺ããŠããŸã
ãªã¹ã21.3ã Select-Objectã䜿çšãããªããžã§ã¯ãã®äœæ
# $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 # Select-Object $obj = 1 | Select-Object ComputerName,OSVersion,OSArchitecture, ProcArchitecture,Model,Manufacturer,BIOSSerial # $obj.ComputerName = $os.CSName $obj.OSVersion = $os.version $obj.OSArchitecture = $os.osarchitecture $obj.ProcArchitecture = $proc.addresswidth $obj.BIOSSerial = $bios.serialnumber $obj.Model = $cs.model $obj.Manufacturer = $cs.manufacturer Write-Output $obj
ãªã¹ã21.3ã§ã¯$ obj = 1ãäœæãããŠããããšã«æ³šæããŠãã ãã;åºæ¬çã«ãå€1ã¯æ±ºããŠäœ¿çšãããŸããã
ãã³ã空ã®æååãåæååãšããŠäœ¿çšãããšãå€ãã®äŸã衚瀺ãããŸãã $ obj = "" | éžæ.... ããã¯ã $ objããªããžã§ã¯ããšããŠå®çŸ©ãããã€ãã©ã€ã³ã«äœããããããããŠSelect-Objectã«ç§»åãããã¹ãŠã®äœæ¥ãè¡ãããã®åãªãæ¹æ³ã§ãã
ãã®ã¢ãããŒãã«ã¯æ¬ ç¹ããããŸãã Get-Memberã«$ objãæž¡ããçµæã確èªããŸãã
PS C:\> $obj | Get-Member TypeName: Selected.System.Int32 Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() BIOSSerial NoteProperty System.String BIOSSerial= ComputerName NoteProperty System.String ComputerName= WIN-KNBA0R0TM23 Manufacturer NoteProperty System.String Manufacturer= VMware, Inc. Model NoteProperty System.String Model= VMware Virtual Platform OSArchitecture NoteProperty System.String OSArchitecture= 64-bit OSVersion NoteProperty System.String OSVersion= 6.1.7601 ProcArchitecture NoteProperty System.UInt16 ProcArchitecture=64
ããããã£ã¯çšæãããŠããŸãããTypeNameã¯åé¡ãåŒãèµ·ãããããæ··ä¹±ãæãããããå¯èœæ§ããããŸãããã¹ãŠã¯ããã®ãªããžã§ã¯ãã§äœãããããšããŠãããã«ãã£ãŠç°ãªããŸãã ãã®ææ³ãé¿ããããšããå§ãããŸãã
翻蚳æ¿å ¥
ããã§ç§ã¯ãããæå³ããŸãïŒ
Get-Memberã«ãªã¹ã21.2ã®äŸã瀺ããŸã-ããŒãã«ã§ããã·ã¥ãäœæããŸãã
次ã«ãGMã«ãªã¹ã21.3ã®äŸã瀺ããŸã-Select-Objectã䜿çšããŠäœæããŸã
21.2.2ãã¯ããã¯3. Add-Memberã®äœ¿çš
ãã®æ¹æ³ã¯æ£åŒãšèŠãªãããŸãã ç§ãã¡ã¯åœŒãæ£åŒãªã¢ãããŒãã§äººçã§äœãèµ·ãã£ãŠããã®ãã瀺ããŠãããšä¿¡ããŠããŸãã ããã¯æãèšç®ã³ã¹ããé«ããæãé ããããå®éã«äœ¿çšããŠãã人ãèŠã€ããããšã¯ã»ãšãã©ãããŸããã ããã¯ãæãäžè¬çãªPowerShell v1ã¢ãããŒãã§ãã 2ã€ã®ããªãšãŒã·ã§ã³ããããæåã®ãªã¹ãã¯ã³ãŒãã§ãã
ãªã¹ã21.4 Add-Memberã䜿çšãããªããžã§ã¯ãã®äœæ
# $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 # $obj = New-Object âTypeName PSObject # $obj | Add-Member NoteProperty ComputerName $os.CSName $obj | Add-Member NoteProperty OSVersion $os.version $obj | Add-Member NoteProperty OSArchitecture $os.osarchitecture $obj | Add-Member NoteProperty ProcArchitecture $proc.addresswidth $obj | Add-Member NoteProperty BIOSSerial $bios.serialnumber $obj | Add-Member NoteProperty Model $cs.model $obj | Add-Member NoteProperty Manufacturer $cs.manufacturer Write-Output $obj
PSObjectãäœæããäžåºŠã«1ã€ã®ããããã£ãè¿œå ããŸãã éçãªå€ã®ã¿ãå«ãNotePropertyãè¿œå ãããã³ã«ãã¡ãœãããåŒã³åºãå¿ èŠããããŸãã ã³ãŒããåæžããããã«ãAdd-Memberäœçœ®ãã©ã¡ãŒã¿ãŒã䜿çšããŸããã å®å šãªæ§æã䜿çšãããšãåAdd-Memberã¹ããŒãã¡ã³ãã¯æ¬¡ã®ããã«ãªããŸãã
Add-Member -MemberType NoteProperty -Name ComputerName -Value $os.CSName
å€ãã®ã³ãŒããå€æããããšãããããŸãã
ãã®ã¡ãœããã®ããªãšãŒã·ã§ã³ã¯ãAdd-Memberã³ãã³ãã®-PassThru ïŒãªã¹ã21.5ã§ã¯-Passãšç¥ãããŸã ïŒ ãã©ã¡ãŒã¿ãŒã䜿çšããããšã§ã ã ãã®ãã©ã¡ãŒã¿ãŒã¯ãå€æŽããããªããžã§ã¯ãããã€ãã©ã€ã³ã«æ»ãã®ã§ã次ã®ã³ãã³ããªã©ã«æž¡ãããšãã§ããŸãã
äŸã瀺ããŸã
ãªã¹ã21.4 -PassThruãã©ã¡ãŒã¿ãŒãæå®ããAdd-Memberã䜿çšãããªããžã§ã¯ãã®äœæ
# $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 # $obj = New-Object âTypeName PSObject # $obj | Add-Member NoteProperty ComputerName $os.CSName âpass | Add-Member NoteProperty OSVersion $os.version âpass | Add-Member NoteProperty OSArchitecture $os.osarchitecture âPass | Add-Member NoteProperty ProcArchitecture $proc.addresswidth âpass | Add-Member NoteProperty BIOSSerial $bios.serialnumber âpass | Add-Member NoteProperty Model $cs.model âpass | Add-Member NoteProperty Manufacturer $cs.manufacturer Write-Output $obj
ããªãã¯äººçã§ãã®ã¢ãããŒãã«åºãããããšãã§ããŸãã å®éãæŸåã®èŠ³ç¹ãããããã¯äœãèµ·ãã£ãŠããã®ããããæ確ã§ãããããããã¯éèŠãªãã¯ããã¯ã§ãã ãã®ã¢ãããŒãã¯æ§æã©ãã«ã«åŸã£ãŠããªãããã段éçã«ç°¡åã«å解ã§ããŸãã
翻蚳è æ¿å ¥
ãããã°ãç°¡åã«ãªããŸã
21.2.4ãã¯ããã¯4.å宣èšã®äœ¿çš
ããã¯ææ³1ã®ããªãšãŒã·ã§ã³ã®1ã€ã§ãããPowerShell v3ããã³v4ã§ã®ã¿æ©èœããŸãããã®æ¹æ³ã¯ããã³ã³ãã¯ãã§ãã ããŒãã«ã®åãããã·ã¥ããå§ããŸãã
# $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 $obj = [pscustomobject]@{ OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth } Write-Output $obj
$ propså€æ°ã®å ¥åãç¶ããããšãã§ããŸãããã®ãã¯ããã¯ã®ç§trickã¯ããªããžã§ã¯ãã®ããããã£ã®é åºã[ordered]ãã£ã¬ã¯ãã£ãã䜿çšãããã®ããã«ä¿æãããããšã§ã
ãã³ãã 䜿çšããã¿ã€ãã¯PSCustomObjectã§ãã .NETã®çšèªã§ã¯ããã©ã¡ãŒã¿ãŒãªãã§PSObjectã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããããããã®ã¿ã€ãã䜿çšããå¿ èŠããããŸãã PSCustomObjectã®ä»£ããã«PSObjectãæå®ããŠã³ãŒããçãããããšããªãã§ãã ãããç®çã®çµæãåŸãããŸããã
以åã®ã¡ãœããã§ã¯ãããããã£ãè¿œå ããé åºãšã¯ç°ãªãé åºã§ããããã£ãè¿œå ãããããšã«æ°ã¥ãããããããŸããã ããšãã°ããã¯ããã¯1ã§ã¯ãæåã«Computer_Nameãè¿œå ããŸããã§ãããããªã¹ãã®æåã«è¡šç€ºãããŸãã å¿é ããªãã§ãã ãããã»ãšãã©ã®å ŽåãPowerShellã¯ä»»æã®é åºã§ããããã£ãæäœããŸãã ãã¯ããã¯4ã¯ã䜿çšããå¿ èŠãããå Žåãããããã£ã®é åºãä¿æããŸãã
21.2.5ãã¯ããã¯5.æ°ããã¯ã©ã¹ãäœæãã
ããŸã䜿çšãããªããã¯ããã¯ã ã³ã³ãã¢åŠçã«ããã€ãã®å©ç¹ããããŸãã é«åºŠãªãã¯ããã¯ãšããŠè©äŸ¡ã§ããŸããããã¹ãŠã®ã¹ãã«ã.NETãæãäžããããšã¯éããŸããã ããããæ©äŒã¯ãªãã·ã§ã³ãšããŠå©çšå¯èœã§ãã
ãªã¹ã21.7ã¯ã©ã¹ã䜿çšãããªããžã§ã¯ãã®äœæ
$source=@" public class MyObject { public string ComputerName {get; set;} public string Model {get; set;} public string Manufacturer {get; set;} public string BIOSSerial {get; set;} public string OSArchitecture {get; set;} public string OSVersion {get; set;} public string ProcArchitecture {get; set;} } "@ Add-Type -TypeDefinition $source -Language CSharpversion3 # $os = Get-WmiObject âClass Win32_OperatingSystem âcomp localhost $cs = Get-WmiObject âClass Win32_ComputerSystem âcomp localhost $bios = Get-WmiObject âClass Win32_BIOS âcomp localhost $proc = Get-WmiObject âClass Win32_Processor âcomp localhost | Select âFirst 1 $props = @{OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth} $obj = New-Object âTypeName MyObject âProperty $props Write-Output $obj
ãªã¹ã21.7ã¯ãCïŒã¯ã©ã¹èšè¿°ã³ãŒããå«ãã¬ã³ãŒããäœæããããšããå§ãŸããŸãã ã¯ã©ã¹ã«ã¯MyObjãšããååããããå€ãã®ã¯ã©ã¹ããããã£ã®èª¬æãäœæããŸãã ãã®äŸã§ã¯ãããããã£ã¯æååãšããŠæå®ãããŠããŸãããã¿ã€ãã¯æ··åšã§ããŸãã ãŸããä»åŸããããã£èšå®ãèšè¿°ããäºå®ã¯ãããŸããããã¯ã©ã¹å®çŸ©ã§ã¯GETããã³SETãèšè¿°ããå¿ èŠããããŸããããããªããšãPowerShellãäŸå€ãã¹ããŒããŸãã
Add-Typeã¯ã¯ã©ã¹ã®ã³ã³ãã€ã«ã«äœ¿çšããããã®åŸPSObjectã®ä»£ããã«äœ¿çšã§ããŸãã ãªããžã§ã¯ãã®ããããã£ãèšå®ããã«ã¯ããããšãªã¹ã21.6ã«ç€ºãææ³ã䜿çšã§ããŸãã
$obj = [MyObject]@{OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth }
get-memberããã¹ãããŸããã
PS C:\> $obj | get-member TypeName: MyObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() BIOSSerial Property string BIOSSerial {get;set;} ComputerName Property string ComputerName {get;set;} Manufacturer Property string Manufacturer {get;set;} Model Property string Model {get;set;} OSArchitecture Property string OSArchitecture {get;set;} OSVersion Property string OSVersion {get;set;} ProcArchitecture Property string ProcArchitecture {get;set;}
ããã¯ããã°ãã ãã§ãªããå©ç¹ããããŸãã ããããã£ãæååãšããŠå®£èšããããã«æ°å€ãæžã蟌ãããšãããšããšã©ãŒã¡ãã»ãŒãžãçæãããŸãã
21.2.6éãã¯äœã§ããïŒ
èªã¿ããããããŒã¹ãããŒã¯ã®æ°ãããããã£ã®é åºãç¶æããããã®èŠä»¶ã«å ããŠããããã®ã¡ãœããã¯ãã¹ãŠæ¬è³ªçã«åãããšãè¡ããŸãã
ããã€ãã®åŸ®åŠãªéãïŒãã¯ããã¯1ãããã·ã¥ããŒãã«ã éåžžãæéã§ãç¹ã«è€æ°ã®ãªããžã§ã¯ããæäœããå Žåãææ³2ã¯å°ãé ããææ³3ã¯ã¯ããã«é ããªããŸãã
è¯ãã¬ãã¥ãŒã§ã learn-powershell.net / 2010/09/19 / custom-powershell-objects-and-performanceã¯ãMVP Boe Proxãäœæãããªããžã§ã¯ãã®äœæé床ã枬å®ããŸããã 3ã€ã®æ¹æ³ã®é床ãæ¯èŒãããšãAdd-Memberæäœã¯10åé ããªããŸãã ãããã£ãŠãããã¯ãã¯ããã¯1ã®ãã©ã¹ã§ããBoeã®ããã«ãã¯ããã¯4ããã¹ãããããšã¯ãããŸãããããã¹ã4ã¯ãã¯ããã¯1ãã10ïŒ é«éã§ããããšã瀺ãããŠããŸãã