2016-12-02 96 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