输出到 CSV 文件时遇到问题。我知道输出是一个大括号"collection",但我被难住了



我有一个输出两列的小PowerShell脚本:

  • 标记为"名称"和"的字符串
  • 标记为"用户"的集合(大括号中(

我需要将其输出到 CSV 文件,但该集合只是输出为错误吞噬。

我知道我需要将其转换为字符串或迭代或其他东西,但我无法弄清楚这一点。

$ActiveSharedCapacityWorkspaces = Get-PowerBIWorkspace -Scope Organization -All | Where {($_.IsOnDedicatedCapacity -eq $false) -and ($_.State -eq "Active") -and ($_.Type -ne "PersonalGroup")}
$ActiveSharedCapacityWorkspaceUsers = $ActiveSharedCapacityWorkspaces | Select Name,Users | Get-Unique -AsString
$ExportFile = "C:UsersxxxDesktopActiveSharedCapacityWorkspaceUsers.csv"
$ActiveSharedCapacityWorkspaceUsers | Export-Csv $ExportFile

预期成果:

Name:WorkspaceName            Users:Joe Schmoe, Billy Bob

实际结果:

Name:WorkspaceName               Users:System.Linq.Enumerable+WhereSelectListIterator`2[Microsoft.PowerBI.Api.V2.Models.GroupUserAccessRight,Microsoft.PowerBI.Common.Api.Workspaces.WorkspaceUser]

Get-PowerBiWorkspace 返回一个IEnumerable,在这种情况下,您在此IEnumerable上存储 Linq 表达式。

如@LotPings所述,您必须调用Where-Linq 表达式。这是通过枚举操作(如-join(完成的(因为联接运算符必须枚举集合,这会导致解析/调用 Linq 查询(。正如@LotOfPings和@AdminOfThings建议的那样,将您的代码更改为:

$ActiveSharedCapacityWorkspaces = Get-PowerBIWorkspace -Scope Organization -All | Where {($_.IsOnDedicatedCapacity -eq $false) -and ($_.State -eq "Active") -and ($_.Type -ne "PersonalGroup")}
$ActiveSharedCapacityWorkspaceUsers = $ActiveSharedCapacityWorkspaces | Select-Object Name,@{n="Users';e={$_.Users.UserPrincipalName -join ', '}}
$ExportFile = "C:UsersxxxDesktopActiveSharedCapacityWorkspaceUsers.csv"
$ActiveSharedCapacityWorkspaceUsers | Export-Csv $ExportFile -NoTypeInformation

-NoTypeInformation 禁止显示生成的 CSV 文件第 0 行中的类型信息。如果是 PowerShell 6 类型,则默认情况下会停用信息生成。

最新更新