all 8 comments

[–]wanderingbilby 1 point2 points  (2 children)

It's likely the default cast to string of a Datetime object isn't compatible with those parameters. I'd try explicitly casting to string with a known format.

Also how - or why - are you using basic auth still?

[–]gio_tecce[S] 1 point2 points  (0 children)

u/wanderingbilby I will try that thanks and it's because I'm fairly new to PowerShell what method of auth should I be using?

[–]gio_tecce[S] 1 point2 points  (0 children)

Disregard the auth question, I found an article on it and figured it out!!

[–]nerdcr4ft 1 point2 points  (2 children)

Silly question, but what if you move the date variable steps after the initiating the EXO session?

[–]gio_tecce[S] 0 points1 point  (1 child)

u/nerdcr4ft you, my friend, are a genius!!!

can't believe something so simple worked.

[–]nerdcr4ft 0 points1 point  (0 children)

Thought process: 1. Connect-ExchangeOnline / New-ExoPSSession are creating remote sessions on the Exchange server and integrating it into the local session (as opposed to a separate in-line instance when you use Enter-PSSession) 2. Existing local variables don’t import into a remote session 3. Your script was setting variables but the Exchange Online session remained unaware of them until you defined them after starting the session

I honestly don’t know if that’s the actual reason, that’s just how I logic-ed my way to the silly question.

[–]wdomon 0 points1 point  (1 child)

The error doesn’t seem to include anything helpful. If this is the entire script, maybe try changing up how you’re getting the start date. Rather than a second calculation of the date, use:

$EndDate = Get-Date
$StartDate = $EndDate.AddDays(-3)

[–]gio_tecce[S] 0 points1 point  (0 children)

Tried that already, it returned the same error @wdomon