使用输入列表检索和导出PowerShell中的AD安全组成员身份
尝试创建我的第一个PS脚本来自动化一些重复的工作,我取得了一些有限的成功。我最初是这样开始的:使用输入列表检索和导出PowerShell中的AD安全组成员身份,powershell,active-directory,Powershell,Active Directory,尝试创建我的第一个PS脚本来自动化一些重复的工作,我取得了一些有限的成功。我最初是这样开始的: Get-ADGroupMember -Identity (Read-Host 'Enter group name') | Select samaccountname | Export-Csv -Path C:\test.csv -NoTypeInformation 这会提示我输入安全组名称,我输入它,然后它将其导出到文件中。伟大的现在我想让它做同样的事情,除了让我输入一个安全组名并重新格式化这一步,
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分别放在input.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的解析不正确。想法?我尝试了你的精确代码,它成功了。可能是输入文件中的某个内容,或者是某个组中的某个内容 尝试删除导出Csv部件,使其输出到命令行,并查看输出是否符合您的要求:
foreach ($securitygroup in Get-Content -Path C:\input.txt)
{
Get-ADGroupMember -Identity $securitygroup | Select samaccountname
}
首先,我建议您不要将文件存储在C:\目录中。您可能会遇到权限问题。改为创建C:\temp\文件夹。请显示
Get Content-Path C:\input.txt
cmdlet的输出。我还运行了你的代码,它运行得很好。