2016-12-02 97 views
0

我有一个脚本,我经常在工作中使用该脚本来显示用户帐户上的所有活动同步设备。我需要Activesync启用或禁用用户列表,所以我编辑了第一个脚本。然而,现在它循环了很多次,如果我把可隐藏的头引起来的话它会循环更多次。我在这段代码中导致循环的错误是什么?我将把我需要帮助的代码放在第一位,然后我将从下面复制的工作代码放在那里。Exchange Powershell脚本循环

我认为问题在于“foreach”块,但无论我在里面改变什么,甚至删除它,文件都是空的。

ActiveSync启用代码:

#Settings for file ouput 
$fLocation = "D:\Exchange Reports\" 

#Read OU imput from console: 
$OU = Read-Host -Prompt "Input the OU name to search: (0202 - Dev Bank)" 

#lookup users based on the OU 
$Ulist = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=CSIDMZ,DC=local" -ResultSize Unlimited 


foreach ($user in $Ulist) 
    { 
     $aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize 

     if ($aSyncUser -ne $null) 
      { 
       $deviceID = $aSyncUser | out-string 
       $Content = "User: $user $deviceID" 
       Add-Content $fName $Content 
      } 

    } 

    write-host "The script completed successfully! The output file can be found at $fName" -ForeGroundColor Yellow 

原始代码:

#Settings for file ouput 
$fLocation = "D:\Exchange Reports\" 

#Read OU imput from console: 
$OU = Read-Host -Prompt "Input the OU name to search: (0202 - Dev Bank)" 

#lookup users based on the OU 
$Ulist = get-mailbox -OrganizationalUnit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" -ResultSize Unlimited 


foreach ($user in $Ulist) 
    { 
     $aSyncUser = get-activesyncdevice -mailbox $user.SAMAccountName -ErrorAction SilentlyContinue |ft DeviceID -HideTableHeaders 

     if ($aSyncUser -ne $null) 
      { 
       $deviceID = $aSyncUser | out-string 
       $Content = "User: $user $deviceID" 
       Add-Content $fName $Content 
      } 
    } 

    write-host "The script completed successfully! The output file can be found at $fName" -ForeGroundColor Yellow 

下面是该文件的输出,对于马特的例子。我缩短了第一盘,但是包括了第二盘的全部收益。我运行该命令时得到的日志,从我所知道的OU中返回每个用户的完整列表,而不仅仅是列表中的那些用户。这是由于在命令中有两次选择?

User: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData 
Name    ActiveSyncEnabled 
----    ----------------- 
Judy X Langford    True 

User: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData 
Name    ActiveSyncEnabled 
----    ----------------- 
Judy X Langford    True 

我编辑的命令,并得到这个回报,这就是我放倒关闭,这是为每个用户一次返回的结果,我需要的只是结果一次。我包含要显示的整个结果集,用户Sharla不在其下面的返回列表中。

User: Domain.local/Hosted Exchange Customers/This is my OU/Sharla x Ryan 
Name    ActiveSyncEnabled 
----    ----------------- 
Judy X Langford    True 
Sandy X Stuckey    True 
0718 test      True 
Shelby D Hansche    True 
Toni X Brooks     True 
Katie X Strain    True 
Monica Minter     True 
Chloe X Mims     True 
Jay X Davis     True 
Aaron Calvit     True 
Joe Nichols     True 
Sherry X Rice     True 
Tracey X Wardlaw    True 
Brad X Davis     True 
Chris X Lannom    True 
Haley X Burleson    True 
Cody X Deal     True 
Cris X Day     True 
Mitch X Stone     True 



User: Domain.local/Hosted Exchange Customers/This is my OU/Judy X Langford 
Name    ActiveSyncEnabled 
----    ----------------- 
Judy X Langford    True 

接受马特的建议,我在$ user.samaccountname行添加了回来。这打破了输出,所以我不得不这样做,但现在它激励我需要什么。

#lookup users based on the OU 

Get-CASMailbox -OrganizationalUnit "OU=$OU,OU=Hosted Exchange Customers,DC=CSIDMZ,DC=local" -ResultSize unlimited | select name,activesyncenabled >>$fLocation+$OU+"_ActiveSyncEnabled.txt" 

foreach ($user in $Ulist) 
    { 
     $aSyncUser = get-activesyncdevice -mailbox $user.SAMAccountName -ErrorAction SilentlyContinue |ft name, activesyncenabled -HideTableHeaders 

     if ($aSyncUser -ne $null) 
      { 
       $deviceID = $aSyncUser | out-string 
       $Content = "User: $user $deviceID" 
       Add-Content $fName $Content 
      } 
    } 
+1

请遵循[SSCCE](http://sscce.org/)的建议。 –

+0

对不起比尔,我加了我认为问题发生的地方。我不知道为什么我没有说这一开始。 – Tekwhat

+0

仅仅说出你认为问题出在哪里是不够的。重新开始编写一个小脚本,其中只包含重现问题所需的最少量代码。仔细阅读我发布的链接。 –

回答

0

这可能不是整个问题,但可以肯定的是它

$aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize 
  1. 你没有得到一个用户的很大一部分。您每次都会在该OU中获得全部用户。我无法想象这就是你要做的。
  2. 您对Format-Table的使用仅仅是要求问题。看看我的回答关于这一主题在这里How can I store output from Format-Table for later use

我需要ActiveSync启用或禁用用户

因此,所有的用户,那么列表?不知道为什么要搜索循环外部的CAS-Mailbox,以便在循环中的其他OU中再次搜索。

+0

这个想法是生成一个已启用Activesync的OU中的所有用户的列表。我得到的结果是正确的,但它为每个用户重演。星期一我重新开始工作,向大家展示我正在谈论的内容时,我会举一个例子。 – Tekwhat

+0

Matt,我用这个例子更新了OP。 – Tekwhat

+0

@Tekwhat你看过我的回答吗?特别指出1.是否所有的用户都在这个OU中返回? – Matt