Не ясно, что вы ожидаете в результате «сложения».
На самом деле вы не можете просто добавить два объекта PSO вместе, как вы можете со строками или массивами, используя +
.
1) Если вам нужно, чтобы два объекта были объединены в массив, то это будет сделано:
$result = @()
$result += Get-Mailbox -Identity "XXXXX" | Select-Object DisplayName
$result += Get-Mailbox -Identity "XXXXX" | Select-Object PrimarySmtpAddress
2) Если искомый результат - это новый объект со свойствами двух объединенных объектов, может помочь что-то вроде приведенной ниже функции:
function Join-Properties {
# combine the properties of two PSObjects and return the result as new object
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
$ObjectA,
[Parameter(Mandatory = $true, Position = 1)]
$ObjectB,
[switch] $Overwrite
)
if ($ObjectA -is [PSObject] -and $ObjectB -is [PSObject]) {
# combine the properties of two PSObjects
# get arrays of property names for both objects
[string[]] $p1 = $ObjectA | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name
[string[]] $p2 = $ObjectB | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name
# or do it this way
# [string[]] $p1 = $ObjectA.PSObject.Properties | Select-Object -ExpandProperty Name
# [string[]] $p2 = $ObjectB.PSObject.Properties | Select-Object -ExpandProperty Name
# if you do not want to overwrite common properties in in $ObjectA with the values from objectB
if (!$Overwrite) { $p2 = $p2 | Where-Object { $p1 -notcontains $_ } }
# create a new object with al properties of $ObjectA and add (or overwrite) the properties from $ObjectB to it.
$Output = $ObjectA | Select-Object $p1
foreach($prop in $p2) {
Add-Member -InputObject $Output -MemberType NoteProperty -Name $prop -Value $ObjectB."$prop" -Force
}
# return the result
$Output
}
else {
Write-Warning "Only [PSObject] objects supported. Both input objects need to be of same type."
}
}
Используя ваш пример на этом:
$Var1 = Get-Mailbox -Identity "XXXXX" | select DisplayName
$Var2 = Get-Mailbox -Identity "XXXXX" | select PrimarySmtpAddress
$var3 = Join-Properties $Var1 $Var2
даст один PSObject с обоими свойствами DisplayName
и PrimarySmtpAddress
DisplayName PrimarySmtpAddress
----------- ------------------
Cassius Clay Mohammed.Ali@yourdomain.com
Надеюсь, что поможет