【发布时间】:2016-08-02 07:59:57
【问题描述】:
我有一个函数 DoWork,它创建一个对象并将其保存在 $AllMailboxes 变量中。然后在该函数中,我执行另一个函数 ProcessEmail,它应该从 $AllMailboxes 中取出 $Mailbox 并通过 ref 变量,向其中添加几个字段,然后更新 $AllMailboxes 或创建新的 $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
}
我尝试了多个使用 ref 的方法,没有 ref,创建单独的变量,但由于某种原因,我不能让它在 $collection 或其他方式中显示任何东西,而不是 ProcessEmail 函数。我确定我错过了什么。
【问题讨论】:
标签: arrays powershell pscustomobject