MyITGuy
5/4/2018 - 8:21 PM

Combine-Objects

Combines two objects into a single object with an option to exclude properties.

Returns PSCustomObject

function Combine-Objects {
	[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="DefaultPS")]
	PARAM(
	    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName="DefaultPS")]
		$InputObject
		, 
	    [Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName="DefaultPS")]
		$AppendObject
		,
	    [Parameter(Mandatory = $false, Position = 2, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName="DefaultPS")]
		[String[]]
		$ExcludeProperty
	)

	begin {
	}

	process {
		$hash = @{}
		foreach ($Property in $InputObject.PSObject.Properties){
			if ($ExcludeProperty -inotcontains $Property.Name) {$hash += @{$Property.Name = $Property.Value}}
		}
		foreach ($Property in $AppendObject.PSObject.Properties){
			if ($ExcludeProperty -inotcontains $Property.Name) {$hash += @{$Property.Name = $Property.Value}}
		}
		$OutputObject = New-Object -TypeName PSObject -Property $hash
		$OutputObject | Select-Object ([string[]]($OutputObject | Get-Member | ? {$_.MemberType -eq 'NoteProperty'} | % {$_.Name} | Sort-Object))
	}

	end {
	}
}