Powershell 2.0 Format-list output to JSON?

匆匆过客 提交于 2019-12-25 01:55:52

问题


PS B:\abrabackups> . C:\ps\ConvertTo-JSON.ps1
PS B:\abrabackups> Get-ItemProperty -Path .\AbraSuite01.03.2014 | select Name,CreationTime | ConvertTo-JSON

And that gives me: (formatted for clarity)

{
 "CreationTime": "2014-01-03T16:48:36", 
 "Name": "AbraSuite01.03.2014"
}

Well that's all well and good, but suppose I want my dates in a different format in the JSON string, is there anyway to do this in powershell and still use my shoehorned ConvertTo-JSON.ps1 for powershell 2.0?


回答1:


Convert your dates to the format you want before converting to JSON by using a calculated property. For example:

Get-ItemProperty -Path .\AbraSuite01.03.2014 | select Name,@{Name="CreationTime";Expression={$_.CreationTime.ToShortDateString()}} | ConvertTo-JSON

Replace $_.CreationTime.ToShortDateString() with whatever you need to get the date format you're after - but the key here is to use $_.CreationTime to grab the creation time of the object in the pipeline.



来源:https://stackoverflow.com/questions/22156504/powershell-2-0-format-list-output-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!