Using DateTimePicker value with StartJob button

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 1 day old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
apowershelluser
Posts: 190
Last visit: Mon Mar 18, 2024 7:13 pm
Answers: 1

Using DateTimePicker value with StartJob button

Post by apowershelluser »

My form has a Start-Job button to call an API
If I statically set the date within the job, everything works as expected
  1. "Attribute": "Timestamp",
  2. "Operator": ">=",
  3. "Value": "$((Get-Date).AddDays(-1).ToString('yyyy-MM-ddT00:00:00.0000Z'))"
When I attempt to use a datetime picker , the json call fails
$StartDate = "$($dtpickerStart.Value.ToString('yyyy-MM-ddT00:00:00.0000Z'))"
$EndDate = "$($dtpickerEnd.Value.ToString('yyyy-MM-ddT00:00:00.0000Z'))"
  1. "Attribute": "Timestamp",
  2. "Operator": ">=",
  3. "Value": "$StartDate"
I don't know how to debug with it being in a jobscript and this would really be useful to my form
Snag_8ec8e40.png
Snag_8ec8e40.png (3.54 KiB) Viewed 1770 times
ERROR: [{"ErrorCode":"API.InternalServerError","Message":"Something went wrong while processing request. Error: Internal error.","Data":["Internal error"]}]
ERROR: + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
ERROR: + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
ERROR: + PSComputerName : localhost
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using DateTimePicker value with StartJob button

Post by jvierra »

You cannot use json directly in PowerShell. You can create strings contain json.

There is not enough information to understand what you are doing. "Jobs" are just PowerShell code.

This is how to create a json string in PowerShell:
  1. $jstr = @"
  2. {
  3.    "Attribute": "Timestamp",
  4.     "Operator": ">=",
  5.     "Value": "$StartDate"
  6. }
  7. "@
User avatar
apowershelluser
Posts: 190
Last visit: Mon Mar 18, 2024 7:13 pm
Answers: 1

Re: Using DateTimePicker value with StartJob button

Post by apowershelluser »

The “json” is the body I sent with my invoke-restmethod call - so a string that looks like json

It’s resolved I was overthinking it and my scoping was wrong
This topic is 1 year and 1 day old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked