[英]Powershell, Run For Each Loop and Export Output to .csv
I'm pretty new to Powershell so I was wondering if I could get some help... 我对Powershell还是很陌生,所以我想知道是否可以得到一些帮助...
I'm running this code down here in order to loop through each distribution group to pull distribution group members. 我正在此处运行此代码,以便循环遍历每个通讯组以拉出通讯组成员。
Foreach ($objDistributionGroup in $objDistributionGroups)
{
write-host "Processing $($objDistributionGroup.DisplayName)..."
#Get members of this group
$objDGMembers = Get-DistributionGroupMember -Identity $($objDistributionGroup.PrimarySmtpAddress)
write-host "Found $($objDGMembers.Count) members..."
#Iterate through each member
Foreach ($objMember in $objDGMembers)
{
Out-File -FilePath $OutputFile = "C:\DistributionGroupWithMembers" -InputObject "$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)" -Encoding UTF8 -append
write-host "`t$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)"
}
}
I get this error: 我收到此错误:
Out-File : Cannot bind argument to parameter 'FilePath' because it is null. Out-File:无法将参数绑定到参数'FilePath',因为它为null。 At line:14 char:22 在线:14字符:22
How do I properly set the Output location? 如何正确设置输出位置? Ultimately, I want to save the output to a .csv for me to open. 最终,我想将输出保存到.csv中以供打开。
The problem might be how you assign $OutputFile = "C:\\DistributionGroupWithMembers"
in the parameters. 问题可能是如何在参数中分配$OutputFile = "C:\\DistributionGroupWithMembers"
。 Try something like this: 尝试这样的事情:
Out-File -FilePath "C:\DistributionGroupWithMembers.csv" -InputObject ....
You could also potentially look at using Export-Csv
or ConvertTo-Csv
, but since you seperate values with ,
inside InputObject this might be fine. 您也可能会考虑使用Export-Csv
或ConvertTo-Csv
,但是由于您在InputObject内使用分隔了值,
因此可能会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.