powershellssm

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


I am using a ssm command to describe instance patches, while getting the output the installedtime is in unspecified format, so how to convert it to the format of mm/dd/yy

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

The default output of the ssm command is json

output:

|| 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

Solution

  • This script work like as expected

        $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