如何在powershell中将SSM输出和unix时间格式转换为系统时间以保存为csv

How to convert the SSM output & unix time format to system time in powershell to save as csv

我正在使用 ssm 命令来描述实例补丁,同时得到的输出是 installedtime 是未指定的格式,那么如何将其转换为 mm/dd/yy

的格式
aws ssm describe-instance-patches --instance-id "XXXXXX" --profile "XXXXXX" --region "us-west-2"  --max-results 10 --output table 

ssm命令的默认输出是json

输出:

|| Classification | InstalledTime | KBId | Severity | State | Title
|| CriticalUpdates | 1476770400.0 | KB3199209 | Unspecified | Installed | Update for Windows Server 2016 for x64-based Systems (KB3199209)
|| CriticalUpdates | 1479193200.0 | KB3199986 | Unspecified | Installed | Update for Windows Server 2016 for x64-based

用输出而不是图像更新您的问题。

日期格式不是未指定格式,是Unix格式。

要从 Unix 格式转换为 DateTime 变量,请执行以下操作:

$oUNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(1476770400))

我无法对此进行测试,但您可以像这样在之后转换输出:

aws ssm describe-instance-patches --instance-id "XXXXXX" --profile "XXXXXX" --region "us-west-2"  --max-results 10 --output table | select-object Classification, @{ Name = 'TimeStamp'; Expression = {  ((Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($_.InstalledTime))) }}, KBId, Severity, State, Title

此脚本按预期工作

    $instances= aws ssm describe-instance-patches --instance-id "xxxxxxx" --profile "xxxxxxx" --region "us-west-2"  --max-results 50
$pathToOutputFile = "C:\Users\Documents\prod_instance_us_west_2.csv"
$array = ($instances| ConvertFrom-Json) | Select-Object -ExpandProperty Patches

$report = @()
foreach($a in $array)
{
    $report += New-Object psobject -Property @{Title=$a.Title;KBId=$a.KBId;Classification=$a.Classification;Severity=$a.Severity;State=$a.State;InstalledTime=[TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($a.InstalledTime))}
}
$report | export-csv $pathToOutputFile