[英]Exchange Powershell script looping
I have a script that I use often at work to display all active sync devices on a users account. 我有一个经常在工作中使用的脚本,用于显示用户帐户上的所有活动同步设备。 I needed a list of Activesync enabled or disabled users, so I edited the first script.
我需要启用或禁用Activesync的用户列表,因此我编辑了第一个脚本。 Now however, it is looping numerous times, and it loops even more times if I put -hidetableheadders.
但是现在,它循环了很多次,如果我放-hidetableheadders,它甚至循环了更多次。 What do I have wrong in this code that is causing the loop?
在导致循环的这段代码中,我有什么错? I will put the code I need help with first, and I will put the working code I copied from below that.
我将首先放置需要帮助的代码,然后将从下面复制的工作代码放入。
I believe the issue lies in the "foreach" block, but no matter what I change in it, even removing it, the file is empty. 我相信问题出在“ foreach”块中,但是无论我更改了什么,甚至删除了它,文件都是空的。
Activesync enabled code: 启用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
Original Code: 原始代码:
#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
Here is the output of the file, for an example for Matt. 这是文件的输出,以Matt为例。 I have shortened the first set, but included the whole return for the second.
我缩短了第一套,但包括了第二套的全部收益。 The log I get when I run the command, returns the full list for every user in the OU from what I can tell, not just those that are in this list.
运行命令时获得的日志,将根据我的判断返回OU中每个用户的完整列表,而不仅仅是此列表中的用户。 Is this due to having the ou selection twice in the command?
这是因为在命令中两次选择了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
I edited the command, and got this return, this is what tipped me off that it was returning the results once for every user, I just need the results once. 我编辑了命令,并得到了返回值,这是我告诉我的结果,它为每个用户返回一次结果,我只需要返回一次结果。 I am including the entire result set to show, the user Sharla isn't in the returned list below it.
我包括要显示的整个结果集,用户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
Taking Matt's advice I added back in the line about $user.samaccountname. 根据Matt的建议,我在行中添加了有关$ user.samaccountname的信息。 This broke the output, so I had to and it in, but now it is giving me excatly what I needed.
这破坏了输出,因此我不得不插入它,但是现在它确实给了我所需的东西。
#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
}
}
This might not be the whole issue but it is certainly a large part of it 这可能不是整个问题,但肯定是其中的很大一部分
$aSyncUser = Get-CASMailbox -organizationalunit "OU=$OU,OU=Hosted Exchange Customers,DC=ThisIsMyDC,DC=local" | where { $_.ActiveSyncEnabled -eq 'True'} | ft name, activesyncenabled -autosize
I needed a list of Activesync enabled or disabled users
我需要启用或禁用Activesync的用户列表
So all users then? 那么所有用户呢? Not sure why you search for CAS-Mailboxes outside the loop to search again in a different OU either inside your loop.
不知道为什么要在循环外搜索CAS邮箱,然后在循环内的另一个OU中再次搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.