嗨,我试图将此数据导出到一个CSV文件,但无法弄清楚,有人知道吗?

r1zhe5dt  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(90)
$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"

字符串

e1xvtsh3

e1xvtsh31#

不能在管道表达式中使用流控制语句(如foreach(...){...}循环)作为命令元素。
惯用的解决方法是使用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"

字符串
或者,您可以将整个语句 Package 在脚本块中,然后将其用作管道中的第一个命令:

$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"

相关问题