我有一个脚本,我经常在工作中使用该脚本来显示用户帐户上的所有活动同步设备。我需要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
仅仅说出你认为问题出在哪里是不够的。重新开始编写一个小脚本,其中只包含重现问题所需的最少量代码。仔细阅读我发布的链接。 –