使用输入列表在 PowerShell 中检索和导出 AD 安全组成员身份



尝试创建我的第一个PS脚本来自动化一些重复性工作,但我取得了一些有限的成功。我最初是从这个开始的:

Get-ADGroupMember -Identity (Read-Host 'Enter group name') | Select samaccountname | Export-Csv -Path C:test.csv -NoTypeInformation

这会提示我输入安全组名称,我输入它,然后将其导出到文件中。伟大!现在我希望它做同样的事情,除了让我输入安全组名称并重新执行此步骤一百次之外,我希望它自动从文件中提取输入。唉,我们有..

foreach ($securitygroup in Get-Content -Path C:input.txt)
{
Get-ADGroupMember -Identity $securitygroup | Select samaccountname | Export-Csv -Path C:test.csv -Append -NoTypeInformation
}

我将 SG1 和 SG2 放在输入中的不同行上.txt并收到以下错误:

Export-Csv : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again.
At C:test script.ps1:3 char:70
+ ... countname | Export-Csv -Path C:test.csv -Appe ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ExportCsvCommand
Get-ADGroupMember : An operations error occurred
At C:test script.ps1:3 char:1
+ Get-ADGroupMember -Identity $securitygroup | Select samaccountname |  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (SG2:ADGroup) [Get-ADGroupMember], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8224,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

看起来这里发生了两件不同的事情 - 第一个错误涉及参数"name"值,第二个错误是因为 SG2 以某种方式解析不正确。思潮?

我尝试了您的确切代码,它起作用了。也许它是输入文件中的内容,或者是其中一个组中的内容。

尝试删除 Export-csv 部分,使其输出到命令行,并查看输出是否是您想要的:

foreach ($securitygroup in Get-Content -Path C:input.txt)
{
Get-ADGroupMember -Identity $securitygroup | Select samaccountname
}

最新更新