I am searching Splunk using powershell via API and get a list of data that contain dates (not in DateTime format).
The API call is performed by a Function that works fine and it works for the inital search which required the search and a start and end date/time.
Once the initial search completes I am looping over the results and performing sub-searches looking (in Splunk) looking for additional data from a different index.
I take the returned date and time (e.g. 16/02/2024 12:45:27) split on the space and just return the date portion and then create two fields:
$EmailSearchStartdt = "$(($DateConversion).split(" ")[0]) 00:00:00"
$EmailSearchEnddt = "$(($DateConversion).split(" ")[0]) 23:59:59"
This is all failing on the the two date fields. I'm taking the dates from the initial search and turning them back into datetime as so:
$EmailSearchStartdt = ([datetime]::ParseExact($EmailSearchStartdt,"dd/MM/yyyy HH:mm:ss", [System.Globalization.CultureInfo]::GetCultureInfo("en-GB")))
$EmailSearchEnddt = ([datetime]::ParseExact($EmailSearchEnddt,"dd/MM/yyyy HH:mm:ss", [System.Globalization.CultureInfo]::GetCultureInfo("en-GB")))
The problem is Powershell is reversing the day and month into US format (I'm in the UK with UK timezone settings etc) and when fed back into the function I get the following:
EmailSearchStartdt: 15/03/2024 00:00:00 (this is the string) EmailSearchStartdt: **03/15**/2024 00:00:00 (this is after the parseexact) EmailSearchEnddt: 15/03/2024 23:59:59 (this is the string) EmailSearchEnddt: **03/15**/2024 23:59:59 (this is after the parseexact)
The result is Splunk finds nothing as the date has been reversed.
If I do the following:
$EmailSearchStartdt = get-date -date $EmailSearchStartdt -Format "dd MMM yyyy HH:mm:ss"
$EmailSearchEnddt = get-date -date $EmailSearchEnddt -Format "dd MMM yyyy HH:mm:ss"
Splunk seems to accept this but if I try:
$EmailSearchStartdt = (get-date -date $EmailSearchStartdt -Format "dd MMM yyyy HH:mm:ss").adddays(-3)
$EmailSearchEnddt = (get-date -date $EmailSearchEnddt -Format "dd MMM yyyy HH:mm:ss").adddays(1)
I get: Method invocation failed because [System.String] does not contain a method named 'adddays'.
So Powershell is seeing this as a string.
Ultimately the script is being invked with two dates that are strings but the function rejects this when its called internally with the sub-searches.
This has been literally driving me utterly mad so any help would be appreciated!
The core problem is that expandable (interpolating), double-quoted strings (
"...") in PowerShell use the invariant culture for stringifying non-string values - irrespective of what culture is currently effect (as reported byGet-Culture), and the invariant culture is based on (but distinct from) theen-USculture with its month-first date format.For instance, an expandable string such as
"$(Get-Date 1970-12-01)"always yields12/01/1970 00:00:00, i.e. it reports the month first, which explains the results of your own attempts (e.g,"$(($DateConversion).split(" ")[0]) 00:00:00").By contrast, the
[datetime]type's.ToString()method (its parameter-less overload) uses the current culture, so that(Get-Date 1970-12-01).ToString()'s output varies by culture, and yields01/12/1970 00:00:00(day first) in theen-GBculture - however you may pass a cultural context explicitly, as shown below.Therefore, it's best to perform any calculations and modifications based on
[datetime]instances first, and then create a string representation explicitly based on the culture of interest (which may be the current one).In your case, this means:
Note that if your current culture is also the target culture, you can omit the
$cultureargument in the calls above.