Make Update-1st object props an advanced cmdlet & faster

This commit is contained in:
jhoneill
2019-11-30 15:28:05 +00:00
parent 163eaebf66
commit 6508043bc0

View File

@@ -5,20 +5,28 @@ param()
function Update-FirstObjectProperties {
[CmdletBinding()]
param()
try {
$Union = @()
$Input | ForEach-Object {
If ($Union.Count) {
$_ | Get-Member | Where-Object {-not ($Union[0] | Get-Member $_.Name)} | ForEach-Object {
$Union[0] | Add-Member -MemberType NoteProperty -Name $_.Name -Value $Null
}
param (
[Parameter(ValueFromPipeline=$true)]
$InputObject
)
begin { $union = New-Object -TypeName System.Collections.ArrayList }
process {
try {
If ($union.Count -eq 0) {
[void]$union.Add($InputObject)
$memberNames = (Get-Member -InputObject $InputObject -MemberType Properties).Name
}
else {
foreach ($propName in (Get-Member -InputObject $InputObject -MemberType Properties).Name) {
if ($propName -notin $memberNames) {
Add-Member -InputObject $Union[0] -MemberType NoteProperty -Name $propName -Value $Null
$memberNames += $propName
}
}
[void]$Union.Add($InputObject)
}
$Union += $_
}
$Union
}
catch {
throw "Failed updating the properties of the first object: $_"
catch {throw "Failed updating the properties of the first object: $_"}
}
end { $Union }
}