Powershell Out-Printer 中断 Format-Table 列布局

Powershell Out-Printer breaks Format-Table column layout

我正在尝试使用 Format-Table 打印一些过程数据。我开始直接在 shell 中测试输出。类似于(Get-Process 只是我的数据的替代品):

Get-Process | Format-Table ProcessName, Id 

问题是第一列有一些较大的单元格,我需要像这样设置一些格式:

$tableFormat = @{Expression={$_.ProcessName}; width=70}, @{Expression={$_.Handles}};
Get-Process | Format-Table $tableFormat 

在这两种情况下,shell 中的输出看起来都很好。这是 $tableFormat:

的示例
DeviceDisplayObjectProvider                        240
dllhost                                            198
dwm                                                78
explorer                                           1077
explorer                                           599
fpassist                                           61
Idle                                               0
inetinfo                                           409
InetMgr                                            314
lsass                                              1132
lsm                                                167
mqsvc                                              235
msdtc                                              152
MsDtsSrvr                                          165
notepad++                                          249
Opc.Ua.ComServerWrapper                            526
Opc.Ua.DiscoveryServer                             530
OpcEnum                                            85
OPCSim                                             204
pageant                                            111
powershell                                         489
powershell_ise                                     442
PresentationFontCache                              151
procexp                                            183
PROCEXP64                                          373
ReportingServicesService                           693
services                                           302
smss                                               30
SMSvcHost                                          298
SMSvcHost                                          151

当我尝试像这样将 table 打印到默认打印机时

...
Get-Process | Format-Table $tableFormat | lp

结果有断栏(取自 pdf 打印机,实际输出看起来一样):

到目前为止我尝试过的其他事情:

这个问题可能与我之前的问题有关,当时我仍然尝试用制表符手动格式化table。 参见:Why does Powershell Out-Printer ignore `f (new page) in text?

乍一看,这像是字体问题。它可能是也可能不是,但至少值得检查一下。在某些字体中,所有字符的宽度都相同。在其他情况下,每个字符可能具有唯一的宽度。

Powershell 的控制台界面通常使用固定宽度的字体。您从 format-table 获得的格式可能假定所有字符的宽度相同。如果您使用可变宽度字体在打印机上打印它,您将获得与您向我们展示的一样的输出。

您可以通过某种方式向您的打印机指定要使用固定宽度的字体来检查这一点。 "Courier" 就是这样一种字体。我不能告诉你如何在你的情况下做到这一点,因为我不知道细节。

如果我的猜测是正确的,那么指定固定宽度的字体应该会使列看起来再次对齐。

要检查的另一件事是设置制表位的位置。