Hash Table构建

 

 1 get-item e:\test\* |format-table @{name="aa";expression={$_.name.tostring().split(".")[1]}}

 2 

 3  

 4 

 5 get-childitem e:\test\* |select name,@{name="changdu";expression={$_.name.length}},lastwritetime |format-table -autosize

 6 

 7  

 8 

 9 Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName . | 

10 Format-Table deviceid,@{Label="free(GB)"; Expression={$_.freespace/1024/1024/1024}},@{Label="size(GB)"; Expression={$_.size/1024/1024/1024}} -AutoSize | Out-String

 

 

 ================================================================

Hash Table只能add两个属性,一个name,一个value

1 $n =$null

2 $n = [pscustomobject]@{

3 Name= 'Boe' 4 Number = 1 5 ID = 007 6 } 7 $n.add("p1","v1")

结果:

Hash Table构建

 1 $states = @{"Washington" = "Olympia"; "Oregon" = "Salem"; California = "Sacramento"}

 2 $states.Add("Alaska", "Fairbanks")

 3 $states.Remove("Alaska")

 4 $states.Set_Item("Alaska", "Juneau")

 5 $states.Get_Item("Oregon")

 6 $states.ContainsKey("Oregon")

 7 $states.ContainsValue("Salem")

 8 $states | Sort-Object

 9 $states.GetEnumerator() | Sort-Object Name

10 

11 $states.GetEnumerator() | % {$_.name} 

 结果:  只有 .name和.value 两个属性列,不能再添加其他属性列

Hash Table构建

 

 1 $hash=$null

 2 $hash=@{}

 3 $ps = gps

 4 foreach($p in $ps)

 5 {

 6 $hash.add($p.id,$p.processname)

 7 }

 8 

 9 $hash.getenumerator()| % {$_.name}

10 # $hash[0] 或者 $hash.getenumerator()[0] #提示出错

 结果:

Hash Table构建

===================================================

这个不是Hash Table

 1 $ps = gps

 2 $hashs = $null

 3 $hashs = @()

 4 Foreach ($p in $ps)

 5     {

 6      $hash = ""|Select pid,pname

 7      $hash.pid = $p.ID

 8      $hash.pname = $p.Processname

 9      $hashs += $hash

10     }

11 

12 $hashs[0].pid     

13 $hashs.psobject|select -ExpandProperty baseobject
 1 $results=@()

 2 foreach ($m in $ms)

 3 {

 4 $result=""|select a,b,c

 5 $result.a = $m.a

 6 $result.b = $m.b

 7 $result.c = $m.c

 8 $results += $result

 9 }

10 $results | export-csv -encoding utf8 -notypeinformation -path c:\perc\result.csv

 

 Example1:

 1 $servers="10.4.34.41","10.4.34.40","10.4.34.36"#,"10.4.34.37","10.4.34.39"

 2 $results = @() 

 3 foreach ($c in $servers)

 4 {

 5 $serverpass="Dell1950"

 6 $UserName="Administrator"

 7 $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force

 8 $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)

 9 $cs = gwmi win32_computersystem -computer $c -Credential $cred

10 $sys = gwmi win32_operatingsystem -computer $c -Credential $cred

11 

12 $result=""|select name,version

13 $result.name = $cs.name

14 $result.version =$sys.version

15 $results +=$result

16 }

17 $results

Hash Table构建

=====================

Example2:

 1 $infors = gc E:\Port-Test_htm\ports01.txt

 2 $results=@()

 3 foreach ($infor in $infors)

 4 {

 5 $a=$infor.split(",")[0]

 6 $b=$infor.split(",")[1]

 7 $result=""|select name,port

 8 $result.name = $a

 9 $result.port = $b

10 $results += $result

11 }

12 $results

 

你可能感兴趣的:(table)