Powershell:格式化表格|导出Csv



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

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

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

我做错了什么?

忽略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你应该很好。

当您发送到csv时,您不希望首先通过格式表发送。只需直接从获取powerbiworkspace到输出csv:

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

最新更新