Powershell输出问题

时间:2017-11-11 23:53:49

标签: powershell scripting command-line-interface

我的输出问题,这是我当前的脚本:

param ([string]$myhost = ‘localhost’)
Clear-Host
Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter IPEnabled=True -ComputerName $myhost |  
  select-object -Property IPAddress, Mac, Description
 "{0,-10}{1,30}{2,60}" -f $_.mac, $_.ipaddress,  $_.description

了解如何让我的输出类似于:

enum-nic localhost
74:86:7A:6F:4A:CD   198.108.110.80   fe80::7dd5:140c:90d3:9be2   Broadcom
NetXtreme 57xx Gigabit Controller

这是我目前的输出:

IPAddress                                  Mac Description                      
---------                                  --- -----------                      
{192.168.87.34, fe80::10c3:482e:799e:85bb}     Broadcom 802.11ac Network Adapter

请感谢任何建议!

1 个答案:

答案 0 :(得分:0)

您似乎希望将PowerShell输出控制为文本流。这不是PowerShell自然的工作方式。需要显式文本格式来控制输出。

PowerShell的本质是输出对象。这会得到你想要的吗?

C:>Get-Content .\enum-nic.ps1
param ([string]$myhost = 'localhost')
Clear-Host
Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter IPEnabled=True -ComputerName $myhost |
    Select-Object -Property IPAddress, MacAddress, Description |
    ForEach-Object { "{0,-18}{1,16}{2,30}   {3}" -f $_.MacAddress, $_.IPAddress[0],  $_.IPAddress[1], $_.Description }

样本运行的输出。

0A:00:27:00:00:09     192.168.56.1     fe80::79aa:55e9:56de:bb9b   VirtualBox Host-Only Ethernet Adapter
FC:AA:14:0D:7C:86     192.168.0.62     fe80::5199:d760:a95d:bda4   Realtek PCIe GBE Family Controller
00:15:5D:16:E9:68     172.28.144.1     fe80::cc8f:e85d:ec8f:42e2   Hyper-V Virtual Ethernet Adapter
76:15:76:DB:53:BC   172.25.156.225     fe80::6190:503c:ed81:123f   Hyper-V Virtual Ethernet Adapter #2