Powershell中声明、使用类
Powershell提供的默认类型往往不能满足工作中的各种需求,所以使用自定义类型相当有必要。
下面就先举个栗子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 $vmConfigInformation = @" 2 public class vmInfo 3 { 4 public string vmName; 5 public int ioCount; 6 public string[] traceFiles; 7 8 public vmInfo ( string vm, 9 int count, 10 string[] trace 11 ) 12 { 13 this.vmName = vm; 14 this.ioCount = count; 15 this.traceFiles = trace; 16 } 17 } 18 "@ 19 20 Add-Type -TypeDefinition $vmConfigInformation 21 22 $testVmName = "win8testing" 23 $testCount = 10 24 $testTraceFiles = @("trace01.csv", "trace02.csv", "trace03.csv") 25 26 $test = New-Object vmInfo( $testVmName, 27 $testCount, 28 $testTraceFiles); 29 30 31 Write-Host "VM Name:" $test.vmName 32 Write-Host "VM IO Count:" $test.ioCount 33 Write-Host "VM Trace Files:" $test.traceFiles
运行结果:
有了这些自定义类型,工作起来着实方便了不少。
更详细的资料参见:Add-Type