2010-06-14 4 views
0

J'ai (encore une autre) requête powershell. J'ai un tableau dans powershell dont j'ai besoin d'utiliser les commandes remove() et split sur.powershell méthodes de membre manquantes dans le tableau

Normalement, vous définissez un tableau (ou une variable) et les méthodes ci-dessus existent. Sur le tableau $ csv2 ci-dessous les deux méthodes sont manquantes, j'ai vérifié en utilisant le cmd get-member.

Comment est-ce que je peux utiliser supprimer pour supprimer des lignes avec nan. Aussi, comment diviser les colonnes en deux variables différentes. à l'heure actuelle, chaque élément du tableau affiche une ligne, pour chaque ligne j'ai besoin de le convertir en deux variables, une pour chaque colonne.

horodatage utilisation
----------- ---------
1276505880 2.0763250000e + 00
1276505890 1.7487730000e + 00
1276505900 1.6906890000e + 00
1276505910 1.7972880000e + 00
1276505920 1.8141900000e + 00
1276505930 nan nan
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 

Répondre

3

Il n'y a pas de méthode Remove ou Split sur le type de .NET Array, ou ajoutée par l'enveloppe PowerShell autour d'une instance Array. Ceci est assez facile de montrer:

 
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;} 

tableaux dans .NET et PowerShell, sont de taille fixe. Pour supprimer un élément que vous devez copier tout sauf l'élément à éliminer, dans PSH cela peut être fait avec Where-Object:

$newArray = $oldArray | Where-Object {some-condition-on-$_} 

De même Select-Object avec -First et -Skip paramètres peuvent être utilisés pour sélectionner des éléments avant ou après (resp3ectively) Un index.


NBSystem.Array n'implémente System.Collections.ILst mais la mise en œuvre explicite de IList.Remove jette juste un NotImplementedException.

+0

Merci beaucoup, ça a marché. J'avais lu un tutoriel sur Arraylists et je n'avais pas réalisé qu'il y avait une différence – Andrew

+1

@Andrew: en effet, 'ArrayList' et' Array' sont des types complètement différents. 'ArrayList' est plus flexible mais a plus de frais généraux, dans les applications cela peut être significatif, mais peu probable dans les scripts. Cependant, toutes les opérations de collecte intégrées dans PSH créent des tableaux. – Richard

Questions connexes