powershell - 无法输出到 CSV 文件。我知道输出是一个大括号 "collection",但我很难过

标签 powershell collections powerbi

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

  • 标有 Name 和
  • 的字符串
  • 标记为 Users
  • 的集合(在花括号中)

    我需要将其输出到 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/

    相关文章:

    powershell - 使用PowerShell 2.0复制/传输文件

    powershell - 如何使用powershell根据特定成员确定集合中对象的频率

    powerbi - 选择每个类别 Power BI 具有 MAX 值的行

    node.js - Power BI Developer Visual 不存在; pbiviz 启动正在监听

    tooltip - Power BI 用户可以访问工具提示滚动条吗

    powershell - Windows 使用 powershell 安排任务创建问题

    powershell - 不包含分割方法

    powershell - 'Install-Module' : is not recognized as the name of a cmdlet, 函数、脚本文件或可运行程序运行时出错

    collections - p :collector vs custom wrapper class? 的用例是什么

    java - 删除匿名监听器