【发布时间】:2017-04-17 21:29:08
【问题描述】:
我有一个脚本,我经常在工作中使用它来显示用户帐户上的所有活动同步设备。我需要一个启用或禁用 Activesync 的用户列表,所以我编辑了第一个脚本。然而,现在它循环了很多次,如果我放 -hidetableheadders,它会循环更多次。这段导致循环的代码有什么问题?我将首先放置我需要帮助的代码,然后将我复制的工作代码放在下面。
我认为问题在于“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
这是文件的输出,以 Matt 为例。我已经缩短了第一组,但包括了第二组的全部回报。我在运行命令时得到的日志,根据我的判断返回 OU 中每个用户的完整列表,而不仅仅是这个列表中的用户。这是因为在命令中有两次 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
接受 Matt 的建议,我在 $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
}
}
【问题讨论】:
-
听从the SSCCE的建议。
-
对不起比尔,我添加了我认为问题发生的地方。我不知道为什么我一开始就没有说明这一点。
-
仅仅说出你认为问题出在哪里是不够的。重新开始并编写一个小脚本,其中只包含重现问题所需的最少代码量。仔细阅读我发布的链接。
标签: loops powershell exchange-server activesync