ConvertFrom-String返回无法以表格形式查看的System.Array

时间:2018-07-12 10:39:44

标签: powershell

我正在尝试将bash中执行的whois命令的结果解析为powershell对象。但是,生成的$obj并不符合我的期望,我不确定为什么。

$obj的生成方式如下:

$domain = "domain.uk"

$whoisResult = bash -c "whois $domain"

$template = "@
        Domain name:
        {Domain:domain.uk}

    Data validation:
        {Validation:Nominet was not able to match the registrant's name and/or address against a 3rd party source on 29-Jan-2016}

    Registrar:
        {Registrar:RegistryGate GmbH [Tag = REGISTRYGATE-DE]}
        {RegistrarURL:URL: http://www.registrygate.com}

    Relevant dates:
        Registered on: {RegistrationDate:29-Jan-2016}
        Expiry date:  {ExpiryDate:29-Jan-2019}
        Last updated:  {LastUpdated:28-Jan-2018}

    Registration status:
        {Status:Registered until expiry date}.

    Name servers:
        {NameServer*:dns.dns1.de}
        dns.dns2.de
        dns.dns3.de
        dns.dns4.de

    WHOIS lookup made at {TimeStamp:18:09:22 11-Jul-2018} 
@"

$obj = $whoisResult | ConvertFrom-String -TemplateContent $template

当我尝试在表格视图中查看此对象时,它仅显示domain属性。如果我使用Format-List查看它,那么它将显示所有属性,但用空格分隔。

最后,我注意到模板中完全没有TimeStamp

所以我的问题是

  1. 我是否需要在模板中转义:
  2. 如何修改结果对象,以便可以表格形式查看它?
  3. $obj显示为System.Array,这正确吗,应该是数组吗?

在此先感谢您,这是我第一次尝试解析来自字符串的更复杂的输出,因此,如果有更好的方法,那么我也愿意改变我的方法。

PS C:\Users\jacob> $obj

Domain   
------   
domain.uk














PS C:\Users\jacob> $obj | ft

Domain   
------   
domain.uk














PS C:\Users\jacob> $obj | fl


Domain : domain.uk

Validation : Nominet was not able to match the registrant's name and/or address against a 3rd party source on 29-Jan-2016

Registrar : RegistryGate GmbH [Tag = REGISTRYGATE-DE]

RegistrarURL : URL: http://www.registrygate.com

RegistrationDate : 29-Jan-2016

ExpiryDate : 29-Jan-2019

LastUpdated : 28-Jan-2018

Status : Registered until expiry date

NameServer : dns.dns1.de

NameServer : dns.dns2.de

NameServer : dns.dns3.de

NameServer : dns.dns4.de




PS C:\Users\jacob> $obj.getType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     Object[]                                 System.Array                                                                                                  

PS C:\Users\jacob> $obj | gm -Force


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition                                                                                                                                      
----        ----------   ----------                                                                                                                                      
pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] pst...
psadapted   MemberSet    psadapted {ToString, Equals, GetHashCode, GetType}                                                                                              
psbase      MemberSet    psbase {ToString, Equals, GetHashCode, GetType}                                                                                                 
psextended  MemberSet    psextended {Domain}                                                                                                                             
psobject    MemberSet    psobject {BaseObject, Members, Properties, Methods, ImmediateBaseObject, TypeNames, get_Members, get_Properties, get_Methods, get_ImmediateBa...
Equals      Method       bool Equals(System.Object obj)                                                                                                                  
GetHashCode Method       int GetHashCode()                                                                                                                               
GetType     Method       type GetType()                                                                                                                                  
ToString    Method       string ToString()                                                                                                                               
Domain      NoteProperty string Domain=domain.uk  

$whoisResult的输出

PS C:\Users\jacob> $whoisResult

    Domain name:
        domain.uk

    Data validation:
        Nominet was not able to match the registrant's name and/or address against a 3rd party source on 29-Jan-2016

    Registrar:
        RegistryGate GmbH [Tag = REGISTRYGATE-DE]
        URL: http://www.registrygate.com

    Relevant dates:
        Registered on: 29-Jan-2016
        Expiry date:  29-Jan-2019
        Last updated:  28-Jan-2018

    Registration status:
        Registered until expiry date.

    Name servers:
        dns.dns1.de
        dns.dns2.de
        dns.dns3.de
        dns.dns4.de

    WHOIS lookup made at 11:40:09 12-Jul-2018

-- 
This WHOIS information is provided for free by Nominet UK the central registry
for .uk domain names. This information and the .uk WHOIS are:

    Copyright Nominet UK 1996 - 2018.

You may not access the .uk WHOIS or use any data from it except as permitted
by the terms of use available in full at https://www.nominet.uk/whoisterms,
which includes restrictions on: (A) use of the data for advertising, or its
repackaging, recompilation, redistribution or reuse (B) obscuring, removing
or hiding any or all of this notice and (C) exceeding query rate or volume
limits. The data is provided on an 'as-is' basis and may lag behind the
register. Access may be withdrawn or restricted at any time. 

0 个答案:

没有答案