Powershell函数导致异常

时间:2015-07-14 08:26:43

标签: function powershell

任何人都可以帮我弄清楚为什么这个计数在从函数中使用时加倍了?当我检查进入结果的值时,它只显示13的计数,但当我检查使用该函数获得的变量的值时,它显示为26.此外,它显示的前13个邮箱是空白的(如果我离开那部分,它现在已经注释掉了)。另一个奇怪的事情是我添加的计数整数永远不应该是0,因为我在写入值之前放了“count ++”,但是当它自身加倍时它从0开始。我删除了邮箱属性并替换它们用一个计数整数来试图找出它出错的地方,但我似乎无法找到它。也许我的大脑在这个夜晚不能正常工作。脚本在下面,我也会粘贴它返回的输出。感谢您的帮助,仅供参考,测试OU中有13个对象,因此13个结果是正确的。

function Get-MailboxUsers {
  param
  (
    [Parameter(Mandatory=$False, ValueFromPipeline=$False, ValueFromPipelineByPropertyName=$False, HelpMessage='Organizational Unit to get mailboxes from.')]
    [Alias('OrganizationalUnit', 'Path')]
    [String] 
    $OU,
    [Parameter(Mandatory=$False, ValueFromPipeline=$False, ValueFromPipelineByPropertyName=$False, HelpMessage='Users to exclude')]
    [Alias('Hide', 'DoNotInclude')]
    [String] 
    $Exclude
  )

    $out = New-Object System.Collections.ArrayList;
    $count = 0;
    Get-Mailbox -OrganizationalUnit $OU | ForEach-Object {
        $count++;
        Write-Host $count
        $FullName = $_.Name;
        $DistinguishedName = $_.DistinguishedName;
        $DeliverAndRedirect = $_.DeliverToMailboxAndForward;
        $Displayname = $_.DisplayName;
        $Email = $_.PrimarySmtpAddress;
        $ForwardingSmtp = $_.ForwardingSmtpAddress;
        $Forwarding = $_.ForwardingAddress;
        $id = $_.id;

        $NewUser = @{
            FullName = $_.Name;
            DistinguishedName = $_.DistinguishedName;
            DeliverAndRedirect = $_.DeliverToMailboxAndForward;
            Displayname = $_.DisplayName;
            Email = $_.PrimarySmtpAddress;
            ForwardingSmtp = $_.ForwardingSmtpAddress;
            Forwarding = $_.ForwardingAddress;
            Id = $_.id;
        }
        Write-Host "Adding $count to array";
        $out.Add($count);
        #$out.Add($NewUser);
        #$OutObj.Add($NewUser);



    }

    $newCount = $out.Count;
    Write-Host "Count variable is $count";
    Write-Host "Count of output object is $newCount";

    foreach($Mailbox in $out)
    {

        #Write-Host ([string]::Format("DN: {0} | FN: {1} | ID: {2}", $Mailbox.DistinguishedName, $Mailbox.FullName, $Mailbox.Id));
        Write-Host $Mailbox

    }

    return $out;


}


$Users = Get-MailboxUsers -OU "OU=Users,OU=IT,DC=contoso,DC=com"

$newCount = $Users.Count

Write-Host "Count of mailboxes is $newCount";

foreach($User in $Users)
{

    #Write-Host ([string]::Format("DN: {0} | FN: {1} | ID: {2}", $Mailbox.DistinguishedName, $Mailbox.FullName, $Mailbox.Id));
    Write-Host $User

}

输出

P:\Scripts\Dev\User Maintenance\GetMailboxes.ps1
1
Adding 1 to array
2
Adding 2 to array
3
Adding 3 to array
4
Adding 4 to array
5
Adding 5 to array
6
Adding 6 to array
7
Adding 7 to array
8
Adding 8 to array
9
Adding 9 to array
10
Adding 10 to array
11
Adding 11 to array
12
Adding 12 to array
13
Adding 13 to array
Checking output variable values inside function, they appear to be fine
1
2
3
4
5
6
7
8
9
10
11
12
13
Checking counts inside function, they appear to be fine as well.
Count variable is 13
Count of output object is 13
If everything above here is 13 then this should be working, unless I am missing something.
Count of new array is 26
0
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
13

0 个答案:

没有答案