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

Get-ADGroup赋值$groups后脚本报错,求原因及解决方法

问题排查:Get-ADGroup赋值$groups后脚本执行失败

当用Get-Content读取CSV文件给$groups变量赋值时脚本正常运行,但改用Get-ADGroup获取组列表赋值给$groups后,脚本抛出参数绑定错误,无法执行。需求是将逻辑整合到单个脚本,无需提前生成CSV文件。

可运行代码

$groups = Get-Content C:\groups.csv 

$results = @()

$file = "C:\Usuarios_Grupos_Darwin_AD.csv"  

foreach($Group in $Groups) { 
    $results += Get-ADGroupMember -Id $Group -Recursive | %{Get-ADUser -Identity $_.SamAccountName -Properties Enabled,Name} | Select @{Expression={$Group};Label="Group Name"},SamAccountName,Name,Enabled
}

$results | export-csv -notypeinformation -Delimiter ";" -path $file 

无法运行代码(仅修改第一行)

$groups = Get-ADGroup -Filter {Name -like '*Darwin*'}  -Properties * | select -property Name

$results = @()

$file = "C:\Usuarios_Grupos_Darwin_AD.csv"  

foreach($Group in $Groups) { 
    $results += Get-ADGroupMember -Id $Group -Recursive | %{Get-ADUser -Identity $_.SamAccountName -Properties Enabled,Name} | Select @{Expression={$Group};Label="Group Name"},SamAccountName,Name,Enabled
}

$results | export-csv -notypeinformation -Delimiter ";" -path $file

报错信息

Get-ADGroupMember : Cannot bind parameter 'Identity'. Cannot create object of type "Microsoft.ActiveDirectory.Management.ADGroup". The adapter cannot set the value of property
"Name".
At line:11 char:34

  • $results +=Get-ADGroupMember -Id $Group -Recursive | %{Get-ADUser -Id ...
  • ~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Get-ADGroupMember], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

原因分析

  • Get-Content输出的是纯字符串类型的组名,Get-ADGroupMember的-Identity参数可直接识别字符串格式的组名。
  • Get-ADGroup | select -property Name输出的是自定义对象(每个对象仅包含Name属性),而非纯字符串。Get-ADGroupMember无法将该对象解析为有效身份参数,导致绑定失败。

解决方案

方式一:提取纯字符串组名(推荐)

通过Select-Object -ExpandProperty Name直接获取Name属性的字符串值,替代原有的select -property Name:

$groups = Get-ADGroup -Filter {Name -like '*Darwin*'} | Select-Object -ExpandProperty Name

$results = @()
$file = "C:\Usuarios_Grupos_Darwin_AD.csv"  

foreach($Group in $Groups) { 
    $results += Get-ADGroupMember -Identity $Group -Recursive | ForEach-Object {
        Get-ADUser -Identity $_.SamAccountName -Properties Enabled,Name
    } | Select-Object @{Expression={$Group};Label="Group Name"},SamAccountName,Name,Enabled
}

$results | Export-Csv -NoTypeInformation -Delimiter ";" -Path $file

方式二:循环中引用对象的Name属性

若需保留组对象的扩展属性,可在循环内明确调用$Group.Name:

$groups = Get-ADGroup -Filter {Name -like '*Darwin*'} | Select-Object Name

$results = @()
$file = "C:\Usuarios_Grupos_Darwin_AD.csv"  

foreach($Group in $Groups) { 
    $currentGroupName = $Group.Name
    $results += Get-ADGroupMember -Identity $currentGroupName -Recursive | ForEach-Object {
        Get-ADUser -Identity $_.SamAccountName -Properties Enabled,Name
    } | Select-Object @{Expression={$currentGroupName};Label="Group Name"},SamAccountName,Name,Enabled
}

$results | Export-Csv -NoTypeInformation -Delimiter ";" -Path $file

性能优化建议

避免用$results +=频繁扩容数组(效率低下),直接将循环结果赋值给$results:

$groups = Get-ADGroup -Filter {Name -like '*Darwin*'} | Select-Object -ExpandProperty Name
$file = "C:\Usuarios_Grupos_Darwin_AD.csv"  

$results = foreach($Group in $Groups) { 
    Get-ADGroupMember -Identity $Group -Recursive | ForEach-Object {
        Get-ADUser -Identity $_.SamAccountName -Properties Enabled,Name
    } | Select-Object @{Expression={$Group};Label="Group Name"},SamAccountName,Name,Enabled
}

$results | Export-Csv -NoTypeInformation -Delimiter ";" -Path $file

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 14:35:21