ConvertFrom-String and ghost property

2019-08-06 06:33发布

I am getting the firmware information of a server and I have it in a variable $t but it's in the form of a string. I've (almost) successfully structured it into an object in order to export it into a CSV but there are a few kinks I need help with.

My main problem at the moment is that it seems like using ConvertFrom-String changes things like 4.10 to 4 or 2017/04/06 to 4/6/2017 12:00:00 AM. Is there any way around this?

Another issue is the ghost property P6 that shows up even though I am not explicitly asking for it. I deal with this by removing it afterwards but I wonder if I'm doing something wrong to spawn it.

PS C:\Users\user> $t.Output
system> Type                     Status       Version      BuildID          ReleaseDate
----                     ------       -------      -------          -----------
IMM2(Primary)            Active       4.10         TCOO32C          2017/04/06
IMM2(Backup)             Inactive     3.70         TCOO26H          2016/11/29
UEFI(Primary)            Active       2.40         TCE130J          2017/04/11
UEFI(Backup)             Inactive     2.40         TCE130J          2017/04/11
DSA                      Active       10.2         DSALA8N          2016/10/28
system> 

PS C:\Users\user> $t.Output | ConvertFrom-String -PropertyNames 'Type', 'Status', 'Version', 'Build', 'ReleaseDate' | Select-Object -Index 2,4

Type        : IMM2(Primary)
Status      : Active
Version     : 4
Build       : TCOO32C
ReleaseDate : 4/6/2017 12:00:00 AM
P6          : 

Type        : UEFI(Primary)
Status      : Active
Version     : 2
Build       : TCE130J
ReleaseDate : 4/11/2017 12:00:00 AM
P6          : 

Thanks in advance.

2条回答
做自己的国王
2楼-- · 2019-08-06 07:05

This 12:00AM and 4.10 to 4 stuff is happening because ConvertFrom-String is setting the type for the items, a DateTime for the date and an int for the status.

To work around this you can use the -TemplateContent parameter to explicitly set the types you want, in this case strings.

$template = @'
{Name*:IMM2(Primary)} {Status:Active} {[string]Version:4.10} {BuildID:TCOO32C} {[string]ReleaseDate:2017/04/06}
{Name*:UEFI(Primary)} {Status:Inactive} {[string]Version:10.2} {BuildID:DSALA8N} {[string]ReleaseDate:2016/10/28}
{Name*:DSA} {Status:Inactive} {[string]Version:10.2} {BuildID:DSALA8N} {[string]ReleaseDate:2016/10/28}
'@

$data | ConvertFrom-String -TemplateContent $template -OutVariable out_data
$out_data
查看更多
可以哭但决不认输i
3楼-- · 2019-08-06 07:18

After replacing the hindering elements from the simulated output,
ConvertFrom-Csv can do its job (the time still missing in your sample)

$toutput= @"
system> Type                     Status       Version      BuildID          ReleaseDate
----                     ------       -------      -------          -----------
IMM2(Primary)            Active       4.10         TCOO32C          2017/04/06
IMM2(Backup)             Inactive     3.70         TCOO26H          2016/11/29
UEFI(Primary)            Active       2.40         TCE130J          2017/04/11
UEFI(Backup)             Inactive     2.40         TCE130J          2017/04/11
DSA                      Active       10.2         DSALA8N          2016/10/28
system> 
"@

$toutput -replace 'system> *' -replace '(--+\s+)+' -replace '\s{2,}',',' |
    ConvertFrom-CSV | Format-Table -auto

Type          Status   Version BuildID ReleaseDate
----          ------   ------- ------- -----------
IMM2(Primary) Active   4.10    TCOO32C 2017/04/06
IMM2(Backup)  Inactive 3.70    TCOO26H 2016/11/29
UEFI(Primary) Active   2.40    TCE130J 2017/04/11
UEFI(Backup)  Inactive 2.40    TCE130J 2017/04/11
DSA           Active   10.2    DSALA8N 2016/10/28
查看更多
登录 后发表回答