简体   繁体   中英

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

Update your question with the output instead of an Image.

The dateformat is not unspecified format, it is Unix format.

To convert from Unix format to a DateTime variable do like this:

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

I cant test this but you can probably do something like this to convert the output afterwards:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM