我有一个输出两列的小型 PowerShell 脚本:
我需要将其输出到 CSV 文件,但该集合仅输出为 error-gobbledygook。
我知道我需要将其转换为字符串或迭代或其他内容,但我无法弄清楚这一点。
$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:\Users\xxx\Desktop\ActiveSharedCapacityWorkspaceUsers.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:\Users\xxx\Desktop\ActiveSharedCapacityWorkspaceUsers.csv"
$ActiveSharedCapacityWorkspaceUsers | Export-Csv $ExportFile -NoTypeInformation
-NoTypeInformation抑制生成的 CSV 文件的第 0 行中的类型信息。如果您使用的是 PowerShell 6 类型的信息生成,则默认情况下是禁用的。
关于powershell - 无法输出到 CSV 文件。我知道输出是一个大括号 "collection",但我很难过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485327/