[英]Export Powershell output to CSV
I have the following code intended to take a list of user names and output a CSV report of username - GroupMembership. 我有以下代码旨在获取用户名列表并输出用户名CSV报告-GroupMembership。 At the command line the output looks great as i get "name" on the left and recursive "group memberships" on the right (see pic http://i.stack.imgur.com/zLxUR.jpg for command line output, sorry can't post imbedded Pics yet)
在命令行中,输出看起来很棒,因为我在左侧获得“名称”,在右侧获得递归“组成员身份”(有关命令行输出,请参见图片http://i.stack.imgur.com/zLxUR.jpg ,抱歉尚不能发布嵌入的图片)
I would like to have the output written to a CSV file with the same format, namely Username in one column and GroupMemberships in the second column.. Original code from: http://thesurlyadmin.com/2013/03/21/get-a-users-group-memberships/ with a few small changes. 我想将输出写入具有相同格式的CSV文件,即一列为Username,第二列为GroupMemberships。。原始代码来自: http : //thesurlyadmin.com/2013/03/21/get- a-users-group-memberships /有一些小的更改。
Param (
[Parameter(Mandatory=$true,ValueFromPipeLine=$true)]
[Alias("ID","Users")]
[string[]]$User
)
Begin {
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?"; Break }
}
Process {
ForEach ($U in $User)
{ $UN = Get-ADUser $U -Properties MemberOf
$Groups = ForEach ($Group in ($UN.MemberOf))
{ (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{ New-Object PSObject -Property @{
Name = $UN.Name
Group = $Group
}
}
}
}
I tried using this "$PSObject | Export-CSV C:\\Scripts\\GroupMembershipList.csv" but it only writes the first line to the CSV and nothing after that. 我尝试使用此“ $ PSObject | Export-CSV C:\\ Scripts \\ GroupMembershipList.csv”,但是它仅将第一行写入CSV,之后没有任何内容。
Nate, 内特,
In Powershell v3.0, the Export-CSV cmdlet introduced the -Append parameter. 在Powershell v3.0中,Export-CSV cmdlet引入了-Append参数。
Not knowing the version of Powershell you are using, this may require an update on your side to make use of the new functionality. 不知道您使用的Powershell的版本,这可能需要您方面进行更新才能使用新功能。
In my own cases, I generally see the opposite issue if I forget to -Append to my CSV; 在我自己的情况下,如果我忘了-追加到CSV,通常会看到相反的问题; I will only end up with the LAST entry as opposed to just the first.
我只会以LAST条目结束,而不是第一个条目。
I won't claim this to be your fix, but might be worth a shot... 我不会声称这是您的解决办法,但可能值得一试...
Example: $PSObject | Export-CSV C:\\Scripts\\GroupMembershipList.csv -Append
示例:
$PSObject | Export-CSV C:\\Scripts\\GroupMembershipList.csv -Append
$PSObject | Export-CSV C:\\Scripts\\GroupMembershipList.csv -Append
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.