Arrays: Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) K |
Admin (Diskussion | Beiträge) |
||
Zeile 17: | Zeile 17: | ||
Wenn das Array nicht zuerst als ''System.Collections.ArrayList'' definiert wird, erscheint folgende Fehlermeldung: <br> | Wenn das Array nicht zuerst als ''System.Collections.ArrayList'' definiert wird, erscheint folgende Fehlermeldung: <br> | ||
'''Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."''' | '''Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."''' | ||
+ | |||
Weitere Informationen: | Weitere Informationen: | ||
*http://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html | *http://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html | ||
*https://www.sapien.com/blog/2014/11/18/removing-objects-from-arrays-in-powershell/ | *https://www.sapien.com/blog/2014/11/18/removing-objects-from-arrays-in-powershell/ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == Array to Hashtable to JSON ''(Name: Value)'' == | ||
+ | <source lang="powershell"> | ||
+ | $Items = Get-ChildItem -Path * -File | ||
+ | $report = @() | ||
+ | |||
+ | Foreach ($Item in $Items) { | ||
+ | $table = @{ | ||
+ | $Item.Name = $Item.Length | ||
+ | } | ||
+ | $report += $table | ||
+ | } | ||
+ | |||
+ | $report | ConvertTo-Json | ||
+ | </source> | ||
+ | |||
+ | |||
+ | === Output === | ||
+ | $report | ConvertTo-Json | ||
+ | [ | ||
+ | { | ||
+ | "accesschk.exe": 773288 | ||
+ | }, | ||
+ | { | ||
+ | "accesschk64.exe": 403120 | ||
+ | }, | ||
+ | { | ||
+ | "AccessEnum.exe": 174968 | ||
+ | }, | ||
+ | { | ||
+ | "AdExplorer.chm": 50379 | ||
+ | }, | ||
+ | { | ||
+ | "ADExplorer.exe": 479832 | ||
+ | } | ||
+ | ] | ||
+ | |||
+ | |||
[[Kategorie:PowerShell]] | [[Kategorie:PowerShell]] |
Version vom 11. Februar 2019, 15:13 Uhr
Inhaltsverzeichnis
[Verbergen]Variable als Array definieren
$Variable = @()
In einer Foreach-Schlaufe, mehrere PowerShell-Objekte in ein Array laden/hinzufügen (+=)
Wenn mehrere PowerShell-Objekte in Array geladen werden müssen, muss man das Array zuerst definieren. -> $Array = @()
Falls der Array nicht im vorhinein definiert wird, erscheint folgender Fehler:
Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 'op_Addition'.
Item aus einem Array entfernen
Um ein Item aus einem Array entfernen zu können, muss das Array zuerst als System.Collections.ArrayList definiert werden:
[System.Collections.ArrayList]$Array = $Array
danach kann mit der Methode Remove das Item entfernt werden:
$Array.Remove("zu_entfernendes_item")
Wenn das Array nicht zuerst als System.Collections.ArrayList definiert wird, erscheint folgende Fehlermeldung:
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
Weitere Informationen:
- http://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html
- https://www.sapien.com/blog/2014/11/18/removing-objects-from-arrays-in-powershell/
Array to Hashtable to JSON (Name: Value)
$Items = Get-ChildItem -Path * -File $report = @() Foreach ($Item in $Items) { $table = @{ $Item.Name = $Item.Length } $report += $table } $report | ConvertTo-Json
Output
$report | ConvertTo-Json [ { "accesschk.exe": 773288 }, { "accesschk64.exe": 403120 }, { "AccessEnum.exe": 174968 }, { "AdExplorer.chm": 50379 }, { "ADExplorer.exe": 479832 } ]