提问人:Aaron Kenny 提问时间:11/16/2023 最后编辑:DavidAaron Kenny 更新时间:11/16/2023 访问量:37
嗨,我正在尝试将此数据导出到 csv 文件,但无法弄清楚,有人知道吗?非常感谢帮助
Hi I am trying to export this data to a csv file but cant figure it out does anyone know? help is greatly apprciated
问:
$infile = "C:\Temp\check.txt"
$users = Get-Content $infile
foreach ($user in $users){ Get-ADUser -Filter {name -like $user -or samaccountname -like $user} | Select Name, SamAccountName,Enabled } | export-csv "C:\temp\Git.csv"
答:
2赞
Mathias R. Jessen
11/16/2023
#1
不能将流控制语句(如循环)用作管道表达式中的命令元素。foreach(...){...}
惯用的解决方法是使用 cmdlet:ForEach-Object
$infile = "C:\Temp\check.txt"
$users = Get-Content $infile
$users |ForEach-Object {
$user = $_
Get-ADUser -Filter {name -like $user -or samaccountname -like $user} | Select Name, SamAccountName,Enabled
} | Export-csv "C:\temp\Git.csv"
或者,可以将整个语句包装在脚本块中,然后将其用作管道中的第一个命令:
$infile = "C:\Temp\check.txt"
$users = Get-Content $infile
& {
foreach($user in $users) {
Get-ADUser -Filter {name -like $user -or samaccountname -like $user} | Select Name, SamAccountName,Enabled
}
} | Export-csv "C:\temp\Git.csv"
评论
0赞
Aaron Kenny
11/16/2023
非常感谢 yo0ur 的帮助,效果很好
评论