0

我有创建一个对象并将其保存在$AllMailboxes变量中的 DoWork 函数。然后在该函数中,我执行另一个函数 ProcessEmail,该函数应该通过 ref$Mailbox取出$AllMailboxes和变量,向其中添加几个字段并更新$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功能之外的任何内容。我确定我错过了一些东西。

4

3 回答 3

1

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

$script:collection = @()
$script:collection += $Mailbox
于 2016-08-02T08:03:48.417 回答
1

您通过使用 PSReference (这将需要您访问 value 属性)使其变得更加复杂。到目前为止,你没有必要在这里。

也几乎不需要使用该全局/脚本变量,除非可能作为 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

克里斯

于 2016-08-02T08:43:16.297 回答
0

我真的决定去

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

似乎工作得很好。

于 2016-08-02T08:46:04.667 回答