You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell 7.4.2中Get-ADGroupMember管道Get-ADUser执行失败问题

PowerShell 7.4.2中Get-ADGroupMember管道到Get-ADUser报错的解决方法

问题场景

在PowerShell 7.4.2中执行以下脚本时出现管道绑定错误,但该脚本在PowerShell 5.1中可正常运行:

原脚本:

Get-ADGroupMember -Identity 'APPS Users' | Get-ADUser -Properties samaccountname,enabled,memberof

报错信息:

Get-ADUser: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

问题原因

PowerShell 7.x版本的Active Directory模块中,Get-ADGroupMember输出对象的管道属性绑定逻辑与PowerShell 5.1存在差异。5.1中,管道对象的SamAccountName属性可自动映射到Get-ADUser的Identity参数,但7.x版本取消了这种隐式绑定,导致无法直接通过管道传递对象。

解决方法

方法1:单组简化处理

通过ForEach-Object显式提取对象的SamAccountName并传递给Get-ADUser的Identity参数:

Get-ADGroupMember -Identity 'APPS Users' | ForEach-Object {
    Get-ADUser -Identity $_.SamAccountName -Properties samaccountname,enabled,memberof
}

方法2:多组批量处理(含存在性校验)

如果需要批量处理多个AD组,可使用如下脚本:

$Groups = "apps users","apps External Users"
$groupMemberships = foreach ($Group in $Groups) {
    $adGroup = Get-ADGroup -Identity $group
    if ($adGroup) {
         $groups2 = Get-ADGroupMember -Identity $adGroup.SamAccountName
           foreach ($group2 in $groups2) {
                Get-ADUser -Identity $group2.SamAccountName -Properties samaccountname,enabled,memberof
                }
    } 
    else {
        Write-Warning "Group '$group' could not be found.."
    }
}

内容的提问来源于stack exchange,提问作者Arbelac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 18:04:53