在 powershell 中使用 ref 到函数中的 return 值

Using ref in powershell to return values from function

我有函数 DoWork 创建一个对象并将其保存在 $AllMailboxes 变量中。然后在该函数中我执行另一个函数 ProcessEmail,它应该从 $AllMailboxes 中取出 $Mailbox 并通过 ref 变量,向其添加几个字段并更新 $AllMailboxes 或创建新的 $collection 然后保存所有带有更新字段的 $Mailbox

$collection = @()

function DoWork() { 
    Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq 'UserMailbox' } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ }
    $AllMailboxes = Get-Mailbox -ResultSize Unlimited | Where { $_.RecipientTypeDetails -eq "UserMailbox" } | ForEach {
    $PrimarySmtpDomain = $_.PrimarySmtpAddress.split("@")

    New-Object psobject | 
        Add-Member -PassThru NoteProperty Alias $_.Alias | 
        Add-Member -PassThru NoteProperty Name $_.Name |        
        Add-Member -PassThru NoteProperty DisplayName $_.DisplayName 
        Add-Member -PassThru NoteProperty .... other values
     foreach ($mailbox in $allmailboxes) {
         $FullEmail = "somestring"
         ProcessEmail ([ref] $Mailbox) ($FullEmail)
     }
     $collection | ft # doesn't display anything


}

function ProcessEmail ([ref] $Mailbox, $FullEmail) {
    $RequireAdd = $true
    $addresses = $Mailbox.EmailAddresses
    foreach ($address in $addresses) {
        if ($address -imatch "sip:") { continue }
        if ($address -ireplace("smtp:","") -ieq $FullEmail) {
            $requireAdd = $false
            break
    }

    $Mailbox | Add-Member -MemberType NoteProperty -Name NewEmailToAdd -Value $FullEmail 
    $Mailbox | Add-Member -MemberType NoteProperty -Name NewEmailRequiresAdding -Value $RequireAdd  
    $Mailbox.NewEmailToAdd # displays correctly
    $Mailbox.NewEmailRequiresAdding #display correctly
    $collection += $Mailbox
}

我已经尝试过使用 ref 的多个方法,没有 ref,创建单独的变量,但由于某种原因我不能让它显示 $collection 中的任何内容或以 ProcessEmail 函数之外的其他方式显示任何内容。我确定我遗漏了什么。

您似乎缺少范围。至少将其更改为脚本范围,如下所示:

$script:collection = @()
$script:collection += $Mailbox

您使用 PSReference(这需要您访问值 属性)使其变得更加复杂。到目前为止,你不需要在这里。

也几乎不需要使用该全局/脚本变量,除非可能作为来自 DoWork 的赋值,如此模型所示。

function DoWork {
    foreach ($i in (1..100)) {
        $psObject = [PSCustomObject]@{
            Property1 = 1
            Property2 = 2
        }

        ProcessEmail -Mailbox $psObject -FullEmail $FullEmail

        $psObject
    }
}

function ProcessEmail {
    param(
        $Mailbox,
    )

    $Mailbox | Add-Member NewProperty1 "one"
    $Mailbox | Add-Member NewProperty2 "two"
}

$collection = DoWork

克里斯

其实我已经决定要去

function ProcessEmail ($Mailbox, $FullEmail) {
    $RequireAdd = $true
    $addresses = $Mailbox.EmailAddresses
    foreach ($address in $addresses) {
        if ($address -imatch "sip:") { continue }
        if ($address -ireplace("smtp:","") -ieq $FullEmail) {
            $requireAdd = $false
            break
        }
    }
    $Mailbox | Add-Member -MemberType NoteProperty -Name NewEmailToAdd -Value $FullEmail 
    $Mailbox | Add-Member -MemberType NoteProperty -Name NewEmailRequiresAdding -Value $RequireAdd  
    return ,$mailbox
}    

简单地通过:

$Mailbox = ProcessEmail ($Mailbox) ($FullEmail)
$collection += $Mailbox

似乎工作正常。