【问题标题】:Powershell: Format-Table | Export-CsvPowershell:格式表 |导出-CSV
【发布时间】:2020-12-05 19:14:33
【问题描述】:

我需要从 Power BI 管理门户中导出一些数据。

Get-PowerBiWorkspace | Format-Table | Export-Csv "MyFile.csv"

现在人们希望将数据保存在 csv 中。但是Get-PowerBiWorkspace | Format-Table的结果在csv中没有,只有一列和很多空列。

我做错了什么?

【问题讨论】:

  • Format-* 命令永远不应该用于任何你想通过管道传递给其他任何东西的东西。它们只能用于显示事物。

标签: powershell


【解决方案1】:

留下Format-Table。它正在破坏您要输出到 CSV 文件的对象并将它们转换为其他对象。

C:\> Get-Process | Select -First 2

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
------    -----      -----     ------      --  -- -----------
    25    30,54      32,29       0,16    3168   1 ApplicationFrameHost
    9     1,82       6,34       0,00    3832   0 armsvc

C:\> (Get-Process | Select -First 2)[0].GetType().FullName
System.Diagnostics.Process

C:\> Get-Process | Select -First 2 | Format-Table

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
------    -----      -----     ------      --  -- -----------
    25    30,54      32,29       0,16    3168   1 ApplicationFrameHost
    9     1,82       6,34       0,00    3832   0 armsvc

C:\> (Get-Process | Select -First 2 | Format-Table)[0].GetType().FullName
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData

C:\> (Get-Process | Select -First 2 | Format-Table)[0] | Get-Member


TypeName: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData

Name                                    MemberType Definition
----                                    ---------- ----------
Equals                                  Method     bool Equals(System.Object obj)
GetHashCode                             Method     int GetHashCode()
GetType                                 Method     type GetType()
ToString                                Method     string ToString()
autosizeInfo                            Property   Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 autosizeInfo {get;set;}
ClassId2e4f51ef21dd47e99d3c952918aff9cd Property   string ClassId2e4f51ef21dd47e99d3c952918aff9cd {get;}
groupingEntry                           Property   Microsoft.PowerShell.Commands.Internal.Format.GroupingEntry, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 groupingEntry {get;set;}
pageFooterEntry                         Property   Microsoft.PowerShell.Commands.Internal.Format.PageFooterEntry, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 pageFooterEntry {get;set;}
pageHeaderEntry                         Property   Microsoft.PowerShell.Commands.Internal.Format.PageHeaderEntry, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 pageHeaderEntry {get;set;}
shapeInfo                               Property   Microsoft.PowerShell.Commands.Internal.Format.ShapeInfo, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 shapeInfo {get;set;}

没有Format-Table你应该很好。

【讨论】:

  • 谢谢。你说的对。这很奇怪,因为Get-PowerBiWorkspace | Format-Table 显示了更多列表。很好,非常感谢!
【解决方案2】:

当您发送到 csv 时,您不想先通过格式表发送它。直接从get-powerbiworkspace到output-csv即可:

Get-PowerBiWorkspace | Export-Csv "MyFile.csv"

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多