在powershell中使用ref从函数

时间:2016-08-02 07:59:57

标签: arrays powershell pscustomobject

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

$collection

我尝试过使用ref的多个approces,没有参考,创建单独的变量,但由于某种原因,我无法在$ collection中显示任何内容或以$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 } 函数的其他方式显示任何内容。我确定我错过了什么。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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

克里斯

答案 2 :(得分:0)

我实际上决定去

function validation() {
  var inputs = document.forms['myForm'].getElementsByTagName('input').length;

  for (i = 0; i <= inputs; i++) {
    if (document.forms['myForm'][i].value == "") {
        document.getElementById("error-box").innerHTML="Please fill " + document.forms['myForm'][i].name;
    }
  }
}

简单地说:

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
}    

似乎工作得很好。