Jump to content

Recommended Posts

Posted

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

Posted (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 by David44
  • Thanks 3
Posted

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

  • Thanks 1
Posted
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.
  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...