How to combine powershell objects ? The fusion sounds like a good idea right ? That is cool but it ‘might’ be a bit time consuming if whe need to learn the dance and stuff 😉
Read below to see how I have done it.
[stextbox id=”note” defcaption=”true”]You can only understand this if you are a DBZ fan (just like me ;))[/stextbox]
How to combine powershell objects ?
This is a question I have recently asked my self. I came up pretty quickly with the Combine-Objects function which allow to concatenate powershell custom objects, or objects that have been returned from WMI or AD for example.
[stextbox id=”download” caption=”Download”]Download the function fastly and directly from Technet right here –> Download script (Don’t forget to rate by clicking on the yellow stars ;))[/stextbox]
Script Listing:
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
|
Function Combine-Objects {
<#
.SYNOPSIS
Combine two PowerShell Objects into one.
.DESCRIPTION
will combine two custom powershell objects in order to make one. This can be helpfull to add information to an already existing object. (this might make sence in all the cases through).
.EXAMPLE
Combine objects allow you to combine two seperate custom objects together in one.
$Object1 = [PsCustomObject]@{“UserName”=$UserName;“FullName” = $FullName;“UPN”=$UPN}
$Object2 = [PsCustomObject]@{“VorName”= $Vorname;“NachName” = $NachName}
Combine-Object -Object1 $Object1 -Object2 $Object2
Name Value
—— ——–
UserName Vangust1
FullName Stephane van Gulick
UPN @PowerShellDistrict.com
VorName Stephane
NachName Van Gulick
.EXAMPLE
It is also possible to combine system objects (Which could not make sence sometimes though!).
$User = Get-ADUser -identity vanGulick
$Bios = Get-wmiObject -class win32_bios
Combine-Objects -Object1 $bios -Object2 $User
.NOTES
-Author: Stephane van Gulick
-Twitter : stephanevg
-CreationDate: 10/28/2014
-LastModifiedDate: 10/28/2014
-Version: 1.0
-History:
.LINK
http://www.powershellDistrict.com
#>
Param (
[Parameter(mandatory=$true)]$Object1,
[Parameter(mandatory=$true)]$Object2
)
$arguments = [Pscustomobject]@()
foreach ( $Property in $Object1.psobject.Properties){
$arguments += @{$Property.Name = $Property.value}
}
foreach ( $Property in $Object2.psobject.Properties){
$arguments += @{ $Property.Name= $Property.value}
}
$Object3 = [Pscustomobject]$arguments
return $Object3
}
|
Leave A Comment