-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-PSCustomObjectWithIndexColumn.ps1
More file actions
76 lines (67 loc) · 3.03 KB
/
Copy pathNew-PSCustomObjectWithIndexColumn.ps1
File metadata and controls
76 lines (67 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<#
.SYNOPSIS
Ergaenzt ein PSCustomObject-Array um eine fortlaufende Indexspalte - drei
Wege im Vergleich.
.DESCRIPTION
Zeigt denselben Vorgang in drei Ausfuehrungen:
Update-...Slow ueber Select-Object mit berechneter Eigenschaft,
bequem, aber langsam
Update-...Fast ueber Add-Member, deutlich schneller
New-... erzeugt eine Kopie, statt das Original zu veraendern
Codebeispiel zum Nachschlagen, kein Betriebswerkzeug.
.NOTES
ACHTUNG bei den Update-Varianten: sie veraendern das uebergebene Objekt.
In PowerShell werden PSCustomObjects als Referenz weitergereicht - das
Original in der aufrufenden Variablen aendert sich mit. Wer das nicht
will, nimmt New-PSCustomObjectWithIndexColumn.
#>
function Update-PSCustomObjectWithIndexColumnSlow {
# DO NOT USE THIS FUNCTION, IF YOU DON'T WANT YOUR SOURCE PSCUSUTOMOBJECT!!!
# This one is slow.
param (
[Parameter(Mandatory = $true)]
[PSCustomObject]$PSCustomObjectToBeModiefiedWithIndexColumn
)
return $PSCustomObjectToBeModiefiedWithIndexColumn | ForEach-Object -Begin { $i = 1 } -Process {
$obj = [PSCustomObject]@{ index = $i++ }
$_.PSObject.Properties | ForEach-Object { $obj | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value }
$obj
}
}
function Update-PSCustomObjectWithIndexColumnFast {
# DO NOT USE THIS FUNCTION, IF YOU DON'T WANT YOUR SOURCE PSCUSUTOMOBJECT!!!
# This one is fast.
param (
[Parameter(Mandatory = $true)]
[PSCustomObject]$PSCustomObjectToBeModiefiedWithIndexColumn
)
return $PSCustomObjectToBeModiefiedWithIndexColumn | ForEach-Object -Begin { $i = 1 } -Process {
if (-not ($_.PSObject.Properties.Name -contains "index")) {
$_ | Add-Member -MemberType NoteProperty -Name "index" -Value ($i++)
}
else {
Throw("An index column can only be added if the elements do not yet have a column with the name index.")
}
}
}
function New-PSCustomObjectWithIndexColumn {
param (
[Parameter(Mandatory = $true)]
[PSCustomObject]$SourcePSCustomObjectWithoutIndexColumn
)
# Serializing and Deserialize is needed, because otherwise the pscustomobject will not be copied completely.
# But be careful. -->Note: However, weirdly, it does not keep the ordering of ordered hashtables.
# Source: https://stackoverflow.com/questions/9204829/deep-copying-a-psobject
$serialData = [System.Management.Automation.PSSerializer]::Serialize($SourcePSCustomObjectWithoutIndexColumn)
$workingcopy = [System.Management.Automation.PSSerializer]::Deserialize($serialData)
$i = 1
$workingcopy | ForEach-Object {
if (-not ($_.PSObject.Properties.Name -contains "index")) {
$_ | Add-Member -MemberType NoteProperty -Name "index" -Value ($i++)
return $_
}
else {
Throw("An index column can only be added if the elements do not yet have a column with the name index.")
}
}
}