question

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 asked ·

Problems with Invoke-WebRequest

Hello,

I tried to connect to my Exchange OWA site by using the code from this page:

 $Body = @{
     User = 'jdoe'
     password = 'P@S$w0rd!'
 }
 $LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST'
    
 $Session
    
 $ProfileResponse = Invoke-WebRequest 'https://www.contoso.com/profile/' -WebSession $Session
    
 $ProfileResponse

...and got confusing results:
41098-1.png

1) my $LoginResponse variable was always null
2) while executing this string
$LoginResponse = Invoke-WebRequest 'https://mail.domain.net/owa/' -SessionVariable 'Session' -Body $Body -Method 'POST'

...the IE's window would always pop up:
41099-2.png

3) in spite of the fact that the $LoginResponse was null (and with any random password supplied) the $ProfileResponse.Status returned was always 200 (OK) - I thought inability to log in to the OWA site will lead to the $ProfileResponse.Status !=200 ...

Maybe these results are by design and I don't understand anything???

Thank you in advance,
Michael


windows-server-powershell
1.png (113.5 KiB)
2.png (23.2 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

What happens if you use If ($null -eq $LoginResponse) instead? If $LoginResponse is null you'll see the error if you do the comparison your way because the variable isn't defined.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered ·

I think that this is more of a "how to programmatically connect to Exchange" question, rather than a Powershell question.

Where did you find sample code that showed that using the $Body variable as you did is the correct way to connect to OWA?

Invoke-WebRequest accepts a -Credential variable, so why not use that?

https://docs.microsoft.com/en-us/powershell/module/exchange/connect-mailbox?view=exchange-ps

https://blog.kloud.com.au/2016/04/19/programmatically-read-email-from-an-exchange-sever-mailbox/

https://www.nakivo.com/blog/how-to-connect-office-365-exchange-online-powershell/



· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't think there's anything wrong with using -Body with credentials in the data. I tried it with Microsoft 365 and it works.

What the OP's doing is NOT accounting for the possibilities that the Invoke-WebRequest may fail. This would be more reliable:

 $Body = @{
      User = 'jdoe'
      password = 'P@S$w0rd!'
  }
 Try{
      $LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST' -ErrorAction STOP
 }
 Catch{
     Write-Host "WebRewuest failed"
 }


0 Votes 0 ·
MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered ·

Hello evereone,

Thank you for your replies!

1) "What happens if you use If ($null -eq $LoginResponse) instead? " - it returns False: sorry, I was not accurate saying $LoginResponse is null because I do define the $LoginResponse as ="" (I just didn't print-screen it) so it's not null but the empty string.

2) "Where did you find sample code that showed that using the $Body variable as you did is the correct way to connect to OWA? " - why not? OWA is just one of examples of the forms-based authentication for IIS - at least I had no reasons to think that's impossible. My goal is just to check the OWA is alive so I thought it should working... Will read your articles, thanks!

3) -ErrorAction STOP

41503-9.png

Seems $LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST' does not work with OWA ...strange enough providing that the $Session variable gets created (without STOP of course) ... :(

41521-10.png



9.png (20.1 KiB)
10.png (42.1 KiB)
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered ·

P.S. With the -ErrorAction STOP IE does not pop up!

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That's a good thing. Have you retrieved the response code to see why the Invoke-WebRequest failed?

[int]$LoginResponse.StatusCode

Handling failure should be part of your script.

0 Votes 0 ·
MikhailFirsov-1277 avatar image
0 Votes"
MikhailFirsov-1277 answered ·

"Have you retrieved the response code to see why the Invoke-WebRequest failed?" - "The remote server returned an error: (440) Login Timeout." That's the bad thing :(

The "odinary" web requests work exactly as they should: connecting to the existing and alive sites return $LR.StatusCode=200, connecting to (for example) non-existing sites return nothing ($LR is null or the empty string)

$LR=Invoke-WebRequest 'https://google.ru/' - Code=200

$LR=Invoke-WebRequest 'https://gooEgle.ru/' - $LR =""

And I was wrong: IE pops up even with the -ErrorAction Stop (I made a mistake in the word Error so PS just threw syntax error but did not run IE).

Sounds strange to me that MS's own example does not work :(

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't know if the IUSR_ and IWAM_ accounts are still used for security, but you might want to try the steps in this article if they are: microsoftexchangeowa440authenticationtimeouterror
or this one (I don't subscribe to the site so I can't see the answers): OWA-issue-Exchange-2013-440-Login-Timeout.html
or this one: 198609-outlook-webaccess-2010-440-login-timeout

There are many similar articles, but I think the underlying causes of your problem (after looking over the things reported by others) is not related to PowerShell.


0 Votes 0 ·

RichMatheisen-8856, thank you for the links - will read those articles!

0 Votes 0 ·