积极答复者
这段用了管道的PowerShell为什么会出错?

问题
-
这几天一直在跟PowerShell较劲。
写了一段代码检索AD Group的信息,其中利用了管道的方式。结果运行过程中总是在检索完某个固定的组之后出错,提示Get-ADGroup这一行代码上“枚举上下文无效”。就是说这个组能够正常检索,它之后的一个组出错,但我不知道出错的到底是哪一个组。
$FilePath = 'C:\Temp\ADGroupMembers\pipe.txt' Get-ADGroup -Filter * -Properties name, member | ForEach-Object { $GroupMembers = Get-ADGroupMember $_ Out-File -FilePath $FilePath -InputObject ($_.Name + "`t" + $GroupMembers.Name.Count) -Append }
出错信息:
Get-ADGroup : 服务器返回以下错误: 枚举上下文无效。
所在位置 C:\temp\ADGroupMembers\ADGroupMemberCount.ps1:11 字符: 1
+ Get-ADGroup -Filter * -Properties name, member | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADGroup], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADGroup然后我改写成以下不使用管道的方式,运行就正常了。
$FilePath = 'C:\Temp\ADGroupMembers\nopipe.txt' $GroupInfo = Get-ADGroup -Filter * -Properties name, member foreach ($Group in $GroupInfo) { $GroupMembers = Get-ADGroupMember $Group Out-File -FilePath $FilePath -InputObject ($Group.Name + "`t" + $GroupMembers.Name.Count) -Append }
虽然问题能解决,但不清楚第一段用管道方式的代码问题出在什么地方。有什么办法进一步调试吗?或者能否定位造成出错的是哪一个AD Group?
- 已编辑 Stanley_L 2017年6月28日 6:32
答案
-
您好 Stanley_L,
如果单独运行verbose这条命令也不报错的话,综合您的问题情况来看,我个人认为其他问题不大,可能是powershell版本的问题,可以尝试升级来排错。
如果想要更深入的了解这个问题的症结,也可以发帖到powershell的论坛上,有专门负责powershell的专家,可以让他们看看,有没有什么其他排错思路。
https://social.technet.microsoft.com/Forums/windows/en-US/home?forum=winserverpowershell
Candy
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.- 已建议为答案 Candy LuoMicrosoft contingent staff, Moderator 2017年6月29日 8:44
- 已标记为答案 Stanley_L 2017年6月29日 9:04
-
Hi Candy,
突然发现其实我多此一举了,根本没必要再去Get-ADGroupMember,代码改成如下就正常了,而且快了很多。
$FilePath = 'C:\Temp\ADGroupMembers\pipe.txt' Get-ADGroup -Filter * -Properties name, member | ForEach-Object { Out-File -FilePath $FilePath -InputObject ($_.name + "`t" + $_.member.Count) -Append }
多谢了!
Stanley
- 已标记为答案 Stanley_L 2017年6月29日 9:04
全部回复
-
您好 Stanley_L,
我在我的环境中测试过,在组正常的情况下,两者获得的结果是一样的。管道方式的代码没有问题,这个情况可能是由于您的组出现问题导致的。
您可以用我下面的代码,可以获取所有组的信息,然后检查是哪个组出现了问题(注意标粗的地方):
$FilePath = 'C:\Temp\ADGroupMembers\pipe.txt' Get-ADGroup -Filter * -Properties name, member | %{ Write-Host $_.name $GroupMembers = Get-ADGroupMember $_.name $GroupMembers}
此致
Candy
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com. -
您好 Stanley_L,
运行verbose详细信息的命令可以看到出错的组吗?
Get-ADGroup -filter * -properties name,member -Verbose
如果还是看不到的话 ,请您尝试用ISE打开,重新输入管道代码测试看是否正常。
管道代码测试过一般是没有问题的,从您的情况看,问题应该和是否是管道代码无关,这个情况也有可能是powershell版本更新问题,可以尝试升级powershell来排错。
此致
Candy
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com. -
您好 Stanley_L,
如果单独运行verbose这条命令也不报错的话,综合您的问题情况来看,我个人认为其他问题不大,可能是powershell版本的问题,可以尝试升级来排错。
如果想要更深入的了解这个问题的症结,也可以发帖到powershell的论坛上,有专门负责powershell的专家,可以让他们看看,有没有什么其他排错思路。
https://social.technet.microsoft.com/Forums/windows/en-US/home?forum=winserverpowershell
Candy
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.- 已建议为答案 Candy LuoMicrosoft contingent staff, Moderator 2017年6月29日 8:44
- 已标记为答案 Stanley_L 2017年6月29日 9:04
-
Hi Candy,
突然发现其实我多此一举了,根本没必要再去Get-ADGroupMember,代码改成如下就正常了,而且快了很多。
$FilePath = 'C:\Temp\ADGroupMembers\pipe.txt' Get-ADGroup -Filter * -Properties name, member | ForEach-Object { Out-File -FilePath $FilePath -InputObject ($_.name + "`t" + $_.member.Count) -Append }
多谢了!
Stanley
- 已标记为答案 Stanley_L 2017年6月29日 9:04