tri_94 Posted August 24, 2021 Posted August 24, 2021 Hi there I have a variable which includes a date from Sophos API currently it shows like "2021-07-29T11:18:54.000Z", so i'm using a split on the "T" but i'm left with year-month-day and i would really like it to be day-month-year. As this data is pulled from the API. I don't think I can change the order in which its pulled in, so looking at trying to swap the string about to give me a the correct format. I've now got a working method however I'm sure this is a better way of scripting this, can anyone advise ? Thanks $Selected = $Problem | Where-Object health -ilike "*bad*" | Select-object @{Name="Computer";Expression={$_.hostname}} , @{Name='Tamper Enabled';Expression={($_.tamperProtectionEnabled)}}, @{Name='Last Seen';Expression={($_.lastSeenAt.Split("T")[0][8..9] -join "")+(($_.lastSeenAt.Split("T")[0][4..4] -join "-"))+(($_.lastSeenAt.Split("T")[0][5..6] -join ""))+(($_.lastSeenAt.Split("T")[0][4..4] -join "-"))+(($_.lastSeenAt.Split("T")[0][0..3] -join ""))}} | Out-GridView -Title "Machines with problems: $Problemcount" -PassThru
David44 Posted August 24, 2021 Posted August 24, 2021 (edited) Could you convert it to an actual date rather than a string and then output it in whatever format you need? $oldDateString = "2021-07-29T11:18:54.000Z" $newDate = get-date $oldDateString -Format "ddMMy" $newDateString = [string]$newDate Or, get-date "2021-07-29T11:18:54.000Z" -Format "ddMMy" Edited August 24, 2021 by David44 3
tri_94 Posted August 25, 2021 Author Posted August 25, 2021 Thanks for that, i'll have a look at how i can use it.
HPlum78 Posted August 26, 2021 Posted August 26, 2021 When you make the call to the API, the API returns a string that contains that date? I dont know the API but can you not output that as json? If so it would make using the output so much easier... 1
HPlum78 Posted August 26, 2021 Posted August 26, 2021 So just had a quick look at the sophos API specs, says it outputs as JSON so put the output into a var and then pipe the var into convertfrom-json then you will be able to do var.date and such likes. Essentially formatting the var as structured JSON. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now