2010-06-14 92 views
0

我有(又一個)PowerShell查詢。我在PowerShell中有一個數組,我需要使用remove()和split命令。PowerShell中缺少成員方法

通常你設置一個數組(或變量),並存在上述方法。在下面的$ csv2數組中,兩個方法都缺失,我使用get-member cmd進行了檢查。

我該如何去使用刪除來擺脫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 

回答

3

有基於.NET的Array類型沒有RemoveSplit方法,或者通過圍繞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參數可以用來選擇之前或之後的元素(resp3ectively )索引。


NBSystem.Array並實現System.Collections.ILst但明確的實施IList.Remove只是拋出一個NotImplementedException

+0

非常感謝你,讓它工作。我讀過Arraylists的教程,並沒有意識到有什麼區別 – Andrew 2010-06-14 10:34:34

+1

@Andrew:的確,ArrayList和Array是完全不同的類型。 'ArrayList'更靈活,但有更多的開銷,在應用程序中這可能很重要,但在腳本中不太可能。但是,PSH中的所有內置收集操作都會創建數組。 – Richard 2010-06-14 14:48:15