方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“ op_Addition”的方法

时间:2019-03-27 20:59:10

标签: powershell

我正在尝试将多个变量加在一起,如下所示:

$Var1 = Get-Mailbox-Identity "XXXXX" | select DisplayName
$Var2 = Get-Mailbox -Identity "XXXXX" | select PrimarySmtpAddress
$var3 = $var1 + $Var2

执行此操作时出现的错误是:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:1 char:1
+ $var3 = $var1 + $Var2
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

我知道您可以做到:

$Var1 = Get-Mailbox -Identity "XXXXX" | Select-Object DisplayName,PrimarySmtpAddress

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

当您尝试完成命令时,将得到一个自定义对象作为返回值,您需要设置ExpandProperty以获取实际值。

  

[STRING] $ Var1 =获取邮箱-身份“ XXXXX” |选择对象-ExpandProperty DisplayName

     

[STRING] $ Var2 =获取邮箱-身份“ XXXXX” |选择对象-ExpandProperty PrimarySmtpAddress

     

$ var3 = $ var1 + $ Var2

答案 1 :(得分:0)

不清楚“加法”对您的期望。 确实,您不能像使用+使用字符串或数组那样简单地将两个PSObject加在一起。

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

将产生具有两个属性DisplayNamePrimarySmtpAddress的单个PSObject

DisplayName  PrimarySmtpAddress         
-----------  ------------------         
Cassius Clay Mohammed.Ali@yourdomain.com

希望有帮助