我有一個腳本,我經常在工作中使用該腳本來顯示用戶帳戶上的所有活動同步設備。我需要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
}
}
請遵循[SSCCE](http://sscce.org/)的建議。 –
對不起比爾,我加了我認爲問題發生的地方。我不知道爲什麼我沒有說這一開始。 – Tekwhat
僅僅說出你認爲問題出在哪裏是不夠的。重新開始編寫一個小腳本,其中只包含重現問題所需的最少量代碼。仔細閱讀我發佈的鏈接。 –