derhoeppi
Goto Top

PowerShell String zerlegen in Array, DataTable, Hashtable speichern

Guten Morgen,

leider habe wieder ein Thema beim zerlegen eines Strings. Folgendes Problem:

Die Rückgabe eines Admintools liefert mir einen sehr langen String. In der Dokumentation steht, dass die Ausgabe per Komma getrennt ist und 54 Spalten umfasst. Die Ausgabe kann leider nicht modifiziert werden. Um diese Daten zu verarbeiten habe ich nun eine DataTable gebildet, die diese Daten nach einem Split entgegennimmt. Mein Problem ist nun die weitere Verarbeitung der Daten.
Wenn ich Daten aus der DataTable in ein neues Array schreibe (ist aus meiner Sicht nicht einmal notwendig), dann schreibt er mir die Daten als System.Data.DataRow ins Array. Damit kann ich aber wiederum nicht filtern und vergleichen.

$dt = New-Object System.Data.DataTable
$dt.Columns.Add('jobid') | Out-Null  
$dt.Columns.Add('jobtype') | Out-Null  
$dt.Columns.Add('state') | Out-Null  
...
$dt.Columns.Add('status') | Out-Null  
$dt.Columns.Add('backupid') | Out-Null  
$dt.Columns.Add('killable') | Out-Null  
$dt.Columns.Add('controllinghost') | Out-Null  


$Output = @()
$Output = bpdbjobs -report -most_columns
$Array_Jobs = @()
for ($i=0;$i -lt $Output.Count;$i++){
    $cols = $Output[$i].Trim() -split ","   
    $dt.Rows.Add($cols, $cols[1], $cols[2], $cols[3], $cols[4], $cols[5], $cols[6], $cols[7], $cols[8], $cols[9], $cols[10], $cols[11], $cols[12], $cols[13], $cols[14], $cols[15], $cols[16], $cols[17], $cols[18], $cols[19], $cols[20], $cols[21], $cols[22], $cols[23], $cols[24], $cols[25], $cols[26], $cols[27], $cols[28], $cols[29], $cols[30], $cols[31], $cols[32], $cols[33], $cols[34], $cols[35], $cols[36], $cols[37], $cols[38], $cols[39], $cols[40], $cols[41], $cols[42], $cols[43], $cols[44], $cols[45], $cols[46], $cols[47], $cols[48], $cols[49], $cols[50], $cols[51], $cols[52], $cols[53]) | Out-Null
	} 
[array]$Failed_Jobs = @()
$Failed_Jobs = $dt | ?{$_.status -ne 0} | Select -expand jobid, status, client
Write-Host $Failed_Jobs.count
Write-Host
Write-Host $Failed_Jobs

Mein Ziel ist die Daten in der DataTable via $dt.jobid anzusprechen und diese zu sortieren. Bis jetzt muss ich aber in der DataTable die Zeile mitgeben $dt.jobid[1].
Wo liegt mein Fehler?

Content-ID: 309573

Url: https://administrator.de/forum/powershell-string-zerlegen-in-array-datatable-hashtable-speichern-309573.html

Ausgedruckt am: 25.12.2024 um 04:12 Uhr

129813
129813 12.07.2016 aktualisiert um 10:46:39 Uhr
Goto Top
You need to give a single array to the add function, not 53 single parameters!
So
$dt.Rows.Add($cols)
is enough instead of line 17 face-wink

Regards
129813
129813 12.07.2016 aktualisiert um 10:45:56 Uhr
Goto Top
p.s. You don't need a datatable all the time.

You can create custom objects in powershell very easily:
$arrayOfObjects = @()
$arrayOfObjects += [pscustomobject] @{Column1 = "Data1";Column2 = "Data2";ColumnN = "DataN"}  
$arrayOfObjects
and with Add-Member you can add properties (columns) to the object afterwards if you need more.
derhoeppi
derhoeppi 12.07.2016 um 12:57:52 Uhr
Goto Top
Hi,


Zitat von @129813:

$dt.Rows.Add($cols)

Powershell returns that are more than one columns are defined - so it is not possible to add this row.


Zitat von @129813:

> $arrayOfObjects = @()
> $arrayOfObjects += [pscustomobject] @{Column1 = "Data1";Column2 = "Data2";ColumnN = "DataN"}  
> $arrayOfObjects
> 

This was my first try. The result if i try to access a value like them $arrayOfObjects i got a message that it is a hashtable.
129813
129813 12.07.2016 aktualisiert um 13:16:12 Uhr
Goto Top
Zitat von @derhoeppi:
Powershell returns that are more than one columns are defined - so it is not possible to add this row.
?? Sure, works as designed, if the number of columns equals your array count...!

This was my first try. The result if i try to access a value like them $arrayOfObjects i got a message that it is a hashtable.
?? No, the above is an array of objects!!!
derhoeppi
derhoeppi 12.07.2016, aktualisiert am 13.07.2016 um 07:17:07 Uhr
Goto Top
Zitat von @129813:

Zitat von @derhoeppi:
Powershell returns that are more than one columns are defined - so it is not possible to add this row.
?? Sure, works as designed ...!
Thats the error message: "Exception calling "Add" with "1" argument(s): "Input array is longer than the number of columns in this table.""


Zitat von @129813:

This was my first try. The result if i try to access a value like them $arrayOfObjects i got a message that it is a hashtable.
?? No, the above is an array of objects!!!
Okay but that's the retun value: "System.Collections.Hashtable"

Thats the actual code:

foreach($i in $Output){
	$cols = $i -split ","   
	$report += [pscustomobject] @{"jobid"=$cols;"jobtype"=$cols[1];"state"=$cols[2];"status"=$cols[3];"class"=$cols[4];"schedule"=$cols[  
}
$report.jobid is emtpy
$report[1] is empty
and $report is a hashtable.
129813
Lösung 129813 12.07.2016 aktualisiert um 13:41:34 Uhr
Goto Top
Zitat von @derhoeppi:
Thats the error message: "Exception calling "Add" with "1" argument(s): "Input array is longer than the number of columns in this table.""
That's what i said, the array has to be the same length as the count of columns ...so that's normal and is a must.

Okay but that's the retun value: "System.Collections.Hashtable"
No .... you have to query the column (property) you want to access!
$report.jobid

You should definitely read more about custom objects ....
Working With Custom Objects

Powershell is an object oriented scripting language ...

$report must be an object array of kind
System.Management.Automation.PSCustomObject

This shortcut works starting on PS 3.0.

On PS 2.0 you have to use this form
$report += new-Object PSObject -Property @{.............}
derhoeppi
derhoeppi 13.07.2016 um 07:15:47 Uhr
Goto Top
Sorry but i want to try this one, because it's new for me (instead of $report += new-Object PSObject -Property @{.............}).


Zitat von @129813:

p.s. You don't need a datatable all the time.

You can create custom objects in powershell very easily:
> $arrayOfObjects = @()
> $arrayOfObjects += [pscustomobject] @{Column1 = "Data1";Column2 = "Data2";ColumnN = "DataN"}  
> $arrayOfObjects
> 
and with Add-Member you can add properties (columns) to the object afterwards if you need more.

I forgot that the script is not running on my normal scripting Workstation, so i read out the psversiontable. At the host where i'm running the script is only PS Version 2.0 installed.
This one "$report += new-Object PSObject -Property @{.............}" was my first try before i wrote this thread. But the content of $Report was not readable for me so i went to find another solution. Now it's working!