我有(又一个)powershell查询。我在powershell中有一个数组,我需要使用remove()和split命令。
通常设置数组(或变量)并存在上述方法。在下面的$ csv2数组中,两个方法都缺失,我已经使用get-member cmd进行了检查。
我怎样才能使用remove来摆脱nan的线条。另外,我如何将列拆分为两个不同的变量。目前,数组的每个元素都显示一行,我需要将每行转换为两个变量,每列一个。
时间戳利用率
--------- -----------
1276505880 2.0763250000e + 00
1276505890 1.7487730000e + 00
1276505900 1.6906890000e + 00
1276505910 1.7972880000e + 00
1276505920 1.8141900000e + 00
1276505930南
1276505940南
1276505950 0.0000000000e + 00
$SystemStats = (Get-F5.iControl).SystemStatistics
$report = "c:\snmp\data" + $gObj + ".csv"
### Allocate a new Query Object and add the inputs needed
$Query = New-Object -TypeName iControl.SystemStatisticsPerformanceStatisticQuery
$Query.object_name = $i
$Query.start_time = $startTime
$Query.end_time = 0
$Query.interval = $interval
$Query.maximum_rows = 0
### Make method call passing in an array of size one with the specified query
$ReportData = $SystemStats.get_performance_graph_csv_statistics( (,$Query) )
### Allocate a new encoder and turn the byte array into a string
$ASCII = New-Object -TypeName System.Text.ASCIIEncoding
$csvdata = $ASCII.GetString($ReportData[0].statistic_data)
$csv2 = convertFrom-CSV $csvdata
$csv2
答案 0 :(得分:3)
.NET的Remove
类型没有Split
或Array
方法,或者由Array
实例的PowerShell包装器添加。这很容易展示:
PS[64bit] E:\> $a = 1,2,3,4,5 PS[64bit] E:\> $a.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array PS[64bit] E:\> Get-Member -InputObject $a TypeName: System.Object[] Name MemberType Definition ---- ---------- ---------- Count AliasProperty Count = Length Address Method System.Object&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK... Clone Method System.Object Clone() CopyTo Method System.Void CopyTo(array array, int index), System.Void CopyTo(arra... Equals Method bool Equals(System.Object obj) Get Method System.Object Get(int ) GetEnumerator Method System.Collections.IEnumerator GetEnumerator() GetHashCode Method int GetHashCode() GetLength Method int GetLength(int dimension) GetLongLength Method long GetLongLength(int dimension) GetLowerBound Method int GetLowerBound(int dimension) GetType Method type GetType() GetUpperBound Method int GetUpperBound(int dimension) GetValue Method System.Object GetValue(Params int[] indices), System.Object GetValu... Initialize Method System.Void Initialize() Set Method System.Void Set(int , System.Object ) SetValue Method System.Void SetValue(System.Object value, int index), System.Void S... ToString Method string ToString() IsFixedSize Property System.Boolean IsFixedSize {get;} IsReadOnly Property System.Boolean IsReadOnly {get;} IsSynchronized Property System.Boolean IsSynchronized {get;} Length Property System.Int32 Length {get;} LongLength Property System.Int64 LongLength {get;} Rank Property System.Int32 Rank {get;}
.NET和PowerShell中的数组是固定大小的。要删除元素,您需要复制除要移除的元素之外的所有元素,在PSH中可以使用Where-Object
完成:
$newArray = $oldArray | Where-Object {some-condition-on-$_}
同样,带有Select-Object
和-First
参数的-Skip
可用于在索引之前或之后(相反地)选择元素。
NB System.Array
确实实施了System.Collections.ILst
,但明确实施IList.Remove
只会引发NotImplementedException
。